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
a67aea88be
commit
65a300a807
@@ -205,9 +205,7 @@ func initStartCmd() *cobra.Command {
|
||||
|
||||
NOTE: running "ctrld start" without any arguments will start already installed ctrld service.`,
|
||||
Args: func(cmd *cobra.Command, args []string) error {
|
||||
args = slices.DeleteFunc(args, func(arg string) bool {
|
||||
return arg == ""
|
||||
})
|
||||
args = filterEmptyStrings(args)
|
||||
if len(args) > 0 {
|
||||
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")
|
||||
@@ -221,6 +219,7 @@ NOTE: running "ctrld start" without any arguments will start already installed c
|
||||
sc := &service.Config{}
|
||||
*sc = *svcConfig
|
||||
osArgs := os.Args[2:]
|
||||
osArgs = filterEmptyStrings(osArgs)
|
||||
if os.Args[1] == "service" {
|
||||
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.`,
|
||||
Args: func(cmd *cobra.Command, args []string) error {
|
||||
args = slices.DeleteFunc(args, func(arg string) bool {
|
||||
return arg == ""
|
||||
})
|
||||
args = filterEmptyStrings(args)
|
||||
if len(args) > 0 {
|
||||
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")
|
||||
@@ -1280,3 +1277,11 @@ func initServicesCmd(commands ...*cobra.Command) *cobra.Command {
|
||||
|
||||
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