Skip to content

Commit 17608ae

Browse files
committed
refactor: use factory methods in commands
1 parent f759a75 commit 17608ae

9 files changed

Lines changed: 138 additions & 122 deletions

File tree

cmd/config/config.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"github.com/spf13/cobra"
55
)
66

7-
var ConfigCmd = &cobra.Command{
8-
Use: "config",
9-
Short: "Manage pal config",
10-
}
7+
func NewConfigCmd() *cobra.Command {
8+
cmd := &cobra.Command{
9+
Use: "config",
10+
Short: "Manage pal config",
11+
}
12+
13+
cmd.AddCommand(NewListCmd())
14+
cmd.AddCommand(NewUpdateCmd())
1115

12-
func init() {
13-
ConfigCmd.AddCommand(listCmd)
14-
ConfigCmd.AddCommand(updateCmd)
16+
return cmd
1517
}

cmd/config/list.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,22 @@ import (
1313
"github.com/spf13/cobra"
1414
)
1515

16-
var listCmd = &cobra.Command{
17-
Use: "list",
18-
Short: "List pal config",
19-
Aliases: []string{"ls"},
20-
RunE: func(cmd *cobra.Command, args []string) error {
21-
fs := afero.NewOsFs()
22-
23-
prereqsErr := CheckListPrerequisites(fs)
24-
if prereqsErr != nil {
25-
return prereqsErr
26-
}
27-
28-
return RunListCmd(fs, os.Stdout)
29-
},
16+
func NewListCmd() *cobra.Command {
17+
return &cobra.Command{
18+
Use: "list",
19+
Short: "List pal config",
20+
Aliases: []string{"ls"},
21+
RunE: func(cmd *cobra.Command, args []string) error {
22+
fs := afero.NewOsFs()
23+
24+
prereqsErr := CheckListPrerequisites(fs)
25+
if prereqsErr != nil {
26+
return prereqsErr
27+
}
28+
29+
return RunListCmd(fs, os.Stdout)
30+
},
31+
}
3032
}
3133

3234
func CheckListPrerequisites(fs afero.Fs) error {

cmd/config/update.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,26 @@ type UpdatePrompts struct {
2222
updatedConfig []UpdatedConfig
2323
}
2424

25-
var updateCmd = &cobra.Command{
26-
Use: "update",
27-
Short: "Update pal config",
28-
RunE: func(cmd *cobra.Command, args []string) error {
29-
fs := afero.NewOsFs()
30-
31-
prereqsErr := CheckUpdatePrerequisites(fs)
32-
if prereqsErr != nil {
33-
return prereqsErr
34-
}
25+
func NewUpdateCmd() *cobra.Command {
26+
return &cobra.Command{
27+
Use: "update",
28+
Short: "Update pal config",
29+
RunE: func(cmd *cobra.Command, args []string) error {
30+
fs := afero.NewOsFs()
31+
32+
prereqsErr := CheckUpdatePrerequisites(fs)
33+
if prereqsErr != nil {
34+
return prereqsErr
35+
}
3536

36-
up, promptsErr := RunUpdatePrompts(fs)
37-
if promptsErr != nil {
38-
return promptsErr
39-
}
37+
up, promptsErr := RunUpdatePrompts(fs)
38+
if promptsErr != nil {
39+
return promptsErr
40+
}
4041

41-
return RunUpdateCmd(fs, up)
42-
},
42+
return RunUpdateCmd(fs, up)
43+
},
44+
}
4345
}
4446

4547
func CheckUpdatePrerequisites(fs afero.Fs) error {

cmd/create.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,27 @@ type CreatePrompts struct {
1919
editCmd string
2020
}
2121

22-
var createCmd = &cobra.Command{
23-
Use: "create",
24-
Short: "Create an alias",
25-
Aliases: []string{"cr"},
26-
RunE: func(cmd *cobra.Command, args []string) error {
27-
fs := afero.NewOsFs()
28-
29-
prereqsErr := CheckCreatePrerequisites(fs)
30-
if prereqsErr != nil {
31-
return prereqsErr
32-
}
33-
34-
cp, promptsErr := RunCreatePrompts(fs)
35-
if promptsErr != nil {
36-
return promptsErr
37-
}
38-
39-
return RunCreateCmd(fs, cp)
40-
},
22+
func NewCreateCmd() *cobra.Command {
23+
return &cobra.Command{
24+
Use: "create",
25+
Short: "Create an alias",
26+
Aliases: []string{"cr"},
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
fs := afero.NewOsFs()
29+
30+
prereqsErr := CheckCreatePrerequisites(fs)
31+
if prereqsErr != nil {
32+
return prereqsErr
33+
}
34+
35+
cp, promptsErr := RunCreatePrompts(fs)
36+
if promptsErr != nil {
37+
return promptsErr
38+
}
39+
40+
return RunCreateCmd(fs, cp)
41+
},
42+
}
4143
}
4244

4345
func CheckCreatePrerequisites(fs afero.Fs) error {

cmd/install.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@ type InstallPrompts struct {
1616
shell string
1717
}
1818

19-
var installCmd = &cobra.Command{
20-
Use: "install",
21-
Short: "Create the config file",
22-
Aliases: []string{"i"},
23-
RunE: func(cmd *cobra.Command, args []string) error {
24-
fs := afero.NewOsFs()
25-
ip, err := RunPrompts()
26-
if err != nil {
27-
return err
28-
}
19+
func NewInstallCmd() *cobra.Command {
20+
return &cobra.Command{
21+
Use: "install",
22+
Short: "Create the config file",
23+
Aliases: []string{"i"},
24+
RunE: func(cmd *cobra.Command, args []string) error {
25+
fs := afero.NewOsFs()
26+
ip, err := RunPrompts()
27+
if err != nil {
28+
return err
29+
}
2930

30-
return RunInstallCmd(fs, ip)
31-
},
31+
return RunInstallCmd(fs, ip)
32+
},
33+
}
3234
}
3335

3436
func RunPrompts() (InstallPrompts, error) {

cmd/list.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@ import (
1212
"github.com/spf13/cobra"
1313
)
1414

15-
var listCmd = &cobra.Command{
16-
Use: "list",
17-
Short: "Display list of aliases",
18-
Aliases: []string{"ls"},
19-
RunE: func(cmd *cobra.Command, args []string) error {
20-
fs := afero.NewOsFs()
15+
func NewListCmd() *cobra.Command {
16+
return &cobra.Command{
17+
Use: "list",
18+
Short: "Display list of aliases",
19+
Aliases: []string{"ls"},
20+
RunE: func(cmd *cobra.Command, args []string) error {
21+
fs := afero.NewOsFs()
2122

22-
prereqsErr := CheckListPrerequisites(fs)
23-
if prereqsErr != nil {
24-
return prereqsErr
25-
}
23+
prereqsErr := CheckListPrerequisites(fs)
24+
if prereqsErr != nil {
25+
return prereqsErr
26+
}
2627

27-
return RunListCmd(fs, os.Stdout)
28-
},
28+
return RunListCmd(fs, os.Stdout)
29+
},
30+
}
2931
}
3032

3133
func CheckListPrerequisites(fs afero.Fs) error {

cmd/remove.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,27 @@ type RemovePrompts struct {
1717
aliasesToRemove []string
1818
}
1919

20-
var removeCmd = &cobra.Command{
21-
Use: "remove",
22-
Short: "Remove an alias",
23-
Aliases: []string{"rm"},
24-
RunE: func(cmd *cobra.Command, args []string) error {
25-
fs := afero.NewOsFs()
26-
27-
prereqsErr := CheckRemovePrerequisites(fs)
28-
if prereqsErr != nil {
29-
return prereqsErr
30-
}
31-
32-
rp, promptsErr := RunRemovePrompts(fs)
33-
if promptsErr != nil {
34-
return promptsErr
35-
}
36-
37-
return RunRemoveCmd(fs, rp)
38-
},
20+
func NewRemoveCmd() *cobra.Command {
21+
return &cobra.Command{
22+
Use: "remove",
23+
Short: "Remove an alias",
24+
Aliases: []string{"rm"},
25+
RunE: func(cmd *cobra.Command, args []string) error {
26+
fs := afero.NewOsFs()
27+
28+
prereqsErr := CheckRemovePrerequisites(fs)
29+
if prereqsErr != nil {
30+
return prereqsErr
31+
}
32+
33+
rp, promptsErr := RunRemovePrompts(fs)
34+
if promptsErr != nil {
35+
return promptsErr
36+
}
37+
38+
return RunRemoveCmd(fs, rp)
39+
},
40+
}
3941
}
4042

4143
func CheckRemovePrerequisites(fs afero.Fs) error {

cmd/root.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ var rootCmd = &cobra.Command{
1414

1515
func Execute(version string) error {
1616
rootCmd.Version = version
17-
rootCmd.AddCommand(createCmd)
18-
rootCmd.AddCommand(installCmd)
19-
rootCmd.AddCommand(listCmd)
20-
rootCmd.AddCommand(removeCmd)
21-
rootCmd.AddCommand(updateCmd)
22-
rootCmd.AddCommand(config.ConfigCmd)
17+
rootCmd.AddCommand(NewCreateCmd())
18+
rootCmd.AddCommand(NewInstallCmd())
19+
rootCmd.AddCommand(NewListCmd())
20+
rootCmd.AddCommand(NewRemoveCmd())
21+
rootCmd.AddCommand(NewUpdateCmd())
22+
rootCmd.AddCommand(config.NewConfigCmd())
2323

2424
return rootCmd.Execute()
2525
}

cmd/update.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,27 @@ type UpdatePrompts struct {
2323
updatedAliases []UpdatedAlias
2424
}
2525

26-
var updateCmd = &cobra.Command{
27-
Use: "update",
28-
Short: "Update an alias",
29-
Aliases: []string{"up"},
30-
RunE: func(cmd *cobra.Command, args []string) error {
31-
fs := afero.NewOsFs()
32-
33-
prereqsErr := CheckUpdatePrerequisites(fs)
34-
if prereqsErr != nil {
35-
return prereqsErr
36-
}
26+
func NewUpdateCmd() *cobra.Command {
27+
return &cobra.Command{
28+
Use: "update",
29+
Short: "Update an alias",
30+
Aliases: []string{"up"},
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
fs := afero.NewOsFs()
33+
34+
prereqsErr := CheckUpdatePrerequisites(fs)
35+
if prereqsErr != nil {
36+
return prereqsErr
37+
}
3738

38-
up, promptsErr := RunUpdatePrompts(fs)
39-
if promptsErr != nil {
40-
return promptsErr
41-
}
39+
up, promptsErr := RunUpdatePrompts(fs)
40+
if promptsErr != nil {
41+
return promptsErr
42+
}
4243

43-
return RunUpdateCmd(fs, up)
44-
},
44+
return RunUpdateCmd(fs, up)
45+
},
46+
}
4547
}
4648

4749
func CheckUpdatePrerequisites(fs afero.Fs) error {

0 commit comments

Comments
 (0)