Skip to content

Commit 71f5e17

Browse files
authored
Merge pull request #13 from carlmontanari/scrapligo-v1.0.0
update to work with scrapligo/cfg v1.0.0
2 parents 03b37e9 + eec9104 commit 71f5e17

File tree

8 files changed

+105
-89
lines changed

8 files changed

+105
-89
lines changed

.github/workflows/cicd.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,36 @@ env:
1414

1515
jobs:
1616
unit-test:
17-
runs-on: ubuntu-20.04
17+
runs-on: ubuntu-latest
1818
steps:
19-
- uses: actions/checkout@v2
20-
- uses: actions/setup-go@v2
19+
- uses: actions/checkout@v3
20+
- uses: actions/setup-go@v3
2121
with:
2222
go-version: ${{ env.GOVER }}
2323
- run: go test -cover ./...
2424

2525
lint:
26-
runs-on: ubuntu-20.04
26+
runs-on: ubuntu-latest
2727
steps:
28-
- uses: actions/checkout@v2
28+
- uses: actions/checkout@v3
2929
- name: set up go lint
30-
uses: golangci/golangci-lint-action@v2
30+
uses: golangci/golangci-lint-action@v3
3131
with:
32-
version: v1.43.0
32+
version: v1.46.2
3333

3434
build-and-release:
35-
runs-on: ubuntu-20.04
35+
runs-on: ubuntu-latest
3636
if: startsWith(github.ref, 'refs/tags/v')
3737
needs:
3838
- unit-test
3939
- lint
4040
steps:
4141
- name: Checkout
42-
uses: actions/checkout@v2
42+
uses: actions/checkout@v3
4343
with:
4444
fetch-depth: 0
4545
- name: Set up Go
46-
uses: actions/setup-go@v2
46+
uses: actions/setup-go@v3
4747
with:
4848
go-version: ${{ env.GOVER }}
4949
- name: Run GoReleaser

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,6 @@ run:
111111
# golangci.com configuration
112112
# https://github.com/golangci/golangci/wiki/Configuration
113113
service:
114-
golangci-lint-version: 1.43.x # use the fixed version to not introduce new linters unexpectedly
114+
golangci-lint-version: 1.46.x # use the fixed version to not introduce new linters unexpectedly
115115
prepare:
116116
- echo "here I can run custom commands, but no preparation needed for this repo"

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
lint:
22
docker run -it --rm -v $$(pwd):/work ghcr.io/hellt/golines:0.8.0 golines -w .
3-
docker run -it --rm -v $$(pwd):/app -w /app golangci/golangci-lint:v1.43.0 golangci-lint run -v
3+
docker run -it --rm -v $$(pwd):/app -w /app golangci/golangci-lint:v1.46.2 golangci-lint run -v

commando/cmdo.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"os"
77
"sync"
88

9-
"github.com/scrapli/scrapligo/cfg"
9+
"github.com/scrapli/scrapligocfg/response"
10+
11+
"github.com/scrapli/scrapligocfg"
1012

1113
"github.com/scrapli/scrapligo/driver/network"
1214
log "github.com/sirupsen/logrus"
@@ -149,7 +151,11 @@ func (app *appCfg) run() error {
149151
return nil
150152
}
151153

152-
func runCfgGetConfig(name string, c *cfg.Cfg, op *cfgOperation) (*cfg.Response, error) {
154+
func runCfgGetConfig(
155+
name string,
156+
c *scrapligocfg.Cfg,
157+
op *cfgOperation,
158+
) (*response.Response, error) {
153159
source := "running"
154160
if op.Source != "" {
155161
source = op.Source
@@ -165,10 +171,10 @@ func runCfgGetConfig(name string, c *cfg.Cfg, op *cfgOperation) (*cfg.Response,
165171
return r, nil
166172
}
167173

168-
func runCfgLoadConfig(name string, c *cfg.Cfg, op *cfgOperation) ([]interface{}, error) {
174+
func runCfgLoadConfig(name string, c *scrapligocfg.Cfg, op *cfgOperation) ([]interface{}, error) {
169175
var responses []interface{}
170176

171-
var r *cfg.Response
177+
var r *response.Response
172178

173179
var err error
174180

@@ -185,7 +191,7 @@ func runCfgLoadConfig(name string, c *cfg.Cfg, op *cfgOperation) ([]interface{},
185191
}
186192

187193
if op.Diff {
188-
dr, diffErr := c.DiffConfig()
194+
dr, diffErr := c.DiffConfig("running")
189195
if diffErr != nil {
190196
log.Errorf("diff-config operation failed for device %s; error: %+v\n", name, diffErr)
191197

@@ -221,7 +227,7 @@ func runCfg(name string, d *device, driver *network.Driver) ([]interface{}, erro
221227
return nil, nil
222228
}
223229

224-
c, err := cfg.NewCfgDriver(driver, d.Platform)
230+
c, err := scrapligocfg.NewCfg(driver, d.Platform)
225231
if err != nil {
226232
log.Errorf("failed to create cfg connection for device %s; error: %+v\n", name, err)
227233

commando/conn.go

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,57 @@
11
package commando
22

33
import (
4-
"github.com/scrapli/scrapligo/driver/base"
5-
"github.com/scrapli/scrapligo/driver/core"
64
"github.com/scrapli/scrapligo/driver/network"
5+
"github.com/scrapli/scrapligo/driver/options"
6+
"github.com/scrapli/scrapligo/platform"
77
"github.com/scrapli/scrapligo/transport"
8+
"github.com/scrapli/scrapligo/util"
89
log "github.com/sirupsen/logrus"
9-
"github.com/srl-labs/srlinux-scrapli"
1010
)
1111

1212
func (app *appCfg) validTransport(t string) bool {
1313
switch t {
14-
case transport.SystemTransportName:
14+
case transport.SystemTransport:
1515
return true
16-
case transport.StandardTransportName:
16+
case transport.StandardTransport:
1717
return true
18-
case transport.TelnetTransportName:
18+
case transport.TelnetTransport:
1919
return true
2020
default:
2121
return false
2222
}
2323
}
2424

25-
func (app *appCfg) loadCredentials(o []base.Option, c string) ([]base.Option, error) {
25+
func (app *appCfg) loadCredentials(o []util.Option, c string) ([]util.Option, error) {
2626
creds, ok := app.credentials[c]
2727
if !ok {
2828
return o, errInvalidCredentialsName
2929
}
3030

3131
if creds.Username != "" {
32-
o = append(o, base.WithAuthUsername(creds.Username))
32+
o = append(o, options.WithAuthUsername(creds.Username))
3333
}
3434

3535
if creds.Password != "" {
36-
o = append(o, base.WithAuthPassword(creds.Password))
36+
o = append(o, options.WithAuthPassword(creds.Password))
3737
}
3838

3939
if creds.SecondaryPassword != "" {
40-
o = append(o, base.WithAuthSecondary(creds.SecondaryPassword))
40+
o = append(o, options.WithAuthSecondary(creds.SecondaryPassword))
4141
}
4242

4343
if creds.PrivateKey != "" {
44-
o = append(o, base.WithAuthPrivateKey(creds.PrivateKey))
44+
o = append(o, options.WithAuthPrivateKey(creds.PrivateKey, ""))
4545
}
4646

4747
return o, nil
4848
}
4949

50-
func (app *appCfg) loadTransport(o []base.Option, t string) ([]base.Option, error) {
51-
// default to strict key false and standard transport, so load those into options first
50+
func (app *appCfg) loadTransport(o []util.Option, t string) ([]util.Option, error) {
51+
// default to standard transport, so load those into options first
5252
o = append(
5353
o,
54-
base.WithTransportType(transport.StandardTransportName),
55-
base.WithAuthStrictKey(false),
54+
options.WithTransportType(transport.StandardTransport),
5655
)
5756

5857
transp, ok := app.transports[t]
@@ -66,31 +65,31 @@ func (app *appCfg) loadTransport(o []base.Option, t string) ([]base.Option, erro
6665
}
6766

6867
if transp.Port != 0 {
69-
o = append(o, base.WithPort(transp.Port))
68+
o = append(o, options.WithPort(transp.Port))
7069
}
7170

72-
if transp.StrictKey {
73-
o = append(o, base.WithAuthStrictKey(transp.StrictKey))
71+
if !transp.StrictKey {
72+
o = append(o, options.WithAuthNoStrictKey())
7473
}
7574

7675
if transp.SSHConfigFile != "" {
77-
o = append(o, base.WithSSHConfigFile(transp.SSHConfigFile))
76+
o = append(o, options.WithSSHConfigFile(transp.SSHConfigFile))
7877
}
7978

8079
if transp.TransportType != "" {
8180
if !app.validTransport(transp.TransportType) {
8281
return nil, errInvalidTransport
8382
}
8483

85-
o = append(o, base.WithTransportType(transp.TransportType))
84+
o = append(o, options.WithTransportType(transp.TransportType))
8685
}
8786

8887
return o, nil
8988
}
9089

9190
// loadOptions loads options from the provided inventory.
92-
func (app *appCfg) loadOptions(d *device) ([]base.Option, error) {
93-
var o []base.Option
91+
func (app *appCfg) loadOptions(d *device) ([]util.Option, error) {
92+
var o []util.Option
9493

9594
var err error
9695

@@ -133,22 +132,19 @@ func (app *appCfg) openCoreConn(name string, d *device) (*network.Driver, error)
133132
return nil, err
134133
}
135134

136-
switch d.Platform {
137-
case "nokia_srlinux":
138-
driver, err = srlinux.NewSRLinuxDriver(
139-
d.Address,
140-
o...,
141-
)
142-
default:
143-
driver, err = core.NewCoreDriver(
144-
d.Address,
145-
d.Platform,
146-
o...,
147-
)
135+
plat, err := platform.NewPlatform(
136+
d.Platform,
137+
d.Address,
138+
o...,
139+
)
140+
if err != nil {
141+
log.Errorf("failed to create platform instance for device %s; error: %+v\n", err, name)
142+
return nil, err
148143
}
149144

145+
driver, err = plat.GetNetworkDriver()
150146
if err != nil {
151-
log.Errorf("failed to create driver for device %s; error: %+v\n", err, name)
147+
log.Errorf("failed to create driver instance for device %s; error: %+v\n", err, name)
152148
return nil, err
153149
}
154150

commando/respwriter.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import (
77
"strings"
88
"time"
99

10-
"github.com/scrapli/scrapligo/driver/base"
11-
12-
"github.com/scrapli/scrapligo/cfg"
10+
"github.com/scrapli/scrapligo/response"
11+
cfgresponse "github.com/scrapli/scrapligocfg/response"
1312

1413
"github.com/fatih/color"
1514
)
@@ -62,27 +61,27 @@ func (w *consoleWriter) writeSuccess(r []interface{}, name string) error {
6261

6362
for _, mr := range r {
6463
switch respObj := mr.(type) {
65-
case *base.MultiResponse:
64+
case *response.MultiResponse:
6665
for _, resp := range respObj.Responses {
6766
c := color.New(color.Bold)
68-
c.Fprintf(os.Stderr, "\n-- %s:\n", resp.ChannelInput)
67+
c.Fprintf(os.Stderr, "\n-- %s:\n", resp.Input)
6968

7069
if resp.Failed != nil {
7170
color.Set(color.FgRed)
7271
}
7372

7473
fmt.Println(resp.Result)
7574
}
76-
case *cfg.Response:
75+
case *cfgresponse.Response:
7776
c := color.New(color.Bold)
78-
c.Fprintf(os.Stderr, "\n-- cfg-%s:\n", respObj.OperationType)
77+
c.Fprintf(os.Stderr, "\n-- cfg-%s:\n", respObj.Op)
7978

8079
if respObj.Failed != nil {
8180
color.Set(color.FgRed)
8281
}
8382

8483
fmt.Println(respObj.Result)
85-
case *cfg.DiffResponse:
84+
case *cfgresponse.DiffResponse:
8685
c := color.New(color.Bold)
8786
c.Fprint(os.Stderr, "\n-- cfg-DiffConfig:\n")
8887

@@ -118,26 +117,26 @@ func (w *fileWriter) WriteResponse(r []interface{}, name string) error {
118117

119118
for _, mr := range r {
120119
switch respObj := mr.(type) {
121-
case *base.MultiResponse:
120+
case *response.MultiResponse:
122121
for _, resp := range respObj.Responses {
123-
c := sanitizeCmd(resp.ChannelInput)
122+
c := sanitizeCmd(resp.Input)
124123

125124
rb := []byte(resp.Result)
126125
if err := os.WriteFile(path.Join(outDir, c), rb, filePermissions); err != nil {
127126
return err
128127
}
129128
}
130-
case *cfg.Response:
129+
case *cfgresponse.Response:
131130
rb := []byte(respObj.Result)
132-
if err := os.WriteFile(path.Join(outDir, respObj.OperationType), rb, filePermissions); err != nil {
131+
if err := os.WriteFile(path.Join(outDir, respObj.Op), rb, filePermissions); err != nil {
133132
return err
134133
}
135-
case *cfg.DiffResponse:
134+
case *cfgresponse.DiffResponse:
136135
rb := []byte(
137136
fmt.Sprintf("Device Diff:\n%s\n\nSide By Side Diff:\n%s\n\nUnified Diff:\n%s",
138137
respObj.DeviceDiff, respObj.SideBySideDiff(), respObj.UnifiedDiff()),
139138
)
140-
if err := os.WriteFile(path.Join(outDir, respObj.OperationType), rb, filePermissions); err != nil {
139+
if err := os.WriteFile(path.Join(outDir, respObj.Op), rb, filePermissions); err != nil {
141140
return err
142141
}
143142
}

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ module github.com/hellt/cmdo
33
go 1.16
44

55
require (
6-
github.com/carlmontanari/difflibgo v0.0.0-20210718194309-31b9e131c298 // indirect
76
github.com/fatih/color v1.12.0
8-
github.com/scrapli/scrapligo v0.1.2
7+
github.com/scrapli/scrapligo v1.0.2
8+
github.com/scrapli/scrapligocfg v1.0.0
99
github.com/sirupsen/logrus v1.8.1
10-
github.com/srl-labs/srlinux-scrapli v0.4.1
1110
github.com/urfave/cli/v2 v2.3.0
1211
gopkg.in/yaml.v2 v2.4.0
1312
)

0 commit comments

Comments
 (0)