Skip to content

Commit

Permalink
Switch to cobra/viper instead of cli/v2. (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigkevmcd authored Apr 29, 2020
1 parent 2a2169b commit 3ecb894
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 169 deletions.
166 changes: 2 additions & 164 deletions cmd/services/main.go
Original file line number Diff line number Diff line change
@@ -1,171 +1,9 @@
package main

import (
"errors"
"fmt"
"log"
"os"

"github.com/mitchellh/go-homedir"
"github.com/tcnksm/go-gitconfig"
"github.com/urfave/cli/v2"

"github.com/rhd-gitops-example/services/pkg/avancement"
"github.com/rhd-gitops-example/services/pkg/git"
)

const (
githubTokenFlag = "github-token"
branchNameFlag = "branch-name"
fromFlag = "from"
toFlag = "to"
serviceFlag = "service"
cacheDirFlag = "cache-dir"
msgFlag = "commit-message"
nameFlag = "commit-name"
emailFlag = "commit-email"
debugFlag = "debug"
keepCacheFlag = "keep-cache"
)

var (
globalFlags = []cli.Flag{
&cli.StringFlag{
Name: githubTokenFlag,
Usage: "oauth access token to authenticate the request",
EnvVars: []string{"GITHUB_TOKEN"},
Required: true,
},
}

promoteFlags = []cli.Flag{
&cli.StringFlag{
Name: fromFlag,
Usage: "source Git repository",
Required: true,
},
&cli.StringFlag{
Name: toFlag,
Usage: "destination Git repository",
Required: true,
},
&cli.StringFlag{
Name: serviceFlag,
Usage: "service name to promote",
Required: true,
},
&cli.StringFlag{
Name: branchNameFlag,
Usage: "the name to use for the newly created branch",
Value: "",
},
&cli.StringFlag{
Name: cacheDirFlag,
Value: "~/.promotion/cache",
Usage: "where to cache Git checkouts",
Required: false,
},

&cli.StringFlag{
Name: nameFlag,
Usage: "the name to use for commits when creating branches",
Required: false,
EnvVars: []string{"COMMIT_NAME"},
},
&cli.StringFlag{
Name: emailFlag,
Usage: "the email to use for commits when creating branches",
Required: false,
EnvVars: []string{"COMMIT_EMAIL"},
},
&cli.StringFlag{
Name: msgFlag,
Usage: "the msg to use on the resultant commit and pull request",
Required: false,
EnvVars: []string{"COMMIT_MSG"},
},
&cli.BoolFlag{
Name: debugFlag,
Usage: "additional debug logging output",
EnvVars: []string{"DEBUG_SERVICES"},
Value: false,
Required: false,
},
&cli.BoolFlag{
Name: keepCacheFlag,
Usage: "whether to retain the locally cloned repositories in the cache directory",
Value: false,
Required: false,
},
}
"github.com/rhd-gitops-example/services/pkg/cmd"
)

func main() {
app := &cli.App{
Name: "services",
Description: "manage services lifecycle via GitOps",
Commands: []*cli.Command{
{
Name: "promote",
Usage: "promote from one environment to another",
Flags: promoteFlags,
Action: promoteAction,
},
},
Flags: globalFlags,
}

err := app.Run(os.Args)
if err != nil {
log.Fatalf("%v", err)
}
}

func promoteAction(c *cli.Context) error {
fromRepo := c.String(fromFlag)
toRepo := c.String(toFlag)
service := c.String(serviceFlag)
newBranchName := c.String(branchNameFlag)
debug := c.Bool(debugFlag)
keepCache := c.Bool(keepCacheFlag)
msg := c.String(msgFlag)

cacheDir, err := homedir.Expand(c.String(cacheDirFlag))
if err != nil {
return fmt.Errorf("failed to expand cacheDir path: %w", err)
}

author, err := newAuthor(c)
if err != nil {
return fmt.Errorf("unable to establish credentials: %w", err)
}
return avancement.New(cacheDir, author, avancement.WithDebug(debug)).Promote(service, fromRepo, toRepo, newBranchName, msg, keepCache)
}

func newAuthor(c *cli.Context) (*git.Author, error) {
name := c.String(nameFlag)
email := c.String(emailFlag)
token := c.String(githubTokenFlag)

var err error
if name == "" {
name, err = gitconfig.Username()
if err != nil {
return nil, err
}
}

if email == "" {
email, err = gitconfig.Email()
if err != nil {
return nil, err
}
}

// TODO: make this a multierror with both errors?
if name == "" || email == "" {
return nil, errors.New("unable to identify user and email for commits")
}

return &git.Author{Name: name, Email: email, Token: token}, nil
cmd.Execute()
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ require (
github.com/jenkins-x/go-scm v1.5.77
github.com/mitchellh/go-homedir v1.1.0
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.6.3
github.com/tcnksm/go-gitconfig v0.1.2
github.com/urfave/cli/v2 v2.1.1
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e // indirect
Expand Down
Loading

0 comments on commit 3ecb894

Please sign in to comment.