mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-05-27 12:52:27 +02:00
refactor: extract empty string filtering to reusable function
- Add filterEmptyStrings utility function for consistent string filtering - Replace inline slices.DeleteFunc calls with filterEmptyStrings - Apply filtering to osArgs in addition to command args - Improves code readability and reduces duplication - Uses slices.DeleteFunc internally for efficient filtering
This commit is contained in:
committed by
Cuong Manh Le
parent
02032c8f9f
commit
6e1e9426da
+11
-6
@@ -205,9 +205,7 @@ func initStartCmd() *cobra.Command {
|
|||||||
|
|
||||||
NOTE: running "ctrld start" without any arguments will start already installed ctrld service.`,
|
NOTE: running "ctrld start" without any arguments will start already installed ctrld service.`,
|
||||||
Args: func(cmd *cobra.Command, args []string) error {
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
args = slices.DeleteFunc(args, func(arg string) bool {
|
args = filterEmptyStrings(args)
|
||||||
return arg == ""
|
|
||||||
})
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
return fmt.Errorf("'ctrld start' doesn't accept positional arguments\n" +
|
return fmt.Errorf("'ctrld start' doesn't accept positional arguments\n" +
|
||||||
"Use flags instead (e.g. --cd, --iface) or see 'ctrld start --help' for all options")
|
"Use flags instead (e.g. --cd, --iface) or see 'ctrld start --help' for all options")
|
||||||
@@ -221,6 +219,7 @@ NOTE: running "ctrld start" without any arguments will start already installed c
|
|||||||
sc := &service.Config{}
|
sc := &service.Config{}
|
||||||
*sc = *svcConfig
|
*sc = *svcConfig
|
||||||
osArgs := os.Args[2:]
|
osArgs := os.Args[2:]
|
||||||
|
osArgs = filterEmptyStrings(osArgs)
|
||||||
if os.Args[1] == "service" {
|
if os.Args[1] == "service" {
|
||||||
osArgs = os.Args[3:]
|
osArgs = os.Args[3:]
|
||||||
}
|
}
|
||||||
@@ -524,9 +523,7 @@ NOTE: running "ctrld start" without any arguments will start already installed c
|
|||||||
|
|
||||||
NOTE: running "ctrld start" without any arguments will start already installed ctrld service.`,
|
NOTE: running "ctrld start" without any arguments will start already installed ctrld service.`,
|
||||||
Args: func(cmd *cobra.Command, args []string) error {
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
args = slices.DeleteFunc(args, func(arg string) bool {
|
args = filterEmptyStrings(args)
|
||||||
return arg == ""
|
|
||||||
})
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
return fmt.Errorf("'ctrld start' doesn't accept positional arguments\n" +
|
return fmt.Errorf("'ctrld start' doesn't accept positional arguments\n" +
|
||||||
"Use flags instead (e.g. --cd, --iface) or see 'ctrld start --help' for all options")
|
"Use flags instead (e.g. --cd, --iface) or see 'ctrld start --help' for all options")
|
||||||
@@ -1280,3 +1277,11 @@ func initServicesCmd(commands ...*cobra.Command) *cobra.Command {
|
|||||||
|
|
||||||
return serviceCmd
|
return serviceCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// filterEmptyStrings removes empty strings from a slice of strings.
|
||||||
|
// It returns a new slice containing only non-empty strings.
|
||||||
|
func filterEmptyStrings(slice []string) []string {
|
||||||
|
return slices.DeleteFunc(slice, func(s string) bool {
|
||||||
|
return s == ""
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user