cmd/ctrld: separate iface variable for start/stop aliases

While at it, also fix a bug in getDNSByResolvectl, which won't return
correct DNS values if there's no "%" symbol in output.
This commit is contained in:
Cuong Manh Le
2023-01-19 22:44:32 +07:00
committed by Cuong Manh Le
parent 47c280cf1d
commit a9fabd1b79
3 changed files with 9 additions and 9 deletions

View File

@@ -388,20 +388,22 @@ func initCLI() {
Use: "start",
Short: "Quick start service and configure DNS on interface",
Run: func(cmd *cobra.Command, args []string) {
os.Args = append(os.Args, "--iface="+ifaceStartStop)
startCmd.Run(cmd, args)
},
}
startCmdAlias.Flags().StringVarP(&iface, "iface", "", "auto", `Update DNS setting for iface, "auto" means the default interface gateway`)
startCmdAlias.Flags().StringVarP(&ifaceStartStop, "iface", "", "auto", `Update DNS setting for iface, "auto" means the default interface gateway`)
startCmdAlias.Flags().AddFlagSet(startCmd.Flags())
rootCmd.AddCommand(startCmdAlias)
stopCmdAlias := &cobra.Command{
Use: "stop",
Short: "Quick stop service and remove DNS from interface",
Run: func(cmd *cobra.Command, args []string) {
os.Args = append(os.Args, "--iface="+ifaceStartStop)
stopCmd.Run(cmd, args)
},
}
stopCmdAlias.Flags().StringVarP(&iface, "iface", "", "auto", `Reset DNS setting for iface, "auto" means the default interface gateway`)
stopCmdAlias.Flags().StringVarP(&ifaceStartStop, "iface", "", "auto", `Reset DNS setting for iface, "auto" means the default interface gateway`)
stopCmdAlias.Flags().AddFlagSet(stopCmd.Flags())
rootCmd.AddCommand(stopCmdAlias)

View File

@@ -33,8 +33,9 @@ var (
mainLog = rootLogger
proxyLog = rootLogger
cdUID string
iface string
cdUID string
iface string
ifaceStartStop string
)
func main() {

View File

@@ -78,11 +78,8 @@ func getDNSByResolvectl(iface string) []string {
if err != nil {
return nil
}
parts := strings.SplitN(string(b), "%", 2)
if len(parts) != 2 {
return nil
}
parts = strings.Fields(parts[0])
parts := strings.Fields(strings.SplitN(string(b), "%", 2)[0])
if len(parts) > 2 {
return parts[3:]
}