Skip to content
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

Add a gh variable get FOO command #9106

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
132 changes: 132 additions & 0 deletions pkg/cmd/variable/get/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package get

import (
"errors"
"fmt"
"net/http"

"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/gh"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/variable/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/spf13/cobra"
)

type GetOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
Config func() (gh.Config, error)
BaseRepo func() (ghrepo.Interface, error)

VariableName string
OrgName string
EnvName string
}

type getVariableResponse struct {
Value string `json:"value"`
// Other available but unused fields
// Name string `json:"name"`
// UpdatedAt time.Time `json:"updated_at"`
// Visibility shared.Visibility `json:"visibility"`
// SelectedReposURL string `json:"selected_repositories_url"`
// NumSelectedRepos int `json:"num_selected_repos"`
}

func NewCmdGet(f *cmdutil.Factory, runF func(*GetOptions) error) *cobra.Command {
opts := &GetOptions{
IO: f.IOStreams,
Config: f.Config,
HttpClient: f.HttpClient,
}

cmd := &cobra.Command{
Use: "get <variable-name>",
Short: "Get variables",
Long: heredoc.Doc(`
Get a variable on one of the following levels:
- repository (default): available to GitHub Actions runs or Dependabot in a repository
- environment: available to GitHub Actions runs for a deployment environment in a repository
- organization: available to GitHub Actions runs or Dependabot within an organization
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo

if err := cmdutil.MutuallyExclusive("specify only one of `--org` or `--env`", opts.OrgName != "", opts.EnvName != ""); err != nil {
return err
}

opts.VariableName = args[0]

if runF != nil {
return runF(opts)
}

return getRun(opts)
},
}
cmd.Flags().StringVarP(&opts.OrgName, "org", "o", "", "Get a variable for an organization")
cmd.Flags().StringVarP(&opts.EnvName, "env", "e", "", "Get a variable for an environment")

return cmd
}

func getRun(opts *GetOptions) error {
c, err := opts.HttpClient()
if err != nil {
return fmt.Errorf("could not create http client: %w", err)
}
client := api.NewClientFromHTTP(c)

orgName := opts.OrgName
envName := opts.EnvName

variableEntity, err := shared.GetVariableEntity(orgName, envName)
if err != nil {
return err
}

var baseRepo ghrepo.Interface
if variableEntity == shared.Repository || variableEntity == shared.Environment {
baseRepo, err = opts.BaseRepo()
if err != nil {
return err
}
}

var path string
switch variableEntity {
case shared.Organization:
path = fmt.Sprintf("orgs/%s/actions/variables/%s", orgName, opts.VariableName)
case shared.Environment:
path = fmt.Sprintf("repos/%s/environments/%s/variables/%s", ghrepo.FullName(baseRepo), envName, opts.VariableName)
case shared.Repository:
path = fmt.Sprintf("repos/%s/actions/variables/%s", ghrepo.FullName(baseRepo), opts.VariableName)
}

cfg, err := opts.Config()
if err != nil {
return err
}

host, _ := cfg.Authentication().DefaultHost()

var response getVariableResponse
if err = client.REST(host, "GET", path, nil, &response); err != nil {
var httpErr api.HTTPError
if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusNotFound {
return fmt.Errorf("variable %s was not found", opts.VariableName)
}

return fmt.Errorf("failed to get variable %s: %w", opts.VariableName, err)
}

fmt.Fprintf(opts.IO.Out, "%s\n", response.Value)
Copy link
Contributor Author

@arnested arnested May 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@williammartin I think adding the newline could be a problem in scripting.

When doing stuff like

FOO=$(gh variable get MY_VAR)

Now the trailing newline would be part of the variable. Of course, I could trim the trailing newline, but it's inconvenient and not obvious.

It's nice when the variable is displayed in the terminal (but if the variable actually had a newline already, you would get an extra).

A solution for increased readability in the terminal could be to add a newline but add it to stderr, if and only if the variable already lacks a trailing newline and both stdout and stderr are to the terminal.

fmt.Fprintf(opts.IO.Out, "%s", response.Value)

if !strings.HasSuffix(response.Value, "\n") && opts.IO.IsStdoutTTY() && opts.IO.IsStderrTTY() {
	fmt.Fprint(opts.IO.ErrOut, "\n")
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well...this sent me on a bit of a journey. Firstly, I appreciate your comment and sorry that I didn't recognise there was more to discuss here.

Command Substitution

I don't believe that your first example is an issue because command substitution ($()) removes trailing line feed characters:

export FOO="---\n\n\n"echo $FOO
---



➜ echo "$(echo "$FOO")"
---

Thus, our addition of a \n in the fmt.Printf should have no impact on FOO=$(gh variable get MY_VAR). This is consistent with other gh commands when not hooked up to a TTY but printing info e.g. repo create showing the newly created repo URL.

This also makes sense from a UX point of view as well because in CI or scripts we wouldn't want users to have to add newlines everywhere in order to avoid text appearing concatenated together with no spacing.

However....

It did get me wondering about your other comment relating to variables ending with newlines. If command substitution strips trailing Line Feed chars then that will be problematic for variables that have trailing new lines:

~/workspace/cli/bin/gh variable set Triage9106 --body "foobar

"
foobar


✓ Updated variable Triage9106 for cli/cli
➜  ~/workspace/cli/bin/gh variable get Triage9106
foobar


➜ echo "$(~/workspace/cli/bin/gh variable get Triage9106)"
foobar

So this leaves us in an interesting situation because for variables ending in newlines, command substitution is a bit of a nightmare. There does appear to be some workarounds however in that case it's probably not possible for a user to distinguish between their own newlines and the one added by the CLI.

However, I'm inclined to say that in this particular case, we document it and place the responsibility on the user to strip our trailing newline before processing the string because the alternative and much more common case seems quite annoying (strings being concatenated together and it working differently from other commands).

Finally

I'm inclined to say there is a bug in both secret set and variable set when reading the variable value from stdin. It looks like in #5086 we decided to trim carriage return and line feed chars from the end because the shell adds one but the implementation actually strips all trailing CR/LF chars:

echo $FOO | ~/workspace/cli/bin/gh variable set Triage9106
---
✓ Updated variable Triage9106 for cli/cli
➜ ~/workspace/cli/bin/gh variable get Triage9106
---

Let me know your thoughts, cheers!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. I didn't know command substitution removes trailing newlines.

It might just have been me being too careful about not loosing my scripting capabilities.

So adding the newline seems perfectly inline with what would normally be expected. I'm not sure, removing the newlines in command substitution was the world's best idea (looking at the workarounds, among others) but that should probably have been handled 45 years ago by someone not me or you 😄


return nil
}
202 changes: 202 additions & 0 deletions pkg/cmd/variable/get/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package get

import (
"bytes"
"fmt"
"net/http"
"testing"

"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/gh"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewCmdGet(t *testing.T) {
tests := []struct {
name string
cli string
wants GetOptions
wantErr error
}{
{
name: "repo",
cli: "FOO",
wants: GetOptions{
OrgName: "",
VariableName: "FOO",
},
},
{
name: "org",
cli: "-o TestOrg BAR",
wants: GetOptions{
OrgName: "TestOrg",
VariableName: "BAR",
},
},
{
name: "env",
cli: "-e Development BAZ",
wants: GetOptions{
EnvName: "Development",
VariableName: "BAZ",
},
},
{
name: "org and env",
cli: "-o TestOrg -e Development QUX",
wantErr: cmdutil.FlagErrorf("%s", "specify only one of `--org` or `--env`"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ios, _, _, _ := iostreams.Test()
f := &cmdutil.Factory{
IOStreams: ios,
}

argv, err := shlex.Split(tt.cli)
assert.NoError(t, err)

var gotOpts *GetOptions
cmd := NewCmdGet(f, func(opts *GetOptions) error {
gotOpts = opts
return nil
})
cmd.SetArgs(argv)
cmd.SetIn(&bytes.Buffer{})
cmd.SetOut(&bytes.Buffer{})
cmd.SetErr(&bytes.Buffer{})

_, err = cmd.ExecuteC()
if tt.wantErr != nil {
require.Equal(t, err, tt.wantErr)
return
}
require.NoError(t, err)

require.Equal(t, tt.wants.OrgName, gotOpts.OrgName)
require.Equal(t, tt.wants.EnvName, gotOpts.EnvName)
require.Equal(t, tt.wants.VariableName, gotOpts.VariableName)
})
}
}

func Test_getRun(t *testing.T) {
tests := []struct {
name string
opts *GetOptions
httpStubs func(*httpmock.Registry)
wantOut string
wantErr error
}{
{
name: "getting repo variable",
opts: &GetOptions{
VariableName: "VARIABLE_ONE",
},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("GET", "repos/owner/repo/actions/variables/VARIABLE_ONE"),
httpmock.JSONResponse(getVariableResponse{
Value: "repo_var",
}))
},
wantOut: "repo_var\n",
},
{
name: "getting org variable",
opts: &GetOptions{
OrgName: "TestOrg",
VariableName: "VARIABLE_ONE",
},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("GET", "orgs/TestOrg/actions/variables/VARIABLE_ONE"),
httpmock.JSONResponse(getVariableResponse{
Value: "org_var",
}))
},
wantOut: "org_var\n",
},
{
name: "getting env variable",
opts: &GetOptions{
EnvName: "Development",
VariableName: "VARIABLE_ONE",
},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("GET", "repos/owner/repo/environments/Development/variables/VARIABLE_ONE"),
httpmock.JSONResponse(getVariableResponse{
Value: "env_var",
}))
},
wantOut: "env_var\n",
},
{
name: "when the variable is not found, an error is returned",
opts: &GetOptions{
VariableName: "VARIABLE_ONE",
},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("GET", "repos/owner/repo/actions/variables/VARIABLE_ONE"),
httpmock.StatusStringResponse(404, "not found"),
)
},
wantErr: fmt.Errorf("variable VARIABLE_ONE was not found"),
},
{
name: "when getting any variable from API fails, the error is bubbled with context",
opts: &GetOptions{
VariableName: "VARIABLE_ONE",
},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("GET", "repos/owner/repo/actions/variables/VARIABLE_ONE"),
httpmock.StatusStringResponse(400, "not found"),
)
},
wantErr: fmt.Errorf("failed to get variable VARIABLE_ONE: HTTP 400 (https://api.github.com/repos/owner/repo/actions/variables/VARIABLE_ONE)"),
},
}

for _, tt := range tests {
var runTest = func(tty bool) func(t *testing.T) {
return func(t *testing.T) {
reg := &httpmock.Registry{}
tt.httpStubs(reg)
defer reg.Verify(t)

ios, _, stdout, _ := iostreams.Test()
ios.SetStdoutTTY(tty)

tt.opts.IO = ios
tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
return ghrepo.FromFullName("owner/repo")
}
tt.opts.HttpClient = func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
}
tt.opts.Config = func() (gh.Config, error) {
return config.NewBlankConfig(), nil
}

err := getRun(tt.opts)
if err != nil {
require.EqualError(t, tt.wantErr, err.Error())
return
}

require.NoError(t, err)
require.Equal(t, tt.wantOut, stdout.String())
}
}

t.Run(tt.name+" tty", runTest(true))
t.Run(tt.name+" no-tty", runTest(false))
}
}
2 changes: 2 additions & 0 deletions pkg/cmd/variable/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package variable
import (
"github.com/MakeNowJust/heredoc"
cmdDelete "github.com/cli/cli/v2/pkg/cmd/variable/delete"
cmdGet "github.com/cli/cli/v2/pkg/cmd/variable/get"
cmdList "github.com/cli/cli/v2/pkg/cmd/variable/list"
cmdSet "github.com/cli/cli/v2/pkg/cmd/variable/set"
"github.com/cli/cli/v2/pkg/cmdutil"
Expand All @@ -21,6 +22,7 @@ func NewCmdVariable(f *cmdutil.Factory) *cobra.Command {

cmdutil.EnableRepoOverride(cmd, f)

cmd.AddCommand(cmdGet.NewCmdGet(f, nil))
cmd.AddCommand(cmdSet.NewCmdSet(f, nil))
cmd.AddCommand(cmdList.NewCmdList(f, nil))
cmd.AddCommand(cmdDelete.NewCmdDelete(f, nil))
Expand Down