Skip to content

Commit

Permalink
refactor: improve dutctl output format
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Drenhaus <[email protected]>
  • Loading branch information
jensdrenhaus committed Oct 17, 2024
1 parent 5e44ea2 commit 276b551
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
26 changes: 23 additions & 3 deletions cmds/dutctl/dutctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,17 @@ func (app *application) start() {

fmt.Fprintf(app.stdout, "Calling List-RPC\non dutagent %s\n",
app.serverAddr)
app.printLine()

err := app.listRPC()
app.exit(err)
}

if len(app.args) == 1 {
device := app.args[0]
fmt.Fprintf(app.stdout, "Calling Commands-RPC with\ndevice=%q\non dutagent %s\n",
fmt.Fprintf(app.stdout, "Calling Commands-RPC with\ndevice: %q\non dutagent %s\n",
device, app.serverAddr)
app.printLine()

err := app.commandsRPC(device)
app.exit(err)
Expand All @@ -154,15 +156,17 @@ func (app *application) start() {
cmdArgs := app.args[2:]

if len(cmdArgs) > 0 && cmdArgs[0] == "help" {
fmt.Fprintf(app.stdout, "Calling Details-RPC with\ndevice=%q\ncommand=%q\nkeyword=%q\non dutagent %s\n",
fmt.Fprintf(app.stdout, "Calling Details-RPC with\ndevice: %q, command: %q, keyword: %q\non dutagent %s\n",
device, command, "help", app.serverAddr)
app.printLine()

err := app.detailsRPC(device, command, "help")
app.exit(err)
}

fmt.Fprintf(app.stdout, "Calling Run-RPC with\ndevice=%q\ncommand=%q\ncmdArgs=%q\non dutagent %s\n",
fmt.Fprintf(app.stdout, "Calling Run-RPC with\ndevice: %q, command: %q, cmdArgs: %q\non dutagent %s\n",
device, command, cmdArgs, app.serverAddr)
app.printLine()

err := app.runRPC(device, command, cmdArgs)
app.exit(err)
Expand All @@ -187,6 +191,22 @@ func (app *application) exit(err error) {
app.exitFunc(1)
}

func (app *application) printLine() {
const length = 80

for range length {
fmt.Fprint(app.stdout, "-")
}

fmt.Fprint(app.stdout, "\n")
}

func (app *application) printList(items []string) {
for _, i := range items {
fmt.Fprintf(app.stdout, "- %s\n", i)
}
}

func main() {
newApp(os.Stdin, os.Stdout, os.Stderr, os.Exit, os.Args).start()
}
4 changes: 2 additions & 2 deletions cmds/dutctl/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (app *application) listRPC() error {
return err
}

fmt.Fprintln(app.stdout, res.Msg.GetDevices())
app.printList(res.Msg.GetDevices())

return nil
}
Expand All @@ -43,7 +43,7 @@ func (app *application) commandsRPC(device string) error {
return err
}

fmt.Fprintln(app.stdout, res.Msg.GetCommands())
app.printList(res.Msg.GetCommands())

return nil
}
Expand Down

0 comments on commit 276b551

Please sign in to comment.