Skip to content

Commit

Permalink
fixes for subcommands of server (start, stop, status)
Browse files Browse the repository at this point in the history
  • Loading branch information
George Pantazis committed May 7, 2024
1 parent f5d73da commit c3d2379
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 32 deletions.
82 changes: 54 additions & 28 deletions commandHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ func handleCommand(args []string, context string, commands map[string]func([]str
if cmd, found := commands[args[argPos]]; found {
cmd(args)
} else {
if cmd, found := commands[args[argPos+1]]; found {
cmd(args[argPos+1:])
} else {
fmt.Printf("Unknown %s subcommand: %s\n", context, args[argPos+1])
// fmt.Println(commands)
if len(args) > argPos+1 {
if cmd, found := commands[args[argPos+1]]; found {
cmd(args[argPos+1:])
} else {
fmt.Printf("Unknown %s subcommand: %s\n", context, args[argPos+1])
}
}
}
}
Expand Down Expand Up @@ -78,9 +81,9 @@ func handleDNS(args []string, currentContext string) {

func handleServer(args []string, currentContext string) {
commands := map[string]func([]string){
"start": func(args []string) { /* restartDNSServer(dnsServerSettings.DNSPort) */ },
"stop": func(args []string) { /* stopDNSServer() */ },
"status": func(args []string) { fmt.Println("Server Status: ", getServerStatus()) },
"start": func(args []string) {},
"stop": func(args []string) {},
"status": func(args []string) {},
"configure": func(args []string) { /* config(args) */ },
"load": func(args []string) { dnsServerSettings = loadSettings() },
"save": func(args []string) { saveSettings(dnsServerSettings) },
Expand All @@ -107,7 +110,24 @@ func handleServerStop(args []string, currentContext string) {
"api": func(args []string) { /* stopGinAPI() */ },
"dhcp": func(args []string) { /* startDHCP() */ },
}
handleCommand(args, "start", commands)
handleCommand(args, "stop", commands)
}

func handleServerStatus(args []string, currentContext string) {
args = args[1:]
commands := map[string]func([]string){
"dns": func(args []string) { fmt.Println("DNS Server Status: ", getServerStatus()) },
"mdns": func(args []string) { /* stopMDNSServer() */ },
"api": func(args []string) { /* stopGinAPI() */ },
"dhcp": func(args []string) { /* startDHCP() */ },
}
handleCommand(args, "status", commands)
}

func handleServerConfigure(args []string, currentContext string) {
args = args[1:]
commands := map[string]func([]string){}
handleCommand(args, "configure", commands)
}

func setupAutocomplete(rl *readline.Instance, context string) {
Expand Down Expand Up @@ -153,12 +173,19 @@ func setupAutocomplete(rl *readline.Instance, context string) {
readline.PcItem("dhcp"),
),
readline.PcItem("stop",
readline.PcItem("all"),
readline.PcItem("dns"),
readline.PcItem("mdns"),
readline.PcItem("api"),
readline.PcItem("dhcp"),
),
readline.PcItem("status",
readline.PcItem("all"),
readline.PcItem("dns"),
readline.PcItem("mdns"),
readline.PcItem("api"),
readline.PcItem("dhcp"),
),
readline.PcItem("status"),
readline.PcItem("configure"),
readline.PcItem("load"),
readline.PcItem("save"),
Expand Down Expand Up @@ -204,26 +231,25 @@ func setupAutocomplete(rl *readline.Instance, context string) {
)
case "server":
rl.Config.AutoComplete = readline.NewPrefixCompleter(
readline.PcItem("server",
readline.PcItem("start",
readline.PcItem("dns"),
readline.PcItem("mdns"),
readline.PcItem("api"),
readline.PcItem("dhcp"),
),
readline.PcItem("stop",
readline.PcItem("dns"),
readline.PcItem("mdns"),
readline.PcItem("api"),
readline.PcItem("dhcp"),
),
readline.PcItem("status"),
readline.PcItem("configure"),
readline.PcItem("load"),
readline.PcItem("save"),
readline.PcItem("?"),
readline.PcItem("start",
readline.PcItem("dns"),
readline.PcItem("mdns"),
readline.PcItem("api"),
readline.PcItem("dhcp"),
),
readline.PcItem("stop",
readline.PcItem("dns"),
readline.PcItem("mdns"),
readline.PcItem("api"),
readline.PcItem("dhcp"),
),
readline.PcItem("status",
readline.PcItem("all"),
readline.PcItem("dns"),
readline.PcItem("mdns"),
readline.PcItem("api"),
readline.PcItem("dhcp"),
),
readline.PcItem("status"),
readline.PcItem("configure"),
readline.PcItem("load"),
readline.PcItem("save"),
Expand Down
14 changes: 10 additions & 4 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ func handleGlobalCommands(args []string, rl *readline.Instance, currentContext *
case "record", "cache", "dns":
handleContextCommand(args[0], args, rl, currentContext)
case "server":
if args[1] == "start" || args[1] == "stop" {
handleContextCommand(args[1], args, rl, currentContext)
} else {
handleContextCommand(args[0], args, rl, currentContext)
if len(args) > 1 {
switch args[1] {
case "start", "stop", "status", "configure":
handleContextCommand(args[1], args, rl, currentContext)
}
}
handleContextCommand(args[0], args, rl, currentContext)
case "help", "h", "?", "ls", "l":
mainHelp()
default:
Expand Down Expand Up @@ -116,6 +118,10 @@ func handleSubcommand(command string, args []string, context string) {
handleServerStart(args, context)
case "stop":
handleServerStop(args, context)
case "status":
handleServerStatus(args, context)
case "configure":
handleServerConfigure(args, context)
default:
fmt.Println("Unknown subcommand:", args[0])
}
Expand Down

0 comments on commit c3d2379

Please sign in to comment.