Skip to content

Commit

Permalink
Added “git config unset” command
Browse files Browse the repository at this point in the history
  • Loading branch information
rykov committed Oct 7, 2021
1 parent f3f7d66 commit b2c5517
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 21 deletions.
8 changes: 5 additions & 3 deletions api/git_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ func (c *Client) GitConfig(cc context.Context, repo string) ([]GitConfigPair, er

out := make([]GitConfigPair, 0, len(resp.ConfigVars))
for k, v := range resp.ConfigVars {
out = append(out, GitConfigPair{k, v})
if v != nil { // Should not happen
out = append(out, GitConfigPair{k, *v})
}
}

return out, nil
}

// Git Config request/response
type gitConfigJSON struct {
ConfigVars map[string]string `json:"config_vars"`
ConfigVars map[string]*string `json:"config_vars"`
}

// Repo represents Git Config KV pair
Expand All @@ -35,7 +37,7 @@ type GitConfigPair struct {
}

// GitConfigSet updates Git Config with passed-in map of new variables
func (c *Client) GitConfigSet(cc context.Context, repo string, vars map[string]string) error {
func (c *Client) GitConfigSet(cc context.Context, repo string, vars map[string]*string) error {
path := "/git/repos/{acct}/" + url.PathEscape(repo) + "/config-vars"
req := c.newRequest(cc, "PATCH", path, false)
c.prepareJSONBody(req, &gitConfigJSON{vars})
Expand Down
63 changes: 45 additions & 18 deletions cli/git_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// NewCmdGitConfig is the root for Git Config
func NewCmdGitConfig() *cobra.Command {
gitConfigCmd := &cobra.Command{
Use: "config",
Use: "config REPO",
Short: "Configure Git build",
RunE: func(cmd *cobra.Command, args []string) error {
return filteredGitConfig(cmd, args, false)
Expand All @@ -23,14 +23,15 @@ func NewCmdGitConfig() *cobra.Command {

gitConfigCmd.AddCommand(NewCmdGitConfigSet())
gitConfigCmd.AddCommand(NewCmdGitConfigGet())
gitConfigCmd.AddCommand(NewCmdGitConfigUnset())

return gitConfigCmd
}

// NewCmdGitConfigGet updates one or more configuration keys
func NewCmdGitConfigGet() *cobra.Command {
gitConfigGetCmd := &cobra.Command{
Use: "get KEY",
Use: "get REPO KEY",
Short: "Get Git build environment key",
RunE: func(cmd *cobra.Command, args []string) error {
return filteredGitConfig(cmd, args, true)
Expand Down Expand Up @@ -93,38 +94,64 @@ func filteredGitConfig(cmd *cobra.Command, args []string, filter bool) error {
// NewCmdGitConfigSet updates one or more configuration keys
func NewCmdGitConfigSet() *cobra.Command {
gitConfigSetCmd := &cobra.Command{
Use: "set KEY=VAL",
Use: "set REPO KEY=VAL",
Short: "Set Git build environment key",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("Please specify a repository and a KEY=VALUE")
}

cc := cmd.Context()
term := ctx.Terminal(cc)
c, err := newAPIClient(cc)
if err != nil {
return err
}

vars := map[string]string{}
vars := map[string]*string{}
for _, pairStr := range args[1:] {
pair := strings.SplitN(pairStr, "=", 2)
if len(pair) != 2 {
return fmt.Errorf("Argument has no value: %s", pairStr)
}
vars[pair[0]] = pair[1]
vars[pair[0]] = &pair[1]
}

return gitConfigUpdate(cmd, args[0], vars)
},
}

return gitConfigSetCmd
}

// NewCmdGitConfigSet updates one or more configuration keys
func NewCmdGitConfigUnset() *cobra.Command {
gitConfigUnsetCmd := &cobra.Command{
Use: "unset REPO KEY",
Short: "Remove Git build environment key",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("Please specify a repository and a KEY")
}

err = c.GitConfigSet(cc, args[0], vars)
if err != nil {
return err
vars := map[string]*string{}
for _, key := range args[1:] {
vars[key] = nil
}

term.Printf("Updated %s repository config\n", args[0])
return nil
return gitConfigUpdate(cmd, args[0], vars)
},
}

return gitConfigSetCmd
return gitConfigUnsetCmd
}

func gitConfigUpdate(cmd *cobra.Command, repo string, vars map[string]*string) error {
cc := cmd.Context()
term := ctx.Terminal(cc)
c, err := newAPIClient(cc)
if err != nil {
return err
}

err = c.GitConfigSet(cc, repo, vars)
if err != nil {
return err
}

term.Printf("Updated %s repository config\n", repo)
return nil
}
40 changes: 40 additions & 0 deletions cli/git_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,43 @@ func TestGitConfigSetCommandForbidden(t *testing.T) {
testCommandForbiddenResponse(t, []string{"git", "config", "set", "repo-name", "KEY2=VALUE2"}, server)
server.Close()
}

// ==== GIT CONFIG UNSET ====

func TestGitConfigUnsetCommandSuccess(t *testing.T) {
auth := terminal.TestAuther("user", "abc123", nil)
term := terminal.NewForTest()

// Fire up test server
path := "/git/repos/me/repo-name/config-vars"
server := testutil.APIServer(t, "PATCH", path, gitConfigResponse, 200)
defer server.Close()

cc := cli.TestContext(term, auth)
flags := ctx.GlobalFlags(cc)
flags.Endpoint = server.URL

err := runCommandNoErr(cc, []string{"git", "config", "unset", "repo-name", "KEY2"})
if err != nil {
t.Fatal(err)
}

exp := "Updated repo-name repository config"
if outStr := compactString(term.OutBytes()); outStr != exp {
t.Errorf("Expected output to include %q, got %q", exp, outStr)
}
}

func TestGitConfigUnsetCommandUnauthorized(t *testing.T) {
path := "/git/repos/me/repo-name/config-vars"
server := testutil.APIServer(t, "PATCH", path, "{}", 200)
testCommandLoginPreCheck(t, []string{"git", "config", "unset", "repo-name", "KEY2"}, server)
server.Close()
}

func TestGitConfigUnsetCommandForbidden(t *testing.T) {
path := "/git/repos/me/repo-name/config-vars"
server := testutil.APIServer(t, "PATCH", path, "", 403)
testCommandForbiddenResponse(t, []string{"git", "config", "unset", "repo-name", "KEY2"}, server)
server.Close()
}

0 comments on commit b2c5517

Please sign in to comment.