Skip to content

Commit

Permalink
linter cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
networkop committed May 5, 2021
1 parent 012726d commit 3b139b0
Show file tree
Hide file tree
Showing 22 changed files with 49 additions and 51 deletions.
3 changes: 2 additions & 1 deletion clab/ceos.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)

func ceosPostDeploy(ctx context.Context, c *CLab, node *types.Node, lworkers uint) error {
Expand Down Expand Up @@ -91,7 +92,7 @@ func initCeosNode(c *CLab, nodeCfg NodeConfig, node *types.Node, user string, en

func (c *CLab) createCEOSFiles(node *types.Node) error {
// generate config directory
CreateDirectory(path.Join(node.LabDir, "flash"), 0777)
utils.CreateDirectory(path.Join(node.LabDir, "flash"), 0777)
cfg := path.Join(node.LabDir, "flash", "startup-config")
node.ResConfig = cfg

Expand Down
4 changes: 2 additions & 2 deletions clab/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var nodeCSRTempl string = `{
func (c *CLab) GenerateRootCa(csrRootJsonTpl *template.Template, input CaRootInput) (*Certificates, error) {
log.Info("Creating root CA")
// create root CA root directory
CreateDirectory(c.Dir.LabCARoot, 0755)
utils.CreateDirectory(c.Dir.LabCARoot, 0755)
var err error
csrBuff := new(bytes.Buffer)
err = csrRootJsonTpl.Execute(csrBuff, input)
Expand Down Expand Up @@ -136,7 +136,7 @@ func (c *CLab) GenerateCert(ca string, caKey string, csrJSONTpl *template.Templa
c.m.RLock()
defer c.m.RUnlock()

CreateDirectory(targetPath, 0755)
utils.CreateDirectory(targetPath, 0755)
var err error
csrBuff := new(bytes.Buffer)
err = csrJSONTpl.Execute(csrBuff, input)
Expand Down
1 change: 0 additions & 1 deletion clab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const (
dockerNetName = "clab"
dockerNetIPv4Addr = "172.20.20.0/24"
dockerNetIPv6Addr = "2001:172:20:20::/64"
dockerNetMTU = "1500"
srlDefaultType = "ixr6"
vrsrosDefaultType = "sr-1"
// default connection mode for vrnetlab based containers
Expand Down
5 changes: 3 additions & 2 deletions clab/crpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)

func initCrpdNode(c *CLab, nodeCfg NodeConfig, node *types.Node, user string, envs map[string]string) error {
Expand Down Expand Up @@ -38,8 +39,8 @@ func initCrpdNode(c *CLab, nodeCfg NodeConfig, node *types.Node, user string, en

func (c *CLab) createCRPDFiles(node *types.Node) error {
// create config and logs directory that will be bind mounted to crpd
CreateDirectory(path.Join(node.LabDir, "config"), 0777)
CreateDirectory(path.Join(node.LabDir, "log"), 0777)
utils.CreateDirectory(path.Join(node.LabDir, "config"), 0777)
utils.CreateDirectory(path.Join(node.LabDir, "log"), 0777)

// copy crpd config from default template or user-provided conf file
cfg := path.Join(node.LabDir, "/config/juniper.conf")
Expand Down
19 changes: 2 additions & 17 deletions clab/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -50,14 +51,6 @@ func (c *CLab) GetTopology(topo string) error {
return nil
}

func fileExists(filename string) bool {
f, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !f.IsDir()
}

// CopyFile copies a file from src to dst. If src and dst files exist, and are
// the same, then return success. Otherwise, copy the file contents from src to dst.
func copyFile(src, dst string) (err error) {
Expand Down Expand Up @@ -126,14 +119,6 @@ func createFile(file, content string) {
}
}

// CreateDirectory creates a directory by a path with a mode/permission specified by perm.
// If directory exists, the function does not do anything.
func CreateDirectory(path string, perm os.FileMode) {
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, perm)
}
}

// CreateNodeDirStructure create the directory structure and files for the lab nodes
func (c *CLab) CreateNodeDirStructure(node *types.Node) (err error) {
c.m.RLock()
Expand All @@ -143,7 +128,7 @@ func (c *CLab) CreateNodeDirStructure(node *types.Node) (err error) {
// skip creation of node directory for linux/bridge kinds
// since they don't keep any state normally
if node.Kind != "linux" && node.Kind != "bridge" {
CreateDirectory(node.LabDir, 0777)
utils.CreateDirectory(node.LabDir, 0777)
}

switch node.Kind {
Expand Down
10 changes: 7 additions & 3 deletions clab/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/awalterschulze/gographviz"
log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/utils"
)

var g *gographviz.Graph
Expand Down Expand Up @@ -66,8 +67,8 @@ func (c *CLab) GenerateGraph(topo string) error {
}

// create graph directory
CreateDirectory(c.Dir.Lab, 0755)
CreateDirectory(c.Dir.LabGraph, 0755)
utils.CreateDirectory(c.Dir.Lab, 0755)
utils.CreateDirectory(c.Dir.LabGraph, 0755)

// create graph filename
dotfile := c.Dir.LabGraph + "/" + c.TopoFile.name + ".dot"
Expand All @@ -78,7 +79,10 @@ func (c *CLab) GenerateGraph(topo string) error {

// Only try to create png
if commandExists("dot") {
generatePngFromDot(dotfile, pngfile)
err := generatePngFromDot(dotfile, pngfile)
if err != nil {
return err
}
log.Info("Created ", pngfile)
}
return nil
Expand Down
5 changes: 4 additions & 1 deletion clab/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ func TestGenerateAnsibleInventory(t *testing.T) {
}

var s strings.Builder
c.generateAnsibleInventory(&s)
err := c.generateAnsibleInventory(&s)
if err != nil {
t.Fatal(err)
}

if !cmp.Equal(s.String(), tc.want) {
t.Errorf("failed at '%s', expected\n%v, got\n%+v", name, tc.want, s.String())
Expand Down
5 changes: 4 additions & 1 deletion clab/mysocketio.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ func createMysocketTunnels(ctx context.Context, c *CLab, node *types.Node) error
cmd = []string{"/bin/sh", "-c", fmt.Sprintf("mysocketctl tunnel connect --host %s -p %d -s %s -t %s > socket-%s-%s-%d.log",
n.LongName, ms.Port, sockID, tunID, n.ShortName, ms.Stype, ms.Port)}
log.Debugf("Running mysocketio command %q", cmd)
c.Runtime.ExecNotWait(ctx, node.ContainerID, cmd)
err = c.Runtime.ExecNotWait(ctx, node.ContainerID, cmd)
if err != nil {
return err
}
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions clab/netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ func (c *CLab) CreateVirtualWiring(l *Link) (err error) {
}

if err = vA.setVethLink(); err != nil {
netlink.LinkDel(vA.Link)
_ = netlink.LinkDel(vA.Link)
return err
}
if err = vB.setVethLink(); err != nil {
netlink.LinkDel(vB.Link)
_ = netlink.LinkDel(vB.Link)
}
return err

Expand Down
3 changes: 2 additions & 1 deletion clab/srl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)

type mac struct {
Expand Down Expand Up @@ -144,7 +145,7 @@ func (c *CLab) createSRLFiles(node *types.Node) error {
// generate a config file if the destination does not exist
// if the node has a `config:` statement, the file specified in that section
// will be used as a template in nodeGenerateConfig()
CreateDirectory(path.Join(node.LabDir, "config"), 0777)
utils.CreateDirectory(path.Join(node.LabDir, "config"), 0777)
dst = path.Join(node.LabDir, "config", "config.json")
err = node.GenerateConfig(dst, defaultConfigTemplates[node.Kind])
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion clab/vr-sros.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)

func initSROSNode(c *CLab, nodeCfg NodeConfig, node *types.Node, user string, envs map[string]string) error {
Expand Down Expand Up @@ -53,7 +54,7 @@ func initSROSNode(c *CLab, nodeCfg NodeConfig, node *types.Node, user string, en

func (c *CLab) createVrSROSFiles(node *types.Node) error {
// create config directory that will be bind mounted to vrnetlab container at / path
CreateDirectory(path.Join(node.LabDir, "tftpboot"), 0777)
utils.CreateDirectory(path.Join(node.LabDir, "tftpboot"), 0777)

if node.License != "" {
// copy license file to node specific lab directory
Expand Down
6 changes: 3 additions & 3 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ fish:
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
_ = cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
_ = cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
_ = cmd.Root().GenFishCompletion(os.Stdout, true)
}
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/spf13/cobra"
"github.com/srl-labs/containerlab/clab"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)

// name of the container management network
Expand Down Expand Up @@ -68,7 +69,7 @@ var deployCmd = &cobra.Command{
if err != nil {
return err
}
destroyLab(ctx, c)
_ = destroyLab(ctx, c)
log.Infof("Removing %s directory...", c.Dir.Lab)
if err := os.RemoveAll(c.Dir.Lab); err != nil {
return err
Expand All @@ -84,7 +85,7 @@ var deployCmd = &cobra.Command{
}

log.Info("Creating lab directory: ", c.Dir.Lab)
clab.CreateDirectory(c.Dir.Lab, 0755)
utils.CreateDirectory(c.Dir.Lab, 0755)

cfssllog.Level = cfssllog.LevelError
if debug {
Expand Down
7 changes: 3 additions & 4 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ func deleteEntriesFromHostsFile(containers []types.GenericContainer, bridgeName
return err
}
for _, l := range remainingLines {
f.Write(l)
f.Write([]byte("\n"))
_, _ = f.Write(l)
_, _ = f.Write([]byte("\n"))
}
return nil
}
Expand Down Expand Up @@ -223,6 +223,5 @@ func destroyLab(ctx context.Context, c *clab.CLab) (err error) {

}
// delete container network namespaces symlinks
c.DeleteNetnsSymlinks()
return nil
return c.DeleteNetnsSymlinks()
}
2 changes: 1 addition & 1 deletion cmd/disableTxOffload.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ var disableTxOffloadCmd = &cobra.Command{
func init() {
toolsCmd.AddCommand(disableTxOffloadCmd)
disableTxOffloadCmd.Flags().StringVarP(&cntName, "container", "c", "", "container name to disable offload in")
disableTxOffloadCmd.MarkFlagRequired("container")
_ = disableTxOffloadCmd.MarkFlagRequired("container")
}
2 changes: 1 addition & 1 deletion cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ var graphCmd = &cobra.Command{
}
tmpl := template.Must(template.ParseFiles(tmpl))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl.Execute(w, topoD)
_ = tmpl.Execute(w, topoD)
})

log.Infof("Listening on %s...", srv)
Expand Down
2 changes: 1 addition & 1 deletion cmd/mysocketio.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func init() {
mysocketioCmd.AddCommand(mysocketioLoginCmd)
mysocketioLoginCmd.Flags().StringVarP(&email, "email", "e", "", "Email address")
mysocketioLoginCmd.Flags().StringVarP(&password, "password", "p", "", "Password")
mysocketioLoginCmd.MarkFlagRequired("email")
_ = mysocketioLoginCmd.MarkFlagRequired("email")
}

// vxlanCmd represents the vxlan command container
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func init() {
rootCmd.SilenceUsage = true
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable debug mode")
rootCmd.PersistentFlags().StringVarP(&topo, "topo", "t", "", "path to the file with topology information")
rootCmd.MarkPersistentFlagFilename("topo", "*.yaml", "*.yml")
_ = rootCmd.MarkPersistentFlagFilename("topo", "*.yaml", "*.yml")
rootCmd.PersistentFlags().StringVarP(&name, "name", "n", "", "lab name")
rootCmd.PersistentFlags().DurationVarP(&timeout, "timeout", "", 30*time.Second, "timeout for docker requests, e.g: 30s, 1m, 2m30s")
rootCmd.PersistentFlags().StringVarP(&rt, "runtime", "r", runtime.DockerRuntime, "container runtime")
Expand Down
2 changes: 1 addition & 1 deletion cmd/verUpgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var upgradeCmd = &cobra.Command{
if err != nil {
log.Fatalf("Failed to create temp file %s\n", err)
}
downloadFile(downloadURL, f)
_ = downloadFile(downloadURL, f)

c := exec.Command("bash", f.Name())
c.Stdout = os.Stdout
Expand Down
6 changes: 3 additions & 3 deletions cmd/vxlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func init() {
vxlanCreateCmd.Flags().StringVarP(&cntLink, "link", "l", "", "link to which 'attach' vxlan tunnel with tc redirect")
vxlanCreateCmd.Flags().IntVarP(&vxlanMTU, "mtu", "m", 1554, "VxLAN MTU")

vxlanCreateCmd.MarkFlagRequired("remote")
vxlanCreateCmd.MarkFlagRequired("id")
vxlanCreateCmd.MarkFlagRequired("link")
_ = vxlanCreateCmd.MarkFlagRequired("remote")
_ = vxlanCreateCmd.MarkFlagRequired("id")
_ = vxlanCreateCmd.MarkFlagRequired("link")

vxlanDeleteCmd.Flags().StringVarP(&delPrefix, "prefix", "p", "vx-", "delete all containerlab created VxLAN interfaces which start with this prefix")
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (c *DockerRuntime) PullImageIfRequired(ctx context.Context, imageName strin
}
defer reader.Close()
// must read from reader, otherwise image is not properly pulled
io.Copy(ioutil.Discard, reader)
_, _ = io.Copy(ioutil.Discard, reader)
log.Infof("Done pulling %s", canonicalImageName)

return nil
Expand Down
2 changes: 1 addition & 1 deletion utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func CreateFile(file, content string) {
// If directory exists, the function does not do anything.
func CreateDirectory(path string, perm os.FileMode) {
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, perm)
_ = os.MkdirAll(path, perm)
}
}

Expand Down

0 comments on commit 3b139b0

Please sign in to comment.