From 3b139b0b2ecf5882ccf5fdb0b3afdacf837021af Mon Sep 17 00:00:00 2001 From: networkop Date: Wed, 5 May 2021 18:33:34 +0100 Subject: [PATCH] linter cleanup --- clab/ceos.go | 3 ++- clab/cert.go | 4 ++-- clab/config.go | 1 - clab/crpd.go | 5 +++-- clab/file.go | 19 ++----------------- clab/graph.go | 10 +++++++--- clab/inventory_test.go | 5 ++++- clab/mysocketio.go | 5 ++++- clab/netlink.go | 4 ++-- clab/srl.go | 3 ++- clab/vr-sros.go | 3 ++- cmd/completion.go | 6 +++--- cmd/deploy.go | 5 +++-- cmd/destroy.go | 7 +++---- cmd/disableTxOffload.go | 2 +- cmd/graph.go | 2 +- cmd/mysocketio.go | 2 +- cmd/root.go | 2 +- cmd/verUpgrade.go | 2 +- cmd/vxlan.go | 6 +++--- runtime/docker/docker.go | 2 +- utils/file.go | 2 +- 22 files changed, 49 insertions(+), 51 deletions(-) diff --git a/clab/ceos.go b/clab/ceos.go index 1f4c3ed32..91e7b7bc2 100644 --- a/clab/ceos.go +++ b/clab/ceos.go @@ -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 { @@ -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 diff --git a/clab/cert.go b/clab/cert.go index aae092fb3..0c1924320 100644 --- a/clab/cert.go +++ b/clab/cert.go @@ -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) @@ -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) diff --git a/clab/config.go b/clab/config.go index 3ce3d72ab..61389afae 100644 --- a/clab/config.go +++ b/clab/config.go @@ -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 diff --git a/clab/crpd.go b/clab/crpd.go index cc660a15d..e566fe3c1 100644 --- a/clab/crpd.go +++ b/clab/crpd.go @@ -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 { @@ -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") diff --git a/clab/file.go b/clab/file.go index e8aae9b23..b4551e1d7 100644 --- a/clab/file.go +++ b/clab/file.go @@ -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" ) @@ -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) { @@ -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() @@ -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 { diff --git a/clab/graph.go b/clab/graph.go index a6170a797..41368bfc7 100644 --- a/clab/graph.go +++ b/clab/graph.go @@ -7,6 +7,7 @@ import ( "github.com/awalterschulze/gographviz" log "github.com/sirupsen/logrus" + "github.com/srl-labs/containerlab/utils" ) var g *gographviz.Graph @@ -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" @@ -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 diff --git a/clab/inventory_test.go b/clab/inventory_test.go index 510e04ca8..afc052e75 100644 --- a/clab/inventory_test.go +++ b/clab/inventory_test.go @@ -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()) diff --git a/clab/mysocketio.go b/clab/mysocketio.go index 50f5a1929..5411e16d8 100644 --- a/clab/mysocketio.go +++ b/clab/mysocketio.go @@ -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 diff --git a/clab/netlink.go b/clab/netlink.go index feffc0db4..9ec6aa8f1 100644 --- a/clab/netlink.go +++ b/clab/netlink.go @@ -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 diff --git a/clab/srl.go b/clab/srl.go index 898f27a43..e5e6c7d99 100644 --- a/clab/srl.go +++ b/clab/srl.go @@ -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 { @@ -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 { diff --git a/clab/vr-sros.go b/clab/vr-sros.go index e75111f75..f55cafb1a 100644 --- a/clab/vr-sros.go +++ b/clab/vr-sros.go @@ -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 { @@ -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 diff --git a/cmd/completion.go b/cmd/completion.go index a469b42e8..1cd5d486b 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -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) } }, } diff --git a/cmd/deploy.go b/cmd/deploy.go index af78892aa..8178734b5 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -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 @@ -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 @@ -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 { diff --git a/cmd/destroy.go b/cmd/destroy.go index 9c6f8bc77..e0891e9e5 100644 --- a/cmd/destroy.go +++ b/cmd/destroy.go @@ -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 } @@ -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() } diff --git a/cmd/disableTxOffload.go b/cmd/disableTxOffload.go index 03b3a2bf2..651d27082 100644 --- a/cmd/disableTxOffload.go +++ b/cmd/disableTxOffload.go @@ -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") } diff --git a/cmd/graph.go b/cmd/graph.go index 8070fa09c..213f1109b 100644 --- a/cmd/graph.go +++ b/cmd/graph.go @@ -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) diff --git a/cmd/mysocketio.go b/cmd/mysocketio.go index f49b94be6..c679eede0 100644 --- a/cmd/mysocketio.go +++ b/cmd/mysocketio.go @@ -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 diff --git a/cmd/root.go b/cmd/root.go index c7ec0a112..c711b8b69 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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") diff --git a/cmd/verUpgrade.go b/cmd/verUpgrade.go index 74958e8f3..6f005fcdd 100644 --- a/cmd/verUpgrade.go +++ b/cmd/verUpgrade.go @@ -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 diff --git a/cmd/vxlan.go b/cmd/vxlan.go index fda482142..8307870c3 100644 --- a/cmd/vxlan.go +++ b/cmd/vxlan.go @@ -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") } diff --git a/runtime/docker/docker.go b/runtime/docker/docker.go index c044b0098..d107f06a0 100644 --- a/runtime/docker/docker.go +++ b/runtime/docker/docker.go @@ -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 diff --git a/utils/file.go b/utils/file.go index 09ddda0c6..67dff2061 100644 --- a/utils/file.go +++ b/utils/file.go @@ -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) } }