Skip to content

Commit

Permalink
Merge pull request #18 from mdb/reorg
Browse files Browse the repository at this point in the history
code reorganization
  • Loading branch information
mdb committed Nov 14, 2022
2 parents d1640c0 + 0ee4ce4 commit 1ce7c73
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 140 deletions.
1 change: 1 addition & 0 deletions .goreleaser.yaml
Expand Up @@ -10,6 +10,7 @@ builds:
- linux
- windows
- netbsd
main: ./cmd/main.go
archives:
- name_template: "{{ .Os }}-{{ .Arch }}"
format: binary
Expand Down
2 changes: 1 addition & 1 deletion Makefile
@@ -1,6 +1,6 @@
SOURCE=./...
GOFMT_FILES?=$$(find . -type f -name '*.go')
VERSION?=0.0.2
VERSION?=0.0.3

default: build

Expand Down
38 changes: 38 additions & 0 deletions 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)
}
}
File renamed without changes.
32 changes: 0 additions & 32 deletions cmd/root.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/fixtures_test.go → dispatch/fixtures_test.go
@@ -1,4 +1,4 @@
package cmd
package dispatch

var (
getWorkflowsResponse string = `{
Expand Down
85 changes: 42 additions & 43 deletions cmd/repository.go → dispatch/repository.go
@@ -1,4 +1,4 @@
package cmd
package dispatch

import (
"bytes"
Expand Down Expand Up @@ -26,23 +26,23 @@ type repositoryDispatchOptions struct {
dispatchOptions
}

var (
repositoryEventType string
repositoryClientPayload string
repositoryWorkflow string
)
func NewCmdRepository() *cobra.Command {
var (
repositoryEventType string
repositoryClientPayload string
repositoryWorkflow string
)

// repositoryCmd represents the repository subcommand
var repositoryCmd = &cobra.Command{
Use: heredoc.Doc(`
cmd := &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(`
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'.
Expand All @@ -51,35 +51,45 @@ var repositoryCmd = &cobra.Command{
watch an unrelated GitHub Actions workflow run in the event that multiple runs of
the specified workflow are running concurrently.
`),
Example: heredoc.Doc(`
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()
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,
})
},
}

dOptions := dispatchOptions{
repo: repo,
httpTransport: http.DefaultTransport,
io: ios,
}
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 repositoryDispatchRun(&repositoryDispatchOptions{
clientPayload: repoClientPayload,
eventType: repositoryEventType,
workflow: repositoryWorkflow,
dispatchOptions: dOptions,
})
},
return cmd
}

func repositoryDispatchRun(opts *repositoryDispatchOptions) error {
Expand Down Expand Up @@ -132,14 +142,3 @@ func repositoryDispatchRun(opts *repositoryDispatchOptions) error {

return render(opts.io, client, opts.repo, run)
}

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")

rootCmd.AddCommand(repositoryCmd)
}
2 changes: 1 addition & 1 deletion cmd/repository_test.go → dispatch/repository_test.go
@@ -1,4 +1,4 @@
package cmd
package dispatch

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cmd/shared.go → dispatch/shared.go
@@ -1,4 +1,4 @@
package cmd
package dispatch

import (
"bytes"
Expand Down
99 changes: 49 additions & 50 deletions cmd/workflow.go → dispatch/workflow.go
@@ -1,4 +1,4 @@
package cmd
package dispatch

import (
"bytes"
Expand Down Expand Up @@ -26,22 +26,22 @@ type workflowDispatchOptions struct {
dispatchOptions
}

var (
workflowInputs string
workflowName string
workflowRef string
)
func NewCmdWorkflow() *cobra.Command {
var (
workflowInputs string
workflowName string
workflowRef string
)

// workflowCmd represents the workflow subcommand
var workflowCmd = &cobra.Command{
Use: heredoc.Doc(`
cmd := &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(`
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'.
Expand All @@ -50,7 +50,7 @@ var workflowCmd = &cobra.Command{
watch an unrelated GitHub Actions workflow run in the event that multiple runs of
the specified workflow are running concurrently.
`),
Example: heredoc.Doc(`
Example: heredoc.Doc(`
gh dispatch workflow \
--repo mdb/gh-dispatch \
--inputs '{"name": "Mike"}' \
Expand All @@ -63,28 +63,43 @@ var workflowCmd = &cobra.Command{
--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,
})
},
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,
})
},
}

// 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 {
Expand Down Expand Up @@ -129,19 +144,3 @@ func workflowDispatchRun(opts *workflowDispatchOptions) error {

return render(opts.io, client, opts.repo, run)
}

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.")

rootCmd.AddCommand(workflowCmd)
}
2 changes: 1 addition & 1 deletion cmd/workflow_test.go → dispatch/workflow_test.go
@@ -1,4 +1,4 @@
package cmd
package dispatch

import (
"fmt"
Expand Down
10 changes: 0 additions & 10 deletions main.go

This file was deleted.

0 comments on commit 1ce7c73

Please sign in to comment.