From e4d00323b575bced6ccf239b57252495a0c182da Mon Sep 17 00:00:00 2001 From: lauralani Date: Fri, 31 Mar 2023 20:13:55 +0200 Subject: [PATCH] fix missing error handling --- cmd/import.go | 109 ++++++++++++++++++++++++++------------------------ 1 file changed, 56 insertions(+), 53 deletions(-) diff --git a/cmd/import.go b/cmd/import.go index 3af5e27..40eba6e 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -5,72 +5,75 @@ Copyright © 2023 Laura Kalb package cmd import ( - "encoding/json" - "fmt" - "os" - "path" - "time" + "encoding/json" + "fmt" + "os" + "path" + "time" - "github.com/spf13/cobra" + "github.com/spf13/cobra" ) var importCmd = &cobra.Command{ - Use: "import", - Short: "Import ipam configuration", - Long: `Import subnets to ipam.`, - Example: "ipam import --file import.json", - Args: cobra.NoArgs, - Aliases: []string{"im"}, - Run: func(cmd *cobra.Command, args []string) { - var importname string - var subnets []Subnet + Use: "import", + Short: "Import ipam configuration", + Long: `Import subnets to ipam.`, + Example: "ipam import --file import.json", + Args: cobra.NoArgs, + Aliases: []string{"im"}, + Run: func(cmd *cobra.Command, args []string) { + var importname string + var subnets []Subnet - flagpath, _ := cmd.Flags().GetString("file") - if path.IsAbs(flagpath) { - importname = flagpath - } else { - wd, _ := os.Getwd() - importname = path.Join(wd, flagpath) - } + flagpath, _ := cmd.Flags().GetString("file") + if path.IsAbs(flagpath) { + importname = flagpath + } else { + wd, _ := os.Getwd() + importname = path.Join(wd, flagpath) + } - file, readerr := os.ReadFile(importname) - if readerr != nil { - fmt.Printf("[ERROR] Can't read file %v\n", importname) - fmt.Println(readerr) - } + file, readerr := os.ReadFile(importname) + if readerr != nil { + fmt.Printf("[ERROR] Can't read file %v\n", importname) + fmt.Println(readerr) + } - marsherr := json.Unmarshal(file, &subnets) - if marsherr != nil { - fmt.Printf("[ERROR] Invalid format for file %v\n", importname) - fmt.Println(marsherr) - } + marsherr := json.Unmarshal(file, &subnets) + if marsherr != nil { + fmt.Printf("[ERROR] Invalid format for file %v\n", importname) + fmt.Println(marsherr) + } - for _, subnet := range subnets { - fmt.Printf("[INFO] Start import of %v\n", subnet.Subnet.String()) - subnet.ChangedBy = "ipam import" - subnet.ChangedAt = time.Now() + for _, subnet := range subnets { + fmt.Printf("[INFO] Start import of %v\n", subnet.Subnet.String()) + subnet.ChangedBy = "ipam import" + subnet.ChangedAt = time.Now() - for _, addr := range subnet.Addresses { - addr.ChangedBy = "ipam import" - addr.ChangedAt = time.Now() - if addr.FQDN != "" { - AddDNSFqdn(addr.FQDN, addr.IP) - } - } + for _, addr := range subnet.Addresses { + addr.ChangedBy = "ipam import" + addr.ChangedAt = time.Now() + if addr.FQDN != "" { + err := AddDNSFqdn(addr.FQDN, addr.IP) + if err != nil { + fmt.Printf("[ERROR] %v\n", err) + } + } + } - suberr := subnet.WriteSubnet() - if suberr != nil { - fmt.Printf("[ERROR] Can't write subnet to file %v\n", subnet.Subnet.String()) - fmt.Println(suberr) - } - fmt.Printf("[INFO] Imported subnet %v successfully\n", subnet.Subnet.String()) + suberr := subnet.WriteSubnet() + if suberr != nil { + fmt.Printf("[ERROR] Can't write subnet to file %v\n", subnet.Subnet.String()) + fmt.Println(suberr) + } + fmt.Printf("[INFO] Imported subnet %v successfully\n", subnet.Subnet.String()) - } - }, + } + }, } func init() { - rootCmd.AddCommand(importCmd) + rootCmd.AddCommand(importCmd) - importCmd.Flags().StringP("file", "f", "import.json", "File to use for import operation") + importCmd.Flags().StringP("file", "f", "import.json", "File to use for import operation") }