Skip to content

Commit

Permalink
Merge pull request #134 from steiler/sendcommandschecklen
Browse files Browse the repository at this point in the history
SendCommands return early if commands len == 0
  • Loading branch information
carlmontanari committed Jun 10, 2023
2 parents a6e583b + f99da3b commit 75916b6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 0 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ linters:
- asciicheck
- bodyclose
- decorder
- depguard
- dogsled
- dupl
- dupword
Expand Down
6 changes: 6 additions & 0 deletions driver/generic/sendcommands.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package generic

import (
"fmt"

"github.com/scrapli/scrapligo/response"
"github.com/scrapli/scrapligo/util"
)
Expand All @@ -12,6 +14,10 @@ func (d *Driver) SendCommands(
) (*response.MultiResponse, error) {
d.Logger.Infof("SendCommands requested, sending '%s'", commands)

if len(commands) == 0 {
return nil, fmt.Errorf("%w: no inputs provided", util.ErrNoOp)
}

op, err := NewOperation(opts...)
if err != nil {
return nil, err
Expand Down
13 changes: 13 additions & 0 deletions driver/generic/sendcommands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,16 @@ func TestSendCommandsFromFile(t *testing.T) {
t.Run(testName, f)
}
}

func TestSendCommandsNoOp(t *testing.T) {
testName := "send-commands-no-op"

t.Logf("%s: starting", testName)

d, _ := prepareDriver(t, testName, "send-commands-simple.txt")

_, err := d.SendCommands(nil)
if err == nil {
t.Fatalf("no-op send commands did not return ErrNoOp")
}
}
3 changes: 3 additions & 0 deletions util/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ var (
ErrNetconfError = errors.New("errNetconfError")
// ErrOperationError is returned for any "operation" issues -- mostly meaning ops timeouts.
ErrOperationError = errors.New("errOperationError")
// ErrNoOp is an error returned when a "no op" event happens -- that is a SendCommands or
// SendConfigs method is called with an empty command/config slice provided.
ErrNoOp = errors.New("errNoOp")
)

0 comments on commit 75916b6

Please sign in to comment.