Skip to content

Commit 1ce7c73

Browse files
authored
Merge pull request #18 from mdb/reorg
code reorganization
2 parents d1640c0 + 0ee4ce4 commit 1ce7c73

12 files changed

+135
-140
lines changed

.goreleaser.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ builds:
1010
- linux
1111
- windows
1212
- netbsd
13+
main: ./cmd/main.go
1314
archives:
1415
- name_template: "{{ .Os }}-{{ .Arch }}"
1516
format: binary

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
SOURCE=./...
22
GOFMT_FILES?=$$(find . -type f -name '*.go')
3-
VERSION?=0.0.2
3+
VERSION?=0.0.3
44

55
default: build
66

cmd/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"github.com/MakeNowJust/heredoc"
7+
"github.com/mdb/gh-dispatch/dispatch"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// version's value is passed in at build time.
12+
var version string
13+
14+
func main() {
15+
rootCmd := &cobra.Command{
16+
Use: "gh dispatch",
17+
Short: "Send a GitHub dispatch event and watch the resulting GitHub Actions run",
18+
Long: heredoc.Doc(`
19+
Send a workflow_dispatch or repository_dispatch event and watch the resulting
20+
GitHub Actions run.
21+
`),
22+
SilenceUsage: true,
23+
Version: version,
24+
}
25+
26+
// TODO: how to make this required?
27+
rootCmd.PersistentFlags().StringP("repo", "R", "", "The targeted repository's full name (in 'owner/repo' format)")
28+
29+
repositoryCmd := dispatch.NewCmdRepository()
30+
rootCmd.AddCommand(repositoryCmd)
31+
32+
workflowCmd := dispatch.NewCmdWorkflow()
33+
rootCmd.AddCommand(workflowCmd)
34+
35+
if err := rootCmd.Execute(); err != nil {
36+
os.Exit(1)
37+
}
38+
}
File renamed without changes.

cmd/root.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

cmd/fixtures_test.go renamed to dispatch/fixtures_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package dispatch
22

33
var (
44
getWorkflowsResponse string = `{

cmd/repository.go renamed to dispatch/repository.go

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package dispatch
22

33
import (
44
"bytes"
@@ -26,23 +26,23 @@ type repositoryDispatchOptions struct {
2626
dispatchOptions
2727
}
2828

29-
var (
30-
repositoryEventType string
31-
repositoryClientPayload string
32-
repositoryWorkflow string
33-
)
29+
func NewCmdRepository() *cobra.Command {
30+
var (
31+
repositoryEventType string
32+
repositoryClientPayload string
33+
repositoryWorkflow string
34+
)
3435

35-
// repositoryCmd represents the repository subcommand
36-
var repositoryCmd = &cobra.Command{
37-
Use: heredoc.Doc(`
36+
cmd := &cobra.Command{
37+
Use: heredoc.Doc(`
3838
repository \
3939
--repo [owner/repo] \
4040
--event-type [event-type] \
4141
--client-payload [json-string] \
4242
--workflow [workflow-name]
4343
`),
44-
Short: "Send a repository dispatch event and watch the resulting GitHub Actions run",
45-
Long: heredoc.Doc(`
44+
Short: "Send a repository dispatch event and watch the resulting GitHub Actions run",
45+
Long: heredoc.Doc(`
4646
This command sends a repository dispatch event and attempts to find and watch the
4747
resulting GitHub Actions run whose name is specified as '--workflow'.
4848
@@ -51,35 +51,45 @@ var repositoryCmd = &cobra.Command{
5151
watch an unrelated GitHub Actions workflow run in the event that multiple runs of
5252
the specified workflow are running concurrently.
5353
`),
54-
Example: heredoc.Doc(`
54+
Example: heredoc.Doc(`
5555
gh dispatch repository \
5656
--repo mdb/gh-dispatch \
5757
--event-type 'hello' \
5858
--client-payload '{"name": "Mike"}' \
5959
--workflow Hello
6060
`),
61-
RunE: func(cmd *cobra.Command, args []string) error {
62-
repo, _ := cmd.Flags().GetString("repo")
63-
64-
b := []byte(repositoryClientPayload)
65-
var repoClientPayload interface{}
66-
json.Unmarshal(b, &repoClientPayload)
67-
68-
ios := iostreams.System()
61+
RunE: func(cmd *cobra.Command, args []string) error {
62+
repo, _ := cmd.Flags().GetString("repo")
63+
64+
b := []byte(repositoryClientPayload)
65+
var repoClientPayload interface{}
66+
json.Unmarshal(b, &repoClientPayload)
67+
68+
ios := iostreams.System()
69+
70+
dOptions := dispatchOptions{
71+
repo: repo,
72+
httpTransport: http.DefaultTransport,
73+
io: ios,
74+
}
75+
76+
return repositoryDispatchRun(&repositoryDispatchOptions{
77+
clientPayload: repoClientPayload,
78+
eventType: repositoryEventType,
79+
workflow: repositoryWorkflow,
80+
dispatchOptions: dOptions,
81+
})
82+
},
83+
}
6984

70-
dOptions := dispatchOptions{
71-
repo: repo,
72-
httpTransport: http.DefaultTransport,
73-
io: ios,
74-
}
85+
cmd.Flags().StringVarP(&repositoryEventType, "event-type", "e", "", "The repository dispatch event type.")
86+
cmd.MarkFlagRequired("event-type")
87+
cmd.Flags().StringVarP(&repositoryClientPayload, "client-payload", "p", "", "The repository dispatch event client payload JSON string.")
88+
cmd.MarkFlagRequired("client-payload")
89+
cmd.Flags().StringVarP(&repositoryWorkflow, "workflow", "w", "", "The resulting GitHub Actions workflow name.")
90+
cmd.MarkFlagRequired("workflow")
7591

76-
return repositoryDispatchRun(&repositoryDispatchOptions{
77-
clientPayload: repoClientPayload,
78-
eventType: repositoryEventType,
79-
workflow: repositoryWorkflow,
80-
dispatchOptions: dOptions,
81-
})
82-
},
92+
return cmd
8393
}
8494

8595
func repositoryDispatchRun(opts *repositoryDispatchOptions) error {
@@ -132,14 +142,3 @@ func repositoryDispatchRun(opts *repositoryDispatchOptions) error {
132142

133143
return render(opts.io, client, opts.repo, run)
134144
}
135-
136-
func init() {
137-
repositoryCmd.Flags().StringVarP(&repositoryEventType, "event-type", "e", "", "The repository dispatch event type.")
138-
repositoryCmd.MarkFlagRequired("event-type")
139-
repositoryCmd.Flags().StringVarP(&repositoryClientPayload, "client-payload", "p", "", "The repository dispatch event client payload JSON string.")
140-
repositoryCmd.MarkFlagRequired("client-payload")
141-
repositoryCmd.Flags().StringVarP(&repositoryWorkflow, "workflow", "w", "", "The resulting GitHub Actions workflow name.")
142-
repositoryCmd.MarkFlagRequired("workflow")
143-
144-
rootCmd.AddCommand(repositoryCmd)
145-
}

cmd/repository_test.go renamed to dispatch/repository_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package dispatch
22

33
import (
44
"fmt"

cmd/shared.go renamed to dispatch/shared.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package dispatch
22

33
import (
44
"bytes"

cmd/workflow.go renamed to dispatch/workflow.go

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package dispatch
22

33
import (
44
"bytes"
@@ -26,22 +26,22 @@ type workflowDispatchOptions struct {
2626
dispatchOptions
2727
}
2828

29-
var (
30-
workflowInputs string
31-
workflowName string
32-
workflowRef string
33-
)
29+
func NewCmdWorkflow() *cobra.Command {
30+
var (
31+
workflowInputs string
32+
workflowName string
33+
workflowRef string
34+
)
3435

35-
// workflowCmd represents the workflow subcommand
36-
var workflowCmd = &cobra.Command{
37-
Use: heredoc.Doc(`
36+
cmd := &cobra.Command{
37+
Use: heredoc.Doc(`
3838
workflow \
3939
--repo [owner/repo] \
4040
--inputs [json-string] \
4141
--workflow [workflow-file-name.yaml]
4242
`),
43-
Short: "Send a workflow dispatch event and watch the resulting GitHub Actions run",
44-
Long: heredoc.Doc(`
43+
Short: "Send a workflow dispatch event and watch the resulting GitHub Actions run",
44+
Long: heredoc.Doc(`
4545
This command sends a workflow dispatch event and attempts to find and watch the
4646
resulting GitHub Actions run whose file name or ID is specified as '--workflow'.
4747
@@ -50,7 +50,7 @@ var workflowCmd = &cobra.Command{
5050
watch an unrelated GitHub Actions workflow run in the event that multiple runs of
5151
the specified workflow are running concurrently.
5252
`),
53-
Example: heredoc.Doc(`
53+
Example: heredoc.Doc(`
5454
gh dispatch workflow \
5555
--repo mdb/gh-dispatch \
5656
--inputs '{"name": "Mike"}' \
@@ -63,28 +63,43 @@ var workflowCmd = &cobra.Command{
6363
--workflow workflow_dispatch.yaml \
6464
--ref my-feature-branch
6565
`),
66-
RunE: func(cmd *cobra.Command, args []string) error {
67-
repo, _ := cmd.Flags().GetString("repo")
68-
69-
b := []byte(workflowInputs)
70-
var wInputs interface{}
71-
json.Unmarshal(b, &wInputs)
72-
73-
ios := iostreams.System()
74-
75-
dOptions := dispatchOptions{
76-
repo: repo,
77-
httpTransport: http.DefaultTransport,
78-
io: ios,
79-
}
80-
81-
return workflowDispatchRun(&workflowDispatchOptions{
82-
inputs: wInputs,
83-
ref: workflowRef,
84-
workflow: workflowName,
85-
dispatchOptions: dOptions,
86-
})
87-
},
66+
RunE: func(cmd *cobra.Command, args []string) error {
67+
repo, _ := cmd.Flags().GetString("repo")
68+
69+
b := []byte(workflowInputs)
70+
var wInputs interface{}
71+
json.Unmarshal(b, &wInputs)
72+
73+
ios := iostreams.System()
74+
75+
dOptions := dispatchOptions{
76+
repo: repo,
77+
httpTransport: http.DefaultTransport,
78+
io: ios,
79+
}
80+
81+
return workflowDispatchRun(&workflowDispatchOptions{
82+
inputs: wInputs,
83+
ref: workflowRef,
84+
workflow: workflowName,
85+
dispatchOptions: dOptions,
86+
})
87+
},
88+
}
89+
90+
// TODO: how does the 'gh run' command represent inputs?
91+
// Is it worth better emulating its interface?
92+
cmd.Flags().StringVarP(&workflowInputs, "inputs", "i", "", "The workflow dispatch inputs JSON string.")
93+
cmd.MarkFlagRequired("inputs")
94+
// TODO: how does the 'gh run' command represent workflow?
95+
// Is it worth better emulating its interface?
96+
cmd.Flags().StringVarP(&workflowName, "workflow", "w", "", "The resulting GitHub Actions workflow name.")
97+
cmd.MarkFlagRequired("workflow")
98+
// TODO: how does the 'gh run' command represent ref?
99+
// Is it worth better emulating its interface?
100+
cmd.Flags().StringVarP(&workflowRef, "ref", "f", "main", "The git reference for the workflow. Can be a branch or tag name.")
101+
102+
return cmd
88103
}
89104

90105
func workflowDispatchRun(opts *workflowDispatchOptions) error {
@@ -129,19 +144,3 @@ func workflowDispatchRun(opts *workflowDispatchOptions) error {
129144

130145
return render(opts.io, client, opts.repo, run)
131146
}
132-
133-
func init() {
134-
// TODO: how does the 'gh run' command represent inputs?
135-
// Is it worth better emulating its interface?
136-
workflowCmd.Flags().StringVarP(&workflowInputs, "inputs", "i", "", "The workflow dispatch inputs JSON string.")
137-
workflowCmd.MarkFlagRequired("inputs")
138-
// TODO: how does the 'gh run' command represent workflow?
139-
// Is it worth better emulating its interface?
140-
workflowCmd.Flags().StringVarP(&workflowName, "workflow", "w", "", "The resulting GitHub Actions workflow name.")
141-
workflowCmd.MarkFlagRequired("workflow")
142-
// TODO: how does the 'gh run' command represent ref?
143-
// Is it worth better emulating its interface?
144-
workflowCmd.Flags().StringVarP(&workflowRef, "ref", "f", "main", "The git reference for the workflow. Can be a branch or tag name.")
145-
146-
rootCmd.AddCommand(workflowCmd)
147-
}

cmd/workflow_test.go renamed to dispatch/workflow_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package dispatch
22

33
import (
44
"fmt"

main.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)