-
Notifications
You must be signed in to change notification settings - Fork 7
/
release.go
73 lines (66 loc) · 2.58 KB
/
release.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package command
import (
"fmt"
"net/http"
"github.com/lunarway/release-manager/cmd/hamctl/command/completion"
"github.com/lunarway/release-manager/internal/git"
httpinternal "github.com/lunarway/release-manager/internal/http"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func NewRelease(client *httpinternal.Client, service *string) *cobra.Command {
var environment, branch, artifact string
var command = &cobra.Command{
Use: "release",
Short: `Release a specific artifact or latest artifact from a branch into a specific environment.`,
Example: `Release artifact 'master-482c9d808e-3bf40478e5' from service 'product' into environment 'dev':
hamctl release --service product --env dev --artifact master-482c9d808e-3bf40478e5
Release latest artifact from branch 'master' of service 'product' into environment 'dev':
hamctl release --service product --env dev --branch master`,
RunE: func(c *cobra.Command, args []string) error {
if branch != "" && artifact != "" {
return errors.New("--branch and --artifact cannot both be specificed")
}
if branch == "" && artifact == "" {
return errors.New("--branch or --artifact is required")
}
committerName, committerEmail, err := git.CommitterDetails()
if err != nil {
return err
}
var resp httpinternal.ReleaseResponse
path, err := client.URL("release")
if err != nil {
return err
}
err = client.Do(http.MethodPost, path, httpinternal.ReleaseRequest{
Service: *service,
Environment: environment,
Branch: branch,
ArtifactID: artifact,
CommitterName: committerName,
CommitterEmail: committerEmail,
}, &resp)
if err != nil {
return err
}
fmt.Printf("Release of service: %s\n", *service)
if resp.Status != "" {
fmt.Printf("%s\n", resp.Status)
} else {
fmt.Printf("[✓] Release of %s to %s initialized\n", resp.Tag, resp.ToEnvironment)
}
return nil
},
}
command.Flags().StringVarP(&environment, "env", "e", "", "environment to release to (required)")
// errors are skipped here as the only case they can occour are if thee flag
// does not exist on the command.
//nolint:errcheck
command.MarkFlagRequired("env")
completion.FlagAnnotation(command, "env", "__hamctl_get_environments")
command.Flags().StringVarP(&branch, "branch", "b", "", "release latest artifact from this branch (mutually exclusive with --artifact)")
completion.FlagAnnotation(command, "branch", "__hamctl_get_branches")
command.Flags().StringVar(&artifact, "artifact", "", "release this artifact id (mutually exclusive with --branch)")
return command
}