Skip to content

Commit

Permalink
🐛 Fix bug that some commands can not be used outside git repository
Browse files Browse the repository at this point in the history
  • Loading branch information
5n7-sk committed Jul 5, 2020
1 parent db3c23e commit dc829d7
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 11 deletions.
8 changes: 5 additions & 3 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ const (

// CLI represents this application itself.
type CLI struct {
// CheckGit checks if a git repository exists.
CheckGit bool
// Clipboard doesn't commit, but copies the commit message to the clipboard.
Clipboard bool
// HookPath is the path to the file to write the commit message to.
HookPath string
}

// NewCLI returns a new CLI.
func NewCLI(clipboard bool, hook string) (*CLI, error) {
c := &CLI{Clipboard: clipboard, HookPath: hook}
if hook == "" {
func NewCLI(checkGit bool, clipboard bool, hook string) (*CLI, error) {
c := &CLI{CheckGit: checkGit, Clipboard: clipboard, HookPath: hook}
if c.CheckGit && hook == "" {
p, err := c.hookPath()
if err != nil {
return nil, err
Expand Down
8 changes: 5 additions & 3 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (

// Run runs CLI.
func (c CLI) Run() error {
_, err := c.GitRoot()
if err != nil {
return err
if c.CheckGit {
_, err := c.GitRoot()
if err != nil {
return err
}
}

p, err := c.ListPath()
Expand Down
2 changes: 1 addition & 1 deletion cmd/gmoji/cmd/cmd_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func runCopy(cmd *cobra.Command, args []string) error {
c, err := cli.NewCLI(true, "")
c, err := cli.NewCLI(false, true, "")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gmoji/cmd/cmd_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func runHook(cmd *cobra.Command, args []string) error {
c, err := cli.NewCLI(false, "")
c, err := cli.NewCLI(true, false, "")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gmoji/cmd/cmd_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func runInit(cmd *cobra.Command, args []string) error {
c, err := cli.NewCLI(false, "")
c, err := cli.NewCLI(false, false, "")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gmoji/cmd/cmd_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func runList(cmd *cobra.Command, args []string) error {
c, err := cli.NewCLI(false, "")
c, err := cli.NewCLI(false, false, "")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gmoji/cmd/cmd_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
)

func runRoot(cmd *cobra.Command, args []string) error {
c, err := cli.NewCLI(false, rootOptions.Hook)
c, err := cli.NewCLI(true, false, rootOptions.Hook)
if err != nil {
return err
}
Expand Down

0 comments on commit dc829d7

Please sign in to comment.