fix missing error handling

This commit is contained in:
Adora Laura Kalb 2023-03-31 20:13:55 +02:00
parent 0dde2b61a1
commit e4d00323b5
Signed by: adoralaura
GPG key ID: 7A4552166FC8C056

View file

@ -5,72 +5,75 @@ Copyright © 2023 Laura Kalb <dev@lauka.net>
package cmd package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"path" "path"
"time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var importCmd = &cobra.Command{ var importCmd = &cobra.Command{
Use: "import", Use: "import",
Short: "Import ipam configuration", Short: "Import ipam configuration",
Long: `Import subnets to ipam.`, Long: `Import subnets to ipam.`,
Example: "ipam import --file import.json", Example: "ipam import --file import.json",
Args: cobra.NoArgs, Args: cobra.NoArgs,
Aliases: []string{"im"}, Aliases: []string{"im"},
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var importname string var importname string
var subnets []Subnet var subnets []Subnet
flagpath, _ := cmd.Flags().GetString("file") flagpath, _ := cmd.Flags().GetString("file")
if path.IsAbs(flagpath) { if path.IsAbs(flagpath) {
importname = flagpath importname = flagpath
} else { } else {
wd, _ := os.Getwd() wd, _ := os.Getwd()
importname = path.Join(wd, flagpath) importname = path.Join(wd, flagpath)
} }
file, readerr := os.ReadFile(importname) file, readerr := os.ReadFile(importname)
if readerr != nil { if readerr != nil {
fmt.Printf("[ERROR] Can't read file %v\n", importname) fmt.Printf("[ERROR] Can't read file %v\n", importname)
fmt.Println(readerr) fmt.Println(readerr)
} }
marsherr := json.Unmarshal(file, &subnets) marsherr := json.Unmarshal(file, &subnets)
if marsherr != nil { if marsherr != nil {
fmt.Printf("[ERROR] Invalid format for file %v\n", importname) fmt.Printf("[ERROR] Invalid format for file %v\n", importname)
fmt.Println(marsherr) fmt.Println(marsherr)
} }
for _, subnet := range subnets { for _, subnet := range subnets {
fmt.Printf("[INFO] Start import of %v\n", subnet.Subnet.String()) fmt.Printf("[INFO] Start import of %v\n", subnet.Subnet.String())
subnet.ChangedBy = "ipam import" subnet.ChangedBy = "ipam import"
subnet.ChangedAt = time.Now() subnet.ChangedAt = time.Now()
for _, addr := range subnet.Addresses { for _, addr := range subnet.Addresses {
addr.ChangedBy = "ipam import" addr.ChangedBy = "ipam import"
addr.ChangedAt = time.Now() addr.ChangedAt = time.Now()
if addr.FQDN != "" { if addr.FQDN != "" {
AddDNSFqdn(addr.FQDN, addr.IP) err := AddDNSFqdn(addr.FQDN, addr.IP)
} if err != nil {
} fmt.Printf("[ERROR] %v\n", err)
}
}
}
suberr := subnet.WriteSubnet() suberr := subnet.WriteSubnet()
if suberr != nil { if suberr != nil {
fmt.Printf("[ERROR] Can't write subnet to file %v\n", subnet.Subnet.String()) fmt.Printf("[ERROR] Can't write subnet to file %v\n", subnet.Subnet.String())
fmt.Println(suberr) fmt.Println(suberr)
} }
fmt.Printf("[INFO] Imported subnet %v successfully\n", subnet.Subnet.String()) fmt.Printf("[INFO] Imported subnet %v successfully\n", subnet.Subnet.String())
} }
}, },
} }
func init() { 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")
} }