Skip to content

Commit

Permalink
goreportcard.
Browse files Browse the repository at this point in the history
  • Loading branch information
udhos committed Nov 18, 2016
1 parent 0289430 commit 934c092
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 78 deletions.
2 changes: 2 additions & 0 deletions README.md
@@ -1,3 +1,5 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/udhos/jazigo)](https://goreportcard.com/report/github.com/udhos/jazigo)

About Jazigo
=============

Expand Down
17 changes: 15 additions & 2 deletions build.sh
Expand Up @@ -3,9 +3,10 @@
src=`find . -type f | egrep '\.go$'`

gofmt -s -w $src
go tool vet $src
go tool fix $src
go install github.com/udhos/jazigo/jazigo
go tool vet .
pkg=github.com/udhos/jazigo
go install $pkg/jazigo

# go get honnef.co/go/simple/cmd/gosimple
s=$GOPATH/bin/gosimple
Expand All @@ -19,5 +20,17 @@ simple() {
}
[ -x "$s" ] && simple

# go get github.com/golang/lint/golint
l=$GOPATH/bin/golint
lint() {
# golint cant handle source files from multiple packages
$l jazigo/*.go
$l conf/*.go
$l dev/*.go
$l store/*.go
$l temp/*.go
}
[ -x "$l" ] && lint

go test github.com/udhos/jazigo/dev
go test github.com/udhos/jazigo/store
36 changes: 18 additions & 18 deletions dev/model.go
Expand Up @@ -102,14 +102,14 @@ func NewDevice(logger hasPrintf, mod *Model, id, hostPort, transports, loginUser
}

const (
FETCH_ERR_NONE = 0
FETCH_ERR_GETDEV = 1
FETCH_ERR_TRANSP = 2
FETCH_ERR_LOGIN = 3
FETCH_ERR_ENABLE = 4
FETCH_ERR_PAGER = 5
FETCH_ERR_COMMANDS = 6
FETCH_ERR_SAVE = 7
fetchErrNone = 0
fetchErrGetDev = 1
fetchErrTransp = 2
fetchErrLogin = 3
fetchErrEnable = 4
fetchErrPager = 5
fetchErrCommands = 6
fetchErrSave = 7
)

type FetchRequest struct {
Expand Down Expand Up @@ -145,7 +145,7 @@ func (d *Device) Fetch(tab DeviceUpdater, logger hasPrintf, resultCh chan FetchR

result := d.fetch(logger, delay, repository, opt.MaxConfigFiles, ft)

good := result.Code == FETCH_ERR_NONE
good := result.Code == fetchErrNone

updateDeviceStatus(tab, d.Id, good, time.Now(), logger, opt.Holdtime)

Expand All @@ -166,7 +166,7 @@ func (d *Device) fetch(logger hasPrintf, delay time.Duration, repository string,

session, transport, logged, err := openTransport(logger, modelName, d.Id, d.HostPort, d.Transports, d.LoginUser, d.LoginPassword)
if err != nil {
return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Msg: fmt.Sprintf("fetch transport: %v", err), Code: FETCH_ERR_TRANSP, Begin: begin}
return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Msg: fmt.Sprintf("fetch transport: %v", err), Code: fetchErrTransp, Begin: begin}
}

defer session.Close()
Expand All @@ -180,7 +180,7 @@ func (d *Device) fetch(logger hasPrintf, delay time.Duration, repository string,
if d.Attr.NeedLoginChat && !logged {
e, loginErr := d.login(logger, session, &capture)
if loginErr != nil {
return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Msg: fmt.Sprintf("fetch login: %v", loginErr), Code: FETCH_ERR_LOGIN, Begin: begin}
return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Msg: fmt.Sprintf("fetch login: %v", loginErr), Code: fetchErrLogin, Begin: begin}
}
if e {
enabled = true
Expand All @@ -190,27 +190,27 @@ func (d *Device) fetch(logger hasPrintf, delay time.Duration, repository string,
if d.Attr.NeedEnabledMode && !enabled {
enableErr := d.enable(logger, session, &capture)
if enableErr != nil {
return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Msg: fmt.Sprintf("fetch enable: %v", enableErr), Code: FETCH_ERR_ENABLE, Begin: begin}
return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Msg: fmt.Sprintf("fetch enable: %v", enableErr), Code: fetchErrEnable, Begin: begin}
}
}

if d.Attr.NeedPagingOff {
pagingErr := d.pagingOff(logger, session, &capture)
if pagingErr != nil {
return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Msg: fmt.Sprintf("fetch pager off: %v", pagingErr), Code: FETCH_ERR_PAGER, Begin: begin}
return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Msg: fmt.Sprintf("fetch pager off: %v", pagingErr), Code: fetchErrPager, Begin: begin}
}
}

if cmdErr := d.sendCommands(logger, session, &capture); cmdErr != nil {
d.saveRollback(logger, &capture)
return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Msg: fmt.Sprintf("commands: %v", cmdErr), Code: FETCH_ERR_COMMANDS, Begin: begin}
return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Msg: fmt.Sprintf("commands: %v", cmdErr), Code: fetchErrCommands, Begin: begin}
}

if saveErr := d.saveCommit(logger, &capture, repository, maxFiles, ft); saveErr != nil {
return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Msg: fmt.Sprintf("save commit: %v", saveErr), Code: FETCH_ERR_SAVE, Begin: begin}
return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Msg: fmt.Sprintf("save commit: %v", saveErr), Code: fetchErrSave, Begin: begin}
}

return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Code: FETCH_ERR_NONE, Begin: begin}
return FetchResult{Model: modelName, DevId: d.Id, DevHostPort: d.HostPort, Transport: transport, Code: fetchErrNone, Begin: begin}
}

func (d *Device) saveRollback(logger hasPrintf, capture *dialog) {
Expand Down Expand Up @@ -354,7 +354,7 @@ READ_LOOP:
d.logf("debug recv: EOF")
}
eof = true // EOF is normal termination for SSH transport
case TELNET_NEG:
case telnetNegOnly:
if d.Debug {
d.logf("debug recv: telnetNegotiationOnly")
}
Expand Down Expand Up @@ -678,7 +678,7 @@ func round(val float64) int {
func ClearDeviceStatus(tab DeviceUpdater, devId string, logger hasPrintf, holdtime time.Duration) (*Device, error) {
d, getErr := tab.GetDevice(devId)
if getErr != nil {
logger.Printf("ClearDeviceStatus: '%s' not found: %v", getErr)
logger.Printf("ClearDeviceStatus: '%s' not found: %v", devId, getErr)
return nil, getErr
}

Expand Down
6 changes: 3 additions & 3 deletions dev/model_cisco_iosxr_test.go
Expand Up @@ -36,7 +36,7 @@ func TestCiscoIOSXR1(t *testing.T) {
RegisterModels(logger, tab)
CreateDevice(tab, logger, "cisco-iosxr", "lab1", "localhost"+addr, "telnet", "lab", "pass", "en", false, nil)

repo := temp.TempRepo()
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()

requestCh := make(chan FetchRequest)
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestCiscoIOSXR2(t *testing.T) {
RegisterModels(logger, tab)
CreateDevice(tab, logger, "cisco-iosxr", "lab1", "localhost"+addr, "telnet", "lab", "pass", "en", false, nil)

repo := temp.TempRepo()
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()

requestCh := make(chan FetchRequest)
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestCiscoIOSXR3(t *testing.T) {
RegisterModels(logger, tab)
CreateDevice(tab, logger, "cisco-iosxr", "lab1", "localhost"+addr, "telnet", "lab", "pass", "en", false, nil)

repo := temp.TempRepo()
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()

requestCh := make(chan FetchRequest)
Expand Down
8 changes: 4 additions & 4 deletions dev/model_cisco_test.go
Expand Up @@ -54,7 +54,7 @@ func TestCiscoIOS1(t *testing.T) {
RegisterModels(logger, tab)
CreateDevice(tab, logger, "cisco-ios", "lab1", "localhost"+addr, "telnet", "lab", "pass", "en", false, nil)

repo := temp.TempRepo()
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()

requestCh := make(chan FetchRequest)
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestCiscoIOS2(t *testing.T) {
RegisterModels(logger, tab)
CreateDevice(tab, logger, "cisco-ios", "lab1", "localhost"+addr, "telnet", "lab", "pass", "en", false, nil)

repo := temp.TempRepo()
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()

requestCh := make(chan FetchRequest)
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestCiscoIOS3(t *testing.T) {
RegisterModels(logger, tab)
CreateDevice(tab, logger, "cisco-ios", "lab1", "localhost"+addr, "telnet", "lab", "pass", "en", false, nil)

repo := temp.TempRepo()
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()

requestCh := make(chan FetchRequest)
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestCiscoIOS4(t *testing.T) {
CreateDevice(tab, logger, "cisco-ios", fmt.Sprintf("lab%02d", i), "localhost"+addr, "telnet", "lab", "pass", "en", false, nil)
}

repo := temp.TempRepo()
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()

requestCh := make(chan FetchRequest)
Expand Down
2 changes: 1 addition & 1 deletion dev/model_http_test.go
Expand Up @@ -30,7 +30,7 @@ func TestHTTP1(t *testing.T) {
RegisterModels(logger, tab)
CreateDevice(tab, logger, "http", "lab1", "localhost"+addr, "", "", "", "", false, nil)

repo := temp.TempRepo()
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()

requestCh := make(chan FetchRequest)
Expand Down
4 changes: 2 additions & 2 deletions dev/model_junos_test.go
Expand Up @@ -31,7 +31,7 @@ func TestJuniperJunOS1(t *testing.T) {
RegisterModels(logger, tab)
CreateDevice(tab, logger, "junos", "lab1", "localhost"+addr, "telnet", "lab", "pass", "en", false, nil)

repo := temp.TempRepo()
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()

requestCh := make(chan FetchRequest)
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestJuniperJunOS2(t *testing.T) {
RegisterModels(logger, tab)
CreateDevice(tab, logger, "junos", "lab1", "localhost"+addr, "telnet", "lab", "pass", "en", false, nil)

repo := temp.TempRepo()
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()

requestCh := make(chan FetchRequest)
Expand Down
6 changes: 3 additions & 3 deletions dev/scan.go
Expand Up @@ -25,7 +25,7 @@ func Spawner(tab DeviceUpdater, logger hasPrintf, reqChan chan FetchRequest, rep
d, getErr := tab.GetDevice(devId)
if getErr != nil {
if replyChan != nil {
replyChan <- FetchResult{DevId: devId, Msg: fmt.Sprintf("Spawner: could not find device: %v", getErr), Code: FETCH_ERR_GETDEV, Begin: time.Now()}
replyChan <- FetchResult{DevId: devId, Msg: fmt.Sprintf("Spawner: could not find device: %v", getErr), Code: fetchErrGetDev, Begin: time.Now()}
}
continue
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func Scan(tab DeviceUpdater, devices []*Device, logger hasPrintf, opt *conf.AppC
elap := end.Sub(r.Begin)
logger.Printf("Scan: recv %s %s %s %s msg=[%s] code=%d wait=%d remain=%d skipped=%d elap=%s", r.Model, r.DevId, r.DevHostPort, r.Transport, r.Msg, r.Code, wait, deviceCount-nextDevice, skipped, elap)

good := r.Code == FETCH_ERR_NONE
good := r.Code == fetchErrNone

if good {
success++
Expand All @@ -122,7 +122,7 @@ func Scan(tab DeviceUpdater, devices []*Device, logger hasPrintf, opt *conf.AppC
func updateDeviceStatus(tab DeviceUpdater, devId string, good bool, last time.Time, logger hasPrintf, holdtime time.Duration) {
d, getErr := tab.GetDevice(devId)
if getErr != nil {
logger.Printf("updateDeviceStatus: '%s' not found: %v", getErr)
logger.Printf("updateDeviceStatus: '%s' not found: %v", devId, getErr)
return
}

Expand Down
6 changes: 3 additions & 3 deletions dev/telnet.go
Expand Up @@ -23,10 +23,10 @@ func shift(b []byte, size, offset int) int {

type telnetNegotiationOnly struct{}

var TELNET_NEG = telnetNegotiationOnly{}
var telnetNegOnly = telnetNegotiationOnly{}

func (e telnetNegotiationOnly) Error() string {
return "telnetNegotiationOnly"
return "telnetNegotiationOnlyError"
}

func telnetNegotiation(b []byte, n int, t transp) (int, error) {
Expand Down Expand Up @@ -63,7 +63,7 @@ func telnetNegotiation(b []byte, n int, t transp) (int, error) {
}

if n == 0 && hitNeg {
return 0, TELNET_NEG
return 0, telnetNegOnly
}

return n, nil
Expand Down
2 changes: 2 additions & 0 deletions jazigo/logg.go
Expand Up @@ -19,6 +19,7 @@ type logfile struct {
logger *log.Logger
}

// NewLogfile creates a new log stream capable of automatically saving to filesystem.
func NewLogfile(prefix string, maxFiles int, maxSize int64, checkInterval time.Duration) *logfile {
l := &logfile{
logPathPrefix: prefix,
Expand Down Expand Up @@ -78,6 +79,7 @@ func (l *logfile) rotate() {
}
}

// Write implements io.Writer in order to be attached to log.New().
func (l *logfile) Write(b []byte) (int, error) {

if l.output == nil {
Expand Down
10 changes: 5 additions & 5 deletions jazigo/main.go
Expand Up @@ -48,7 +48,7 @@ type app struct {
logger *log.Logger

filterModel string
filterId string
filterID string
filterHost string

priority chan string
Expand Down Expand Up @@ -346,9 +346,9 @@ func manageDeviceList(jaz *app, imp, del, purge, list bool) error {
if imp {
jaz.logf("reading device list from stdin")

autoId := "auto"
nextId := jaz.table.FindDeviceFreeId(autoId)
valueStr := nextId[len(autoId):]
autoID := "auto"
nextID := jaz.table.FindDeviceFreeId(autoID)
valueStr := nextID[len(autoID):]
value, valErr := strconv.Atoi(valueStr)
if valErr != nil {
return fmt.Errorf("could not get free device id: %v", valErr)
Expand Down Expand Up @@ -387,7 +387,7 @@ func manageDeviceList(jaz *app, imp, del, purge, list bool) error {
}

id := f[1]
if id == autoId {
if id == autoID {
id += strconv.Itoa(value)
value++
}
Expand Down

0 comments on commit 934c092

Please sign in to comment.