-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.go
112 lines (89 loc) · 2.83 KB
/
cli.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package chocolatey
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/blang/semver"
"github.com/google/go-github/v32/github"
gh "github.com/pulumi/pulumictl/pkg/github"
"github.com/pulumi/pulumictl/pkg/gitversion"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
githubToken string
org string
repo string
ref string
tokenClient *http.Client
app string
)
type Payload struct {
Ref string `json:"ref"`
}
const eventType = "choco-deploy"
func Command() *cobra.Command {
command := &cobra.Command{
Use: "choco-deploy [tag]",
Short: "Create a Chocolatey Deployment",
Long: `Send a repository dispatch payload to the pulumi-chocolatey repo that triggers the deployment of a chocolatey package`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// Grab all the configuration variables
githubToken = viper.GetString("token")
org = viper.GetString("org")
ref = viper.GetString("ref")
containerRepo := "pulumi/pulumi-chocolatey"
ref := args[0]
app = viper.GetString("app")
// perform some string manipulation and validation
containerRepoArray := strings.Split(containerRepo, "/")
// if the string split doesn't return 2 values, it's probably not right
if len(containerRepoArray) != 2 {
return fmt.Errorf("unable to use container repo: format must be <org>/<repo> - value: %s\n", containerRepo)
}
_, err := semver.Parse(gitversion.StripModuleTagPrefixes(ref))
if err != nil {
return fmt.Errorf("must specify a valid semver ref - value: %s\n", ref)
}
// create a github client and token
ctx, client := gh.CreateGithubClient(githubToken)
// create the JSON payload
jsonPayload, err := json.Marshal(Payload{
Ref: ref,
})
if err != nil {
return err
}
payload := json.RawMessage(jsonPayload)
var eventTriggerType string
if app != "" {
eventTriggerType = fmt.Sprintf("choco-deploy-%s", app)
} else {
eventTriggerType = eventType
}
// create the repository dispatch event
_, _, err = client.Repositories.Dispatch(ctx,
containerRepoArray[0],
containerRepoArray[1],
github.DispatchRequestOptions{
EventType: eventTriggerType,
ClientPayload: &payload,
})
if err != nil {
return fmt.Errorf("unable to create dispatch event: %w\n", err)
}
// output stuff
fmt.Println("Submitting dispatch event to:", containerRepo)
fmt.Println(string(payload))
return nil
},
}
command.Flags().StringP("org", "o", "pulumi", "the GitHub org that hosts the provider in the arg")
command.Flags().StringP("app", "a", "", "The name of the chocolatey application to deploy")
viper.BindEnv("org", "GITHUB_ORG")
viper.BindPFlag("org", command.Flags().Lookup("org"))
viper.BindPFlag("app", command.Flags().Lookup("app"))
return command
}