Skip to content

Commit

Permalink
feat: cli for dutctl
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Drenhaus <jens.drenhaus@blindspot.software>
  • Loading branch information
jensdrenhaus committed Sep 26, 2024
1 parent 5c41ffa commit 80fc4bc
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 232 deletions.
37 changes: 37 additions & 0 deletions cmds/dutagent/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"fmt"

"connectrpc.com/connect"
"github.com/BlindspotSoftware/dutctl/pkg/dut"
)

func findCmd(list dut.Devlist, wantDev, wantCmd string) (dut.Device, dut.Command, error) {
var (
dev dut.Device
cmd dut.Command
)

dev, ok := list[wantDev]
if !ok {
e := connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("device %q does not exist", wantDev))

return dev, cmd, e
}

cmd, ok = dev.Cmds[wantCmd]
if !ok {
e := connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("device %q does not have command %q", wantDev, wantCmd))

return dev, cmd, e
}

if len(cmd.Modules) == 0 {
e := connect.NewError(connect.CodeInternal, fmt.Errorf("no modules set for command %q at device %q", wantCmd, wantDev))

return dev, cmd, e
}

return dev, cmd, nil
}
53 changes: 50 additions & 3 deletions cmds/dutagent/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"log"

"connectrpc.com/connect"
Expand Down Expand Up @@ -52,7 +52,54 @@ func (a *rpcService) Commands(

func (a *rpcService) Details(
_ context.Context,
_ *connect.Request[pb.DetailsRequest],
req *connect.Request[pb.DetailsRequest],
) (*connect.Response[pb.DetailsResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("Details RPC not implemented"))
log.Println("Server received Details request")

wantDev := req.Msg.GetDevice()
wantCmd := req.Msg.GetCmd()
keyword := req.Msg.GetKeyword()

_, cmd, err := findCmd(a.devices, wantDev, wantCmd)
if err != nil {
return nil, err
}

var (
helpStr string
foundMain bool
)

for _, module := range cmd.Modules {
if module.Config.Main {
foundMain = true
helpStr = module.Help()
}
}

if !foundMain {
e := connect.NewError(
connect.CodeInternal,
fmt.Errorf("no main module found for command %q at device %q", wantCmd, wantDev),
)

return nil, e
}

if keyword != "help" {
e := connect.NewError(
connect.CodeInvalidArgument,
fmt.Errorf("unknown keyword %q, possible values are: 'help'", keyword),
)

return nil, e
}

res := connect.NewResponse(&pb.DetailsResponse{
Details: helpStr,
})

log.Print("Details-RPC finished")

return res, nil
}
23 changes: 4 additions & 19 deletions cmds/dutagent/runRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,12 @@ func findDUTCmd(_ context.Context, args runCmdArgs) (runCmdArgs, fsm.State[runCm
wantDev := args.cmdMsg.GetDevice()
wantCmd := args.cmdMsg.GetCmd()

device, ok := args.deviceList[wantDev]
if !ok {
e := connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("device %q does not exist", wantDev))

return args, nil, e
}

cmd, ok := device.Cmds[wantCmd]
if !ok {
e := connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("device %q does not have command %q", wantDev, wantCmd))

return args, nil, e
}

if len(cmd.Modules) == 0 {
e := connect.NewError(connect.CodeInternal, fmt.Errorf("no modules set for command %q", wantCmd))

return args, nil, e
dev, cmd, err := findCmd(args.deviceList, wantDev, wantCmd)
if err != nil {
return args, nil, err
}

args.dev = device
args.dev = dev
args.cmd = cmd

return args, executeModules, nil
Expand Down
Loading

0 comments on commit 80fc4bc

Please sign in to comment.