Skip to content

Commit 3ecb894

Browse files
authored
Switch to cobra/viper instead of cli/v2. (#64)
1 parent 2a2169b commit 3ecb894

File tree

5 files changed

+353
-169
lines changed

5 files changed

+353
-169
lines changed

cmd/services/main.go

Lines changed: 2 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,9 @@
11
package main
22

33
import (
4-
"errors"
5-
"fmt"
6-
"log"
7-
"os"
8-
9-
"github.com/mitchellh/go-homedir"
10-
"github.com/tcnksm/go-gitconfig"
11-
"github.com/urfave/cli/v2"
12-
13-
"github.com/rhd-gitops-example/services/pkg/avancement"
14-
"github.com/rhd-gitops-example/services/pkg/git"
15-
)
16-
17-
const (
18-
githubTokenFlag = "github-token"
19-
branchNameFlag = "branch-name"
20-
fromFlag = "from"
21-
toFlag = "to"
22-
serviceFlag = "service"
23-
cacheDirFlag = "cache-dir"
24-
msgFlag = "commit-message"
25-
nameFlag = "commit-name"
26-
emailFlag = "commit-email"
27-
debugFlag = "debug"
28-
keepCacheFlag = "keep-cache"
29-
)
30-
31-
var (
32-
globalFlags = []cli.Flag{
33-
&cli.StringFlag{
34-
Name: githubTokenFlag,
35-
Usage: "oauth access token to authenticate the request",
36-
EnvVars: []string{"GITHUB_TOKEN"},
37-
Required: true,
38-
},
39-
}
40-
41-
promoteFlags = []cli.Flag{
42-
&cli.StringFlag{
43-
Name: fromFlag,
44-
Usage: "source Git repository",
45-
Required: true,
46-
},
47-
&cli.StringFlag{
48-
Name: toFlag,
49-
Usage: "destination Git repository",
50-
Required: true,
51-
},
52-
&cli.StringFlag{
53-
Name: serviceFlag,
54-
Usage: "service name to promote",
55-
Required: true,
56-
},
57-
&cli.StringFlag{
58-
Name: branchNameFlag,
59-
Usage: "the name to use for the newly created branch",
60-
Value: "",
61-
},
62-
&cli.StringFlag{
63-
Name: cacheDirFlag,
64-
Value: "~/.promotion/cache",
65-
Usage: "where to cache Git checkouts",
66-
Required: false,
67-
},
68-
69-
&cli.StringFlag{
70-
Name: nameFlag,
71-
Usage: "the name to use for commits when creating branches",
72-
Required: false,
73-
EnvVars: []string{"COMMIT_NAME"},
74-
},
75-
&cli.StringFlag{
76-
Name: emailFlag,
77-
Usage: "the email to use for commits when creating branches",
78-
Required: false,
79-
EnvVars: []string{"COMMIT_EMAIL"},
80-
},
81-
&cli.StringFlag{
82-
Name: msgFlag,
83-
Usage: "the msg to use on the resultant commit and pull request",
84-
Required: false,
85-
EnvVars: []string{"COMMIT_MSG"},
86-
},
87-
&cli.BoolFlag{
88-
Name: debugFlag,
89-
Usage: "additional debug logging output",
90-
EnvVars: []string{"DEBUG_SERVICES"},
91-
Value: false,
92-
Required: false,
93-
},
94-
&cli.BoolFlag{
95-
Name: keepCacheFlag,
96-
Usage: "whether to retain the locally cloned repositories in the cache directory",
97-
Value: false,
98-
Required: false,
99-
},
100-
}
4+
"github.com/rhd-gitops-example/services/pkg/cmd"
1015
)
1026

1037
func main() {
104-
app := &cli.App{
105-
Name: "services",
106-
Description: "manage services lifecycle via GitOps",
107-
Commands: []*cli.Command{
108-
{
109-
Name: "promote",
110-
Usage: "promote from one environment to another",
111-
Flags: promoteFlags,
112-
Action: promoteAction,
113-
},
114-
},
115-
Flags: globalFlags,
116-
}
117-
118-
err := app.Run(os.Args)
119-
if err != nil {
120-
log.Fatalf("%v", err)
121-
}
122-
}
123-
124-
func promoteAction(c *cli.Context) error {
125-
fromRepo := c.String(fromFlag)
126-
toRepo := c.String(toFlag)
127-
service := c.String(serviceFlag)
128-
newBranchName := c.String(branchNameFlag)
129-
debug := c.Bool(debugFlag)
130-
keepCache := c.Bool(keepCacheFlag)
131-
msg := c.String(msgFlag)
132-
133-
cacheDir, err := homedir.Expand(c.String(cacheDirFlag))
134-
if err != nil {
135-
return fmt.Errorf("failed to expand cacheDir path: %w", err)
136-
}
137-
138-
author, err := newAuthor(c)
139-
if err != nil {
140-
return fmt.Errorf("unable to establish credentials: %w", err)
141-
}
142-
return avancement.New(cacheDir, author, avancement.WithDebug(debug)).Promote(service, fromRepo, toRepo, newBranchName, msg, keepCache)
143-
}
144-
145-
func newAuthor(c *cli.Context) (*git.Author, error) {
146-
name := c.String(nameFlag)
147-
email := c.String(emailFlag)
148-
token := c.String(githubTokenFlag)
149-
150-
var err error
151-
if name == "" {
152-
name, err = gitconfig.Username()
153-
if err != nil {
154-
return nil, err
155-
}
156-
}
157-
158-
if email == "" {
159-
email, err = gitconfig.Email()
160-
if err != nil {
161-
return nil, err
162-
}
163-
}
164-
165-
// TODO: make this a multierror with both errors?
166-
if name == "" || email == "" {
167-
return nil, errors.New("unable to identify user and email for commits")
168-
}
169-
170-
return &git.Author{Name: name, Email: email, Token: token}, nil
8+
cmd.Execute()
1719
}

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ require (
99
github.com/jenkins-x/go-scm v1.5.77
1010
github.com/mitchellh/go-homedir v1.1.0
1111
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect
12+
github.com/spf13/cobra v1.0.0
13+
github.com/spf13/pflag v1.0.3
14+
github.com/spf13/viper v1.6.3
1215
github.com/tcnksm/go-gitconfig v0.1.2
13-
github.com/urfave/cli/v2 v2.1.1
1416
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
1517
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
1618
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e // indirect

0 commit comments

Comments
 (0)