Skip to content

Commit

Permalink
commands: refactor to avoid globals
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed Jun 28, 2020
1 parent c0dbc14 commit 26bd1dd
Show file tree
Hide file tree
Showing 42 changed files with 1,339 additions and 1,122 deletions.
80 changes: 40 additions & 40 deletions commands/add.go
@@ -1,78 +1,78 @@
package commands

import (
"fmt"
"github.com/spf13/cobra"

"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/input"
"github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)

var (
addTitle string
addMessage string
addMessageFile string
)
type addOptions struct {
title string
message string
messageFile string
}

func newAddCommand() *cobra.Command {
env := newEnv()
options := addOptions{}

cmd := &cobra.Command{
Use: "add",
Short: "Create a new bug.",
PreRunE: loadRepoEnsureUser(env),
RunE: func(cmd *cobra.Command, args []string) error {
return runAdd(env, options)
},
}

flags := cmd.Flags()
flags.SortFlags = false

func runAddBug(cmd *cobra.Command, args []string) error {
var err error
flags.StringVarP(&options.title, "title", "t", "",
"Provide a title to describe the issue")
flags.StringVarP(&options.message, "message", "m", "",
"Provide a message to describe the issue")
flags.StringVarP(&options.messageFile, "file", "F", "",
"Take the message from the given file. Use - to read the message from the standard input")

backend, err := cache.NewRepoCache(repo)
return cmd
}

func runAdd(env *Env, opts addOptions) error {
backend, err := cache.NewRepoCache(env.repo)
if err != nil {
return err
}
defer backend.Close()
interrupt.RegisterCleaner(backend.Close)

if addMessageFile != "" && addMessage == "" {
addTitle, addMessage, err = input.BugCreateFileInput(addMessageFile)
if opts.messageFile != "" && opts.message == "" {
opts.title, opts.message, err = input.BugCreateFileInput(opts.messageFile)
if err != nil {
return err
}
}

if addMessageFile == "" && (addMessage == "" || addTitle == "") {
addTitle, addMessage, err = input.BugCreateEditorInput(backend, addTitle, addMessage)
if opts.messageFile == "" && (opts.message == "" || opts.title == "") {
opts.title, opts.message, err = input.BugCreateEditorInput(backend, opts.title, opts.message)

if err == input.ErrEmptyTitle {
fmt.Println("Empty title, aborting.")
env.out.Println("Empty title, aborting.")
return nil
}
if err != nil {
return err
}
}

b, _, err := backend.NewBug(addTitle, addMessage)
b, _, err := backend.NewBug(opts.title, opts.message)
if err != nil {
return err
}

fmt.Printf("%s created\n", b.Id().Human())
env.out.Printf("%s created\n", b.Id().Human())

return nil
}

var addCmd = &cobra.Command{
Use: "add",
Short: "Create a new bug.",
PreRunE: loadRepoEnsureUser,
RunE: runAddBug,
}

func init() {
RootCmd.AddCommand(addCmd)

addCmd.Flags().SortFlags = false

addCmd.Flags().StringVarP(&addTitle, "title", "t", "",
"Provide a title to describe the issue",
)
addCmd.Flags().StringVarP(&addMessage, "message", "m", "",
"Provide a message to describe the issue",
)
addCmd.Flags().StringVarP(&addMessageFile, "file", "F", "",
"Take the message from the given file. Use - to read the message from the standard input",
)
}
42 changes: 25 additions & 17 deletions commands/bridge.go
@@ -1,17 +1,37 @@
package commands

import (
"fmt"

"github.com/spf13/cobra"

"github.com/MichaelMure/git-bug/bridge"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/util/interrupt"
)

func runBridge(cmd *cobra.Command, args []string) error {
backend, err := cache.NewRepoCache(repo)
func newBridgeCommand() *cobra.Command {
env := newEnv()

cmd := &cobra.Command{
Use: "bridge",
Short: "Configure and use bridges to other bug trackers.",
PreRunE: loadRepo(env),
RunE: func(cmd *cobra.Command, args []string) error {
return runBridge(env)
},
Args: cobra.NoArgs,
}

cmd.AddCommand(newBridgeAuthCommand())
cmd.AddCommand(newBridgeConfigureCommand())
cmd.AddCommand(newBridgePullCommand())
cmd.AddCommand(newBridgePushCommand())
cmd.AddCommand(newBridgeRm())

return cmd
}

func runBridge(env *Env) error {
backend, err := cache.NewRepoCache(env.repo)
if err != nil {
return err
}
Expand All @@ -24,20 +44,8 @@ func runBridge(cmd *cobra.Command, args []string) error {
}

for _, c := range configured {
fmt.Println(c)
env.out.Println(c)
}

return nil
}

var bridgeCmd = &cobra.Command{
Use: "bridge",
Short: "Configure and use bridges to other bug trackers.",
PreRunE: loadRepo,
RunE: runBridge,
Args: cobra.NoArgs,
}

func init() {
RootCmd.AddCommand(bridgeCmd)
}
40 changes: 23 additions & 17 deletions commands/bridge_auth.go
@@ -1,7 +1,6 @@
package commands

import (
"fmt"
"sort"
"strings"

Expand All @@ -15,8 +14,28 @@ import (
"github.com/MichaelMure/git-bug/util/interrupt"
)

func runBridgeAuth(cmd *cobra.Command, args []string) error {
backend, err := cache.NewRepoCache(repo)
func newBridgeAuthCommand() *cobra.Command {
env := newEnv()

cmd := &cobra.Command{
Use: "auth",
Short: "List all known bridge authentication credentials.",
PreRunE: loadRepo(env),
RunE: func(cmd *cobra.Command, args []string) error {
return runBridgeAuth(env)
},
Args: cobra.NoArgs,
}

cmd.AddCommand(newBridgeAuthAddTokenCommand())
cmd.AddCommand(newBridgeAuthRm())
cmd.AddCommand(newBridgeAuthShow())

return cmd
}

func runBridgeAuth(env *Env) error {
backend, err := cache.NewRepoCache(env.repo)
if err != nil {
return err
}
Expand Down Expand Up @@ -44,7 +63,7 @@ func runBridgeAuth(cmd *cobra.Command, args []string) error {
sort.Strings(meta)
metaFmt := strings.Join(meta, ",")

fmt.Printf("%s %s %s %s %s\n",
env.out.Printf("%s %s %s %s %s\n",
colors.Cyan(cred.ID().Human()),
colors.Yellow(targetFmt),
colors.Magenta(cred.Kind()),
Expand All @@ -55,16 +74,3 @@ func runBridgeAuth(cmd *cobra.Command, args []string) error {

return nil
}

var bridgeAuthCmd = &cobra.Command{
Use: "auth",
Short: "List all known bridge authentication credentials.",
PreRunE: loadRepo,
RunE: runBridgeAuth,
Args: cobra.NoArgs,
}

func init() {
bridgeCmd.AddCommand(bridgeAuthCmd)
bridgeAuthCmd.Flags().SortFlags = false
}

0 comments on commit 26bd1dd

Please sign in to comment.