From 8f7e7a7630543766c6efdc561e9817be0e5a2acc Mon Sep 17 00:00:00 2001 From: Mike Ball Date: Mon, 14 Nov 2022 14:50:40 -0500 Subject: [PATCH 1/4] create 'dispatch' package --- cmd/repository.go | 126 +------------------------ cmd/workflow.go | 121 +----------------------- {cmd => dispatch}/fixtures_test.go | 2 +- dispatch/repository.go | 135 +++++++++++++++++++++++++++ {cmd => dispatch}/repository_test.go | 2 +- {cmd => dispatch}/shared.go | 2 +- dispatch/workflow.go | 132 ++++++++++++++++++++++++++ {cmd => dispatch}/workflow_test.go | 2 +- 8 files changed, 275 insertions(+), 247 deletions(-) rename {cmd => dispatch}/fixtures_test.go (99%) create mode 100644 dispatch/repository.go rename {cmd => dispatch}/repository_test.go (99%) rename {cmd => dispatch}/shared.go (99%) create mode 100644 dispatch/workflow.go rename {cmd => dispatch}/workflow_test.go (99%) diff --git a/cmd/repository.go b/cmd/repository.go index cbecf03..b36075a 100644 --- a/cmd/repository.go +++ b/cmd/repository.go @@ -1,30 +1,6 @@ package cmd -import ( - "bytes" - "encoding/json" - "fmt" - "net/http" - - "github.com/MakeNowJust/heredoc" - "github.com/cli/cli/v2/pkg/cmd/workflow/shared" - "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/go-gh" - "github.com/cli/go-gh/pkg/api" - "github.com/spf13/cobra" -) - -type repositoryDispatchRequest struct { - EventType string `json:"event_type"` - ClientPayload interface{} `json:"client_payload"` -} - -type repositoryDispatchOptions struct { - clientPayload interface{} - eventType string - workflow string - dispatchOptions -} +import "github.com/mdb/gh-dispatch/dispatch" var ( repositoryEventType string @@ -33,105 +9,7 @@ var ( ) // repositoryCmd represents the repository subcommand -var repositoryCmd = &cobra.Command{ - Use: heredoc.Doc(` - repository \ - --repo [owner/repo] \ - --event-type [event-type] \ - --client-payload [json-string] \ - --workflow [workflow-name] - `), - Short: "Send a repository dispatch event and watch the resulting GitHub Actions run", - Long: heredoc.Doc(` - This command sends a repository dispatch event and attempts to find and watch the - resulting GitHub Actions run whose name is specified as '--workflow'. - - Note that the command assumes the specified workflow supports a repository_dispatch - 'on' trigger. Also note that the command is vulnerable to race conditions and may - watch an unrelated GitHub Actions workflow run in the event that multiple runs of - the specified workflow are running concurrently. - `), - Example: heredoc.Doc(` - gh dispatch repository \ - --repo mdb/gh-dispatch \ - --event-type 'hello' \ - --client-payload '{"name": "Mike"}' \ - --workflow Hello - `), - RunE: func(cmd *cobra.Command, args []string) error { - repo, _ := cmd.Flags().GetString("repo") - - b := []byte(repositoryClientPayload) - var repoClientPayload interface{} - json.Unmarshal(b, &repoClientPayload) - - ios := iostreams.System() - - dOptions := dispatchOptions{ - repo: repo, - httpTransport: http.DefaultTransport, - io: ios, - } - - return repositoryDispatchRun(&repositoryDispatchOptions{ - clientPayload: repoClientPayload, - eventType: repositoryEventType, - workflow: repositoryWorkflow, - dispatchOptions: dOptions, - }) - }, -} - -func repositoryDispatchRun(opts *repositoryDispatchOptions) error { - var buf bytes.Buffer - err := json.NewEncoder(&buf).Encode(repositoryDispatchRequest{ - EventType: opts.eventType, - ClientPayload: opts.clientPayload, - }) - if err != nil { - return err - } - - client, err := gh.RESTClient(&api.ClientOptions{ - Transport: opts.httpTransport, - AuthToken: opts.authToken, - }) - if err != nil { - return err - } - - var in interface{} - err = client.Post(fmt.Sprintf("repos/%s/dispatches", opts.repo), &buf, &in) - if err != nil { - return err - } - - var wfs shared.WorkflowsPayload - err = client.Get(fmt.Sprintf("repos/%s/actions/workflows", opts.repo), &wfs) - if err != nil { - return err - } - - var workflowID int64 - for _, wf := range wfs.Workflows { - if wf.Name == opts.workflow { - workflowID = wf.ID - break - } - } - - runID, err := getRunID(client, opts.repo, "repository_dispatch", workflowID) - if err != nil { - return err - } - - run, err := getRun(client, opts.repo, runID) - if err != nil { - return fmt.Errorf("failed to get run: %w", err) - } - - return render(opts.io, client, opts.repo, run) -} +var repositoryCmd = dispatch.NewCmdRepository() func init() { repositoryCmd.Flags().StringVarP(&repositoryEventType, "event-type", "e", "", "The repository dispatch event type.") diff --git a/cmd/workflow.go b/cmd/workflow.go index 312897b..0f7b56f 100644 --- a/cmd/workflow.go +++ b/cmd/workflow.go @@ -1,31 +1,9 @@ package cmd import ( - "bytes" - "encoding/json" - "fmt" - "net/http" - - "github.com/MakeNowJust/heredoc" - "github.com/cli/cli/v2/pkg/cmd/workflow/shared" - "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/go-gh" - "github.com/cli/go-gh/pkg/api" - "github.com/spf13/cobra" + "github.com/mdb/gh-dispatch/dispatch" ) -type workflowDispatchRequest struct { - Inputs interface{} `json:"inputs"` - Ref string `json:"ref"` -} - -type workflowDispatchOptions struct { - inputs interface{} - ref string - workflow string - dispatchOptions -} - var ( workflowInputs string workflowName string @@ -33,102 +11,7 @@ var ( ) // workflowCmd represents the workflow subcommand -var workflowCmd = &cobra.Command{ - Use: heredoc.Doc(` - workflow \ - --repo [owner/repo] \ - --inputs [json-string] \ - --workflow [workflow-file-name.yaml] - `), - Short: "Send a workflow dispatch event and watch the resulting GitHub Actions run", - Long: heredoc.Doc(` - This command sends a workflow dispatch event and attempts to find and watch the - resulting GitHub Actions run whose file name or ID is specified as '--workflow'. - - Note that the command assumes the specified workflow supports a workflow_dispatch - 'on' trigger. Also note that the command is vulnerable to race conditions and may - watch an unrelated GitHub Actions workflow run in the event that multiple runs of - the specified workflow are running concurrently. - `), - Example: heredoc.Doc(` - gh dispatch workflow \ - --repo mdb/gh-dispatch \ - --inputs '{"name": "Mike"}' \ - --workflow workflow_dispatch.yaml - - # Specify a workflow ref other than 'main' - gh dispatch workflow \ - --repo mdb/gh-dispatch \ - --inputs '{"name": "Mike"}' \ - --workflow workflow_dispatch.yaml \ - --ref my-feature-branch - `), - RunE: func(cmd *cobra.Command, args []string) error { - repo, _ := cmd.Flags().GetString("repo") - - b := []byte(workflowInputs) - var wInputs interface{} - json.Unmarshal(b, &wInputs) - - ios := iostreams.System() - - dOptions := dispatchOptions{ - repo: repo, - httpTransport: http.DefaultTransport, - io: ios, - } - - return workflowDispatchRun(&workflowDispatchOptions{ - inputs: wInputs, - ref: workflowRef, - workflow: workflowName, - dispatchOptions: dOptions, - }) - }, -} - -func workflowDispatchRun(opts *workflowDispatchOptions) error { - var buf bytes.Buffer - err := json.NewEncoder(&buf).Encode(workflowDispatchRequest{ - Inputs: opts.inputs, - Ref: opts.ref, - }) - if err != nil { - return err - } - - client, err := gh.RESTClient(&api.ClientOptions{ - Transport: opts.httpTransport, - AuthToken: opts.authToken, - }) - if err != nil { - return err - } - - var in interface{} - err = client.Post(fmt.Sprintf("repos/%s/actions/workflows/%s/dispatches", opts.repo, opts.workflow), &buf, &in) - if err != nil { - return err - } - - var wf shared.Workflow - err = client.Get(fmt.Sprintf("repos/%s/actions/workflows/%s", opts.repo, opts.workflow), &wf) - if err != nil { - return err - } - - runID, err := getRunID(client, opts.repo, "workflow_dispatch", wf.ID) - if err != nil { - return err - } - - run, err := getRun(client, opts.repo, runID) - if err != nil { - return fmt.Errorf("failed to get run: %w", err) - } - - return render(opts.io, client, opts.repo, run) -} +var workflowCmd = dispatch.NewCmdWorkflow() func init() { // TODO: how does the 'gh run' command represent inputs? diff --git a/cmd/fixtures_test.go b/dispatch/fixtures_test.go similarity index 99% rename from cmd/fixtures_test.go rename to dispatch/fixtures_test.go index 555036c..a1cf53b 100644 --- a/cmd/fixtures_test.go +++ b/dispatch/fixtures_test.go @@ -1,4 +1,4 @@ -package cmd +package dispatch var ( getWorkflowsResponse string = `{ diff --git a/dispatch/repository.go b/dispatch/repository.go new file mode 100644 index 0000000..60c8753 --- /dev/null +++ b/dispatch/repository.go @@ -0,0 +1,135 @@ +package dispatch + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/workflow/shared" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/cli/go-gh" + "github.com/cli/go-gh/pkg/api" + "github.com/spf13/cobra" +) + +type repositoryDispatchRequest struct { + EventType string `json:"event_type"` + ClientPayload interface{} `json:"client_payload"` +} + +type repositoryDispatchOptions struct { + clientPayload interface{} + eventType string + workflow string + dispatchOptions +} + +var ( + repositoryEventType string + repositoryClientPayload string + repositoryWorkflow string +) + +func NewCmdRepository() *cobra.Command { + return &cobra.Command{ + Use: heredoc.Doc(` + repository \ + --repo [owner/repo] \ + --event-type [event-type] \ + --client-payload [json-string] \ + --workflow [workflow-name] + `), + Short: "Send a repository dispatch event and watch the resulting GitHub Actions run", + Long: heredoc.Doc(` + This command sends a repository dispatch event and attempts to find and watch the + resulting GitHub Actions run whose name is specified as '--workflow'. + + Note that the command assumes the specified workflow supports a repository_dispatch + 'on' trigger. Also note that the command is vulnerable to race conditions and may + watch an unrelated GitHub Actions workflow run in the event that multiple runs of + the specified workflow are running concurrently. + `), + Example: heredoc.Doc(` + gh dispatch repository \ + --repo mdb/gh-dispatch \ + --event-type 'hello' \ + --client-payload '{"name": "Mike"}' \ + --workflow Hello + `), + RunE: func(cmd *cobra.Command, args []string) error { + repo, _ := cmd.Flags().GetString("repo") + + b := []byte(repositoryClientPayload) + var repoClientPayload interface{} + json.Unmarshal(b, &repoClientPayload) + + ios := iostreams.System() + + dOptions := dispatchOptions{ + repo: repo, + httpTransport: http.DefaultTransport, + io: ios, + } + + return repositoryDispatchRun(&repositoryDispatchOptions{ + clientPayload: repoClientPayload, + eventType: repositoryEventType, + workflow: repositoryWorkflow, + dispatchOptions: dOptions, + }) + }, + } +} + +func repositoryDispatchRun(opts *repositoryDispatchOptions) error { + var buf bytes.Buffer + err := json.NewEncoder(&buf).Encode(repositoryDispatchRequest{ + EventType: opts.eventType, + ClientPayload: opts.clientPayload, + }) + if err != nil { + return err + } + + client, err := gh.RESTClient(&api.ClientOptions{ + Transport: opts.httpTransport, + AuthToken: opts.authToken, + }) + if err != nil { + return err + } + + var in interface{} + err = client.Post(fmt.Sprintf("repos/%s/dispatches", opts.repo), &buf, &in) + if err != nil { + return err + } + + var wfs shared.WorkflowsPayload + err = client.Get(fmt.Sprintf("repos/%s/actions/workflows", opts.repo), &wfs) + if err != nil { + return err + } + + var workflowID int64 + for _, wf := range wfs.Workflows { + if wf.Name == opts.workflow { + workflowID = wf.ID + break + } + } + + runID, err := getRunID(client, opts.repo, "repository_dispatch", workflowID) + if err != nil { + return err + } + + run, err := getRun(client, opts.repo, runID) + if err != nil { + return fmt.Errorf("failed to get run: %w", err) + } + + return render(opts.io, client, opts.repo, run) +} diff --git a/cmd/repository_test.go b/dispatch/repository_test.go similarity index 99% rename from cmd/repository_test.go rename to dispatch/repository_test.go index 13596b1..32ef979 100644 --- a/cmd/repository_test.go +++ b/dispatch/repository_test.go @@ -1,4 +1,4 @@ -package cmd +package dispatch import ( "fmt" diff --git a/cmd/shared.go b/dispatch/shared.go similarity index 99% rename from cmd/shared.go rename to dispatch/shared.go index e5967d7..e4c0698 100644 --- a/cmd/shared.go +++ b/dispatch/shared.go @@ -1,4 +1,4 @@ -package cmd +package dispatch import ( "bytes" diff --git a/dispatch/workflow.go b/dispatch/workflow.go new file mode 100644 index 0000000..4e91920 --- /dev/null +++ b/dispatch/workflow.go @@ -0,0 +1,132 @@ +package dispatch + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/workflow/shared" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/cli/go-gh" + "github.com/cli/go-gh/pkg/api" + "github.com/spf13/cobra" +) + +type workflowDispatchRequest struct { + Inputs interface{} `json:"inputs"` + Ref string `json:"ref"` +} + +type workflowDispatchOptions struct { + inputs interface{} + ref string + workflow string + dispatchOptions +} + +var ( + workflowInputs string + workflowName string + workflowRef string +) + +func NewCmdWorkflow() *cobra.Command { + return &cobra.Command{ + Use: heredoc.Doc(` + workflow \ + --repo [owner/repo] \ + --inputs [json-string] \ + --workflow [workflow-file-name.yaml] + `), + Short: "Send a workflow dispatch event and watch the resulting GitHub Actions run", + Long: heredoc.Doc(` + This command sends a workflow dispatch event and attempts to find and watch the + resulting GitHub Actions run whose file name or ID is specified as '--workflow'. + + Note that the command assumes the specified workflow supports a workflow_dispatch + 'on' trigger. Also note that the command is vulnerable to race conditions and may + watch an unrelated GitHub Actions workflow run in the event that multiple runs of + the specified workflow are running concurrently. + `), + Example: heredoc.Doc(` + gh dispatch workflow \ + --repo mdb/gh-dispatch \ + --inputs '{"name": "Mike"}' \ + --workflow workflow_dispatch.yaml + + # Specify a workflow ref other than 'main' + gh dispatch workflow \ + --repo mdb/gh-dispatch \ + --inputs '{"name": "Mike"}' \ + --workflow workflow_dispatch.yaml \ + --ref my-feature-branch + `), + RunE: func(cmd *cobra.Command, args []string) error { + repo, _ := cmd.Flags().GetString("repo") + + b := []byte(workflowInputs) + var wInputs interface{} + json.Unmarshal(b, &wInputs) + + ios := iostreams.System() + + dOptions := dispatchOptions{ + repo: repo, + httpTransport: http.DefaultTransport, + io: ios, + } + + return workflowDispatchRun(&workflowDispatchOptions{ + inputs: wInputs, + ref: workflowRef, + workflow: workflowName, + dispatchOptions: dOptions, + }) + }, + } +} + +func workflowDispatchRun(opts *workflowDispatchOptions) error { + var buf bytes.Buffer + err := json.NewEncoder(&buf).Encode(workflowDispatchRequest{ + Inputs: opts.inputs, + Ref: opts.ref, + }) + if err != nil { + return err + } + + client, err := gh.RESTClient(&api.ClientOptions{ + Transport: opts.httpTransport, + AuthToken: opts.authToken, + }) + if err != nil { + return err + } + + var in interface{} + err = client.Post(fmt.Sprintf("repos/%s/actions/workflows/%s/dispatches", opts.repo, opts.workflow), &buf, &in) + if err != nil { + return err + } + + var wf shared.Workflow + err = client.Get(fmt.Sprintf("repos/%s/actions/workflows/%s", opts.repo, opts.workflow), &wf) + if err != nil { + return err + } + + runID, err := getRunID(client, opts.repo, "workflow_dispatch", wf.ID) + if err != nil { + return err + } + + run, err := getRun(client, opts.repo, runID) + if err != nil { + return fmt.Errorf("failed to get run: %w", err) + } + + return render(opts.io, client, opts.repo, run) +} diff --git a/cmd/workflow_test.go b/dispatch/workflow_test.go similarity index 99% rename from cmd/workflow_test.go rename to dispatch/workflow_test.go index 4003581..88371a0 100644 --- a/cmd/workflow_test.go +++ b/dispatch/workflow_test.go @@ -1,4 +1,4 @@ -package cmd +package dispatch import ( "fmt" From fafb25ea734eec80bf5cef6f2f46eee6909b101f Mon Sep 17 00:00:00 2001 From: Mike Ball Date: Mon, 14 Nov 2022 15:13:31 -0500 Subject: [PATCH 2/4] bump version to 0.0.2 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 17ecb66..4d3d699 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ SOURCE=./... GOFMT_FILES?=$$(find . -type f -name '*.go') -VERSION?=0.0.2 +VERSION?=0.0.3 default: build From 32606ba109b27d364a06178dfda87efad400a71e Mon Sep 17 00:00:00 2001 From: Mike Ball Date: Mon, 14 Nov 2022 15:40:10 -0500 Subject: [PATCH 3/4] ensure flags are properly passed to commands --- cmd/repository.go | 17 +---------------- cmd/workflow.go | 22 +--------------------- dispatch/repository.go | 23 ++++++++++++++++------- dispatch/workflow.go | 28 +++++++++++++++++++++------- 4 files changed, 39 insertions(+), 51 deletions(-) diff --git a/cmd/repository.go b/cmd/repository.go index b36075a..031856b 100644 --- a/cmd/repository.go +++ b/cmd/repository.go @@ -2,22 +2,7 @@ package cmd import "github.com/mdb/gh-dispatch/dispatch" -var ( - repositoryEventType string - repositoryClientPayload string - repositoryWorkflow string -) - -// repositoryCmd represents the repository subcommand -var repositoryCmd = dispatch.NewCmdRepository() - func init() { - repositoryCmd.Flags().StringVarP(&repositoryEventType, "event-type", "e", "", "The repository dispatch event type.") - repositoryCmd.MarkFlagRequired("event-type") - repositoryCmd.Flags().StringVarP(&repositoryClientPayload, "client-payload", "p", "", "The repository dispatch event client payload JSON string.") - repositoryCmd.MarkFlagRequired("client-payload") - repositoryCmd.Flags().StringVarP(&repositoryWorkflow, "workflow", "w", "", "The resulting GitHub Actions workflow name.") - repositoryCmd.MarkFlagRequired("workflow") - + repositoryCmd := dispatch.NewCmdRepository() rootCmd.AddCommand(repositoryCmd) } diff --git a/cmd/workflow.go b/cmd/workflow.go index 0f7b56f..7c5f176 100644 --- a/cmd/workflow.go +++ b/cmd/workflow.go @@ -4,27 +4,7 @@ import ( "github.com/mdb/gh-dispatch/dispatch" ) -var ( - workflowInputs string - workflowName string - workflowRef string -) - -// workflowCmd represents the workflow subcommand -var workflowCmd = dispatch.NewCmdWorkflow() - func init() { - // TODO: how does the 'gh run' command represent inputs? - // Is it worth better emulating its interface? - workflowCmd.Flags().StringVarP(&workflowInputs, "inputs", "i", "", "The workflow dispatch inputs JSON string.") - workflowCmd.MarkFlagRequired("inputs") - // TODO: how does the 'gh run' command represent workflow? - // Is it worth better emulating its interface? - workflowCmd.Flags().StringVarP(&workflowName, "workflow", "w", "", "The resulting GitHub Actions workflow name.") - workflowCmd.MarkFlagRequired("workflow") - // TODO: how does the 'gh run' command represent ref? - // Is it worth better emulating its interface? - workflowCmd.Flags().StringVarP(&workflowRef, "ref", "f", "main", "The git reference for the workflow. Can be a branch or tag name.") - + workflowCmd := dispatch.NewCmdWorkflow() rootCmd.AddCommand(workflowCmd) } diff --git a/dispatch/repository.go b/dispatch/repository.go index 60c8753..32abaad 100644 --- a/dispatch/repository.go +++ b/dispatch/repository.go @@ -26,14 +26,14 @@ type repositoryDispatchOptions struct { dispatchOptions } -var ( - repositoryEventType string - repositoryClientPayload string - repositoryWorkflow string -) - func NewCmdRepository() *cobra.Command { - return &cobra.Command{ + var ( + repositoryEventType string + repositoryClientPayload string + repositoryWorkflow string + ) + + cmd := &cobra.Command{ Use: heredoc.Doc(` repository \ --repo [owner/repo] \ @@ -81,6 +81,15 @@ func NewCmdRepository() *cobra.Command { }) }, } + + cmd.Flags().StringVarP(&repositoryEventType, "event-type", "e", "", "The repository dispatch event type.") + cmd.MarkFlagRequired("event-type") + cmd.Flags().StringVarP(&repositoryClientPayload, "client-payload", "p", "", "The repository dispatch event client payload JSON string.") + cmd.MarkFlagRequired("client-payload") + cmd.Flags().StringVarP(&repositoryWorkflow, "workflow", "w", "", "The resulting GitHub Actions workflow name.") + cmd.MarkFlagRequired("workflow") + + return cmd } func repositoryDispatchRun(opts *repositoryDispatchOptions) error { diff --git a/dispatch/workflow.go b/dispatch/workflow.go index 4e91920..3a4a08c 100644 --- a/dispatch/workflow.go +++ b/dispatch/workflow.go @@ -26,14 +26,14 @@ type workflowDispatchOptions struct { dispatchOptions } -var ( - workflowInputs string - workflowName string - workflowRef string -) - func NewCmdWorkflow() *cobra.Command { - return &cobra.Command{ + var ( + workflowInputs string + workflowName string + workflowRef string + ) + + cmd := &cobra.Command{ Use: heredoc.Doc(` workflow \ --repo [owner/repo] \ @@ -86,6 +86,20 @@ func NewCmdWorkflow() *cobra.Command { }) }, } + + // TODO: how does the 'gh run' command represent inputs? + // Is it worth better emulating its interface? + cmd.Flags().StringVarP(&workflowInputs, "inputs", "i", "", "The workflow dispatch inputs JSON string.") + cmd.MarkFlagRequired("inputs") + // TODO: how does the 'gh run' command represent workflow? + // Is it worth better emulating its interface? + cmd.Flags().StringVarP(&workflowName, "workflow", "w", "", "The resulting GitHub Actions workflow name.") + cmd.MarkFlagRequired("workflow") + // TODO: how does the 'gh run' command represent ref? + // Is it worth better emulating its interface? + cmd.Flags().StringVarP(&workflowRef, "ref", "f", "main", "The git reference for the workflow. Can be a branch or tag name.") + + return cmd } func workflowDispatchRun(opts *workflowDispatchOptions) error { From 0ee4ce48a8d09171b92951c9beeaa5507b23d51d Mon Sep 17 00:00:00 2001 From: Mike Ball Date: Mon, 14 Nov 2022 16:32:14 -0500 Subject: [PATCH 4/4] move main to cmd directory --- .goreleaser.yaml | 1 + cmd/main.go | 38 ++++++++++++++++++++++++++++++++ main_test.go => cmd/main_test.go | 0 cmd/repository.go | 8 ------- cmd/root.go | 32 --------------------------- cmd/workflow.go | 10 --------- main.go | 10 --------- 7 files changed, 39 insertions(+), 60 deletions(-) create mode 100644 cmd/main.go rename main_test.go => cmd/main_test.go (100%) delete mode 100644 cmd/repository.go delete mode 100644 cmd/root.go delete mode 100644 cmd/workflow.go delete mode 100644 main.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 752be5b..3e15a0c 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -10,6 +10,7 @@ builds: - linux - windows - netbsd + main: ./cmd/main.go archives: - name_template: "{{ .Os }}-{{ .Arch }}" format: binary diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..31c31ed --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "os" + + "github.com/MakeNowJust/heredoc" + "github.com/mdb/gh-dispatch/dispatch" + "github.com/spf13/cobra" +) + +// version's value is passed in at build time. +var version string + +func main() { + rootCmd := &cobra.Command{ + Use: "gh dispatch", + Short: "Send a GitHub dispatch event and watch the resulting GitHub Actions run", + Long: heredoc.Doc(` + Send a workflow_dispatch or repository_dispatch event and watch the resulting + GitHub Actions run. + `), + SilenceUsage: true, + Version: version, + } + + // TODO: how to make this required? + rootCmd.PersistentFlags().StringP("repo", "R", "", "The targeted repository's full name (in 'owner/repo' format)") + + repositoryCmd := dispatch.NewCmdRepository() + rootCmd.AddCommand(repositoryCmd) + + workflowCmd := dispatch.NewCmdWorkflow() + rootCmd.AddCommand(workflowCmd) + + if err := rootCmd.Execute(); err != nil { + os.Exit(1) + } +} diff --git a/main_test.go b/cmd/main_test.go similarity index 100% rename from main_test.go rename to cmd/main_test.go diff --git a/cmd/repository.go b/cmd/repository.go deleted file mode 100644 index 031856b..0000000 --- a/cmd/repository.go +++ /dev/null @@ -1,8 +0,0 @@ -package cmd - -import "github.com/mdb/gh-dispatch/dispatch" - -func init() { - repositoryCmd := dispatch.NewCmdRepository() - rootCmd.AddCommand(repositoryCmd) -} diff --git a/cmd/root.go b/cmd/root.go deleted file mode 100644 index 056ed4c..0000000 --- a/cmd/root.go +++ /dev/null @@ -1,32 +0,0 @@ -package cmd - -import ( - "os" - - "github.com/MakeNowJust/heredoc" - "github.com/spf13/cobra" -) - -// rootCmd is the root command. -var rootCmd = &cobra.Command{ - Use: "gh dispatch", - Short: "Send a GitHub dispatch event and watch the resulting GitHub Actions run", - Long: heredoc.Doc(` - Send a workflow_dispatch or repository_dispatch event and watch the resulting - GitHub Actions run. - `), - SilenceUsage: true, -} - -func init() { - // TODO: how to make this required? - rootCmd.PersistentFlags().StringP("repo", "R", "", "The targeted repository's full name (in 'owner/repo' format)") -} - -// Execute executes the root command. -func Execute(version string) { - rootCmd.Version = version - if err := rootCmd.Execute(); err != nil { - os.Exit(1) - } -} diff --git a/cmd/workflow.go b/cmd/workflow.go deleted file mode 100644 index 7c5f176..0000000 --- a/cmd/workflow.go +++ /dev/null @@ -1,10 +0,0 @@ -package cmd - -import ( - "github.com/mdb/gh-dispatch/dispatch" -) - -func init() { - workflowCmd := dispatch.NewCmdWorkflow() - rootCmd.AddCommand(workflowCmd) -} diff --git a/main.go b/main.go deleted file mode 100644 index 7fb63d5..0000000 --- a/main.go +++ /dev/null @@ -1,10 +0,0 @@ -package main - -import "github.com/mdb/gh-dispatch/cmd" - -// version's value is passed in at build time. -var version string - -func main() { - cmd.Execute(version) -}