|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | 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" |
101 | 5 | ) |
102 | 6 |
|
103 | 7 | 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() |
171 | 9 | } |
0 commit comments