Skip to content

Commit bdbd5ea

Browse files
committed
refactor: centralize command execution logic in cobra commands
Consolidate the command execution logic by setting `cmd.Run` directly in the constructor functions instead of within the `Command` method. This improves maintainability and consistency across all command implementations.
1 parent 09e486f commit bdbd5ea

File tree

8 files changed

+14
-8
lines changed

8 files changed

+14
-8
lines changed

cmd/encode.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ func NewEncodeCommand() *encodeCmd {
3838

3939
cmd.MarkPersistentFlagRequired("source")
4040

41+
cmd.Run = apps.Execute
42+
4143
apps.cmd = cmd
4244
return apps
4345
}
4446

4547
func (c *encodeCmd) Command() *cobra.Command {
46-
c.cmd.Run = c.Execute
4748
return c.cmd
4849
}
4950

cmd/find.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ func NewFindCommand() *findCommand {
3434

3535
cmd.MarkPersistentFlagRequired("lang")
3636

37+
cmd.Run = apps.Execute
38+
3739
apps.cmd = cmd
3840
return apps
3941
}
4042

4143
func (c *findCommand) Command() *cobra.Command {
42-
c.cmd.Run = c.Execute
4344
return c.cmd
4445
}
4546

cmd/password_gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ func NewGeneratePasswordCommand() *generatePasswordCmd {
2828
cmd.PersistentFlags().IntVarP(&apps.len, "len", "l", 0, "Password length (words or chars)")
2929
cmd.PersistentFlags().IntVarP(&apps.passType, "type", "t", 0, "Password type (0: word, 1: phrase, 2: word with special, 3: phrase with special, 4: secure)")
3030

31+
cmd.Run = apps.Execute
3132
apps.cmd = cmd
3233
return apps
3334
}
3435

3536
func (c *generatePasswordCmd) Command() *cobra.Command {
36-
c.cmd.Run = c.Execute
3737
return c.cmd
3838
}
3939

cmd/pull.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ func NewPullCommand() *pullCmd {
3232
cmd.MarkPersistentFlagRequired("user")
3333
cmd.MarkPersistentFlagRequired("repo")
3434

35+
cmd.Run = apps.Execute
36+
3537
apps.cmd = cmd
3638
return apps
3739
}
3840

3941
func (c *pullCmd) Command() *cobra.Command {
40-
c.cmd.Run = c.Execute
4142
return c.cmd
4243
}
4344

cmd/runtest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ func NewRunTestCommand() *runtestCmd {
3434
cmd.MarkPersistentFlagRequired("file")
3535
cmd.MarkPersistentFlagRequired("input")
3636
cmd.MarkPersistentFlagRequired("output")
37+
cmd.Run = apps.Execute
3738

3839
apps.cmd = cmd
3940
return apps
4041
}
4142

4243
func (c *runtestCmd) Command() *cobra.Command {
43-
c.cmd.Run = c.Execute
4444
return c.cmd
4545
}
4646

cmd/setup.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ func NewSetupCommand() *setupCommand {
2626

2727
cmd.MarkPersistentFlagRequired("lang")
2828

29+
cmd.Run = apps.Execute
30+
2931
apps.cmd = cmd
3032
return apps
3133
}
3234

3335
func (c *setupCommand) Command() *cobra.Command {
34-
c.cmd.Run = c.Execute
3536
return c.cmd
3637
}
3738

cmd/sharefile.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ func NewShareFileCommand() *shareFileCmd {
2727
cmd.PersistentFlags().StringVarP(&apps.port, "port", "P", "9000", "Port of server")
2828
cmd.MarkPersistentFlagRequired("root")
2929

30+
cmd.Run = apps.Execute
31+
3032
apps.cmd = cmd
3133
return apps
3234
}
3335

3436
func (c *shareFileCmd) Command() *cobra.Command {
35-
c.cmd.Run = c.Execute
3637
return c.cmd
3738
}
3839

cmd/track.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ func NewTrackCommand() *trackCmd {
2626

2727
cmd.MarkPersistentFlagRequired("config")
2828

29+
cmd.Run = apps.Execute
30+
2931
apps.cmd = cmd
3032
return apps
3133
}
3234

3335
func (c *trackCmd) Command() *cobra.Command {
34-
c.cmd.Run = c.Execute
3536
return c.cmd
3637
}
3738

0 commit comments

Comments
 (0)