mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00: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
e616091249
commit
36a7423634
@@ -207,9 +207,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")
|
||||||
@@ -223,6 +221,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:]
|
||||||
}
|
}
|
||||||
@@ -570,9 +569,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")
|
||||||
@@ -1388,3 +1385,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