-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Add env command #1690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Add env command #1690
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add env command
- Loading branch information
commit 22debb88f7fb0d429aea2820289b2927eac508c1
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package env | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/cli/cli/pkg/cmdutil" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewCmdEnv(f *cmdutil.Factory) *cobra.Command { | ||
| var verbose bool | ||
| cmd := &cobra.Command{ | ||
| Use: "env", | ||
| Short: "Display environment variables for gh", | ||
| Long: "Display values for gh environment variables.", | ||
| Args: cobra.NoArgs, | ||
|
|
||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| envVars := map[string]string{ | ||
| "GITHUB_TOKEN": "an authentication token for github.com API requests. Setting this avoids\nbeing prompted to authenticate and takes precedence over previously stored credentials.", | ||
| "GITHUB_ENTERPRISE_TOKEN": "an authentication token for API requests to GitHub Enterprise.", | ||
| "GH_REPO": "specify the GitHub repository in the '[HOST/]OWNER/REPO' format for commands\nthat otherwise operate on a local repository.", | ||
| "GH_HOST": "specify the GitHub hostname for commands that would otherwise assume\nthe 'github.com' host when not in a context of an existing repository.", | ||
| "GH_EDITOR": "the editor tool to use for authoring text (1st precedence).", | ||
| "GIT_EDITOR": "the editor tool to use for authoring text (2nd precedence).", | ||
| "VISUAL": "the editor tool to use for authoring text (3rd precedence).", | ||
| "EDITOR": "the editor tool to use for authoring text (4th precedence).", | ||
| "BROWSER": "the web browser to use for opening links.", | ||
| "DEBUG": "set to any value to enable verbose output to standard error. Include values 'api'\nor 'oauth' to print detailed information about HTTP requests or authentication flow.", | ||
| "GLAMOR_STYLE": "the style to use for rendering Markdown. See\nhttps://github.com/charmbracelet/glamour#styles", | ||
| "NO_COLOR": "avoid printing ANSI escape sequences for color output.", | ||
| } | ||
|
|
||
| orderedKeys := []string{ | ||
| "GITHUB_TOKEN", | ||
| "GITHUB_ENTERPRISE_TOKEN", | ||
| "GH_REPO", | ||
| "GH_HOST", | ||
| "GH_EDITOR", | ||
| "GIT_EDITOR", | ||
| "VISUAL", | ||
| "EDITOR", | ||
| "BROWSER", | ||
| "DEBUG", | ||
| "GLAMOR_STYLE", | ||
| "NO_COLOR", | ||
| } | ||
|
|
||
| for _, k := range orderedKeys { | ||
| if verbose { | ||
| fmt.Fprintf(f.IOStreams.Out, "%s - %s\n", k, envVars[k]) | ||
| } | ||
|
|
||
| fmt.Fprintf(f.IOStreams.Out, "%s: %s\n", k, os.Getenv(k)) | ||
|
|
||
| if verbose { | ||
| fmt.Fprintln(f.IOStreams.Out, "") | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Show environment variable descriptions") | ||
|
|
||
| cmdutil.DisableAuthCheck(cmd) | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package env | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/cli/cli/pkg/cmdutil" | ||
| "github.com/cli/cli/pkg/iostreams" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestNewCmdEnv(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| args []string | ||
| flags []string | ||
| wantsErr bool | ||
| }{ | ||
| { | ||
| name: "no args or flags", | ||
| args: []string{}, | ||
| flags: []string{}, | ||
| wantsErr: false, | ||
| }, | ||
| { | ||
| name: "verbose flag", | ||
| args: []string{}, | ||
| flags: []string{"--verbose"}, | ||
| wantsErr: false, | ||
| }, | ||
| { | ||
| name: "help flag", | ||
| args: []string{}, | ||
| flags: []string{"--help"}, | ||
| wantsErr: false, | ||
| }, | ||
| { | ||
| name: "invalid arg", | ||
| args: []string{"invalid"}, | ||
| flags: []string{}, | ||
| wantsErr: true, | ||
| }, | ||
| { | ||
| name: "invalid flag", | ||
| args: []string{}, | ||
| flags: []string{"--invalid"}, | ||
| wantsErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| io, _, stdout, stderr := iostreams.Test() | ||
|
|
||
| f := &cmdutil.Factory{ | ||
| IOStreams: io, | ||
| } | ||
|
|
||
| cmd := NewCmdEnv(f) | ||
| cmd.SetArgs(append(tt.args, tt.flags...)) | ||
| cmd.SetOut(stdout) | ||
| cmd.SetErr(stderr) | ||
|
|
||
| _, err := cmd.ExecuteC() | ||
| if tt.wantsErr { | ||
| assert.Error(t, err) | ||
| return | ||
| } | ||
| assert.NoError(t, err) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mislav @vilmibm Is there a reason why this is currently only using
LocalFlags()?