From d201967479df2dd1fe5169144a2b31a443606975 Mon Sep 17 00:00:00 2001 From: Srinandan Sridhar <13950006+srinandan@users.noreply.github.com> Date: Fri, 13 Sep 2024 14:06:26 -0700 Subject: [PATCH] feat: adds continue on err #538 (#539) * feat: adds continue on err #538 * bug: create proxy kvms first #538 --- internal/cmd/kvm/impkvms.go | 53 ++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/internal/cmd/kvm/impkvms.go b/internal/cmd/kvm/impkvms.go index 27b092caf..bc03bf420 100644 --- a/internal/cmd/kvm/impkvms.go +++ b/internal/cmd/kvm/impkvms.go @@ -50,60 +50,71 @@ var ImpCmd = &cobra.Command{ kvmFile := filepath.Base(orgKVMFile) kvmMetadata := strings.Split(kvmFile, utils.DefaultFileSplitter) clilog.Info.Printf("\tCreating KVM %s\n", orgKVMFile) - if _, err = kvm.Create("", kvmMetadata[1], true); err != nil { + if _, err = kvm.Create("", kvmMetadata[1], true); proceedOnError(err) != nil { return err } clilog.Info.Printf("\tImporting entries for %s\n", orgKVMFile) - if err = kvm.ImportEntries("", kvmMetadata[1], conn, orgKVMFile); err != nil { + if err = kvm.ImportEntries("", kvmMetadata[1], conn, orgKVMFile); proceedOnError(err) != nil { return err } } } - if len(envKVMFileList) > 0 { - clilog.Info.Println("Importing env scoped KVMs...") - for _, envKVMFile := range envKVMFileList { - kvmFile := filepath.Base(envKVMFile) + if len(proxyKVMFileList) > 0 { + clilog.Info.Println("Importing proxy scoped KVMs...") + for _, proxyKVMFile := range proxyKVMFileList { + kvmFile := filepath.Base(proxyKVMFile) kvmMetadata := strings.Split(kvmFile, utils.DefaultFileSplitter) - apiclient.SetApigeeEnv(kvmMetadata[1]) - clilog.Info.Printf("\tCreating KVM %s\n", envKVMFile) - if _, err = kvm.Create("", kvmMetadata[2], true); err != nil { + clilog.Info.Printf("\tCreating KVM %s\n", proxyKVMFile) + if _, err = kvm.Create(kvmMetadata[1], kvmMetadata[2], true); proceedOnError(err) != nil { return err } - clilog.Info.Printf("\tImporting entries for %s\n", envKVMFile) - if err = kvm.ImportEntries("", kvmMetadata[2], conn, envKVMFile); err != nil { + clilog.Info.Printf("\tImporting entries for %s\n", proxyKVMFile) + if err = kvm.ImportEntries(kvmMetadata[1], kvmMetadata[2], conn, proxyKVMFile); proceedOnError(err) != nil { return err } } } - if len(proxyKVMFileList) > 0 { - clilog.Info.Println("Importing proxy scoped KVMs...") - for _, proxyKVMFile := range proxyKVMFileList { - kvmFile := filepath.Base(proxyKVMFile) + if len(envKVMFileList) > 0 { + clilog.Info.Println("Importing env scoped KVMs...") + for _, envKVMFile := range envKVMFileList { + kvmFile := filepath.Base(envKVMFile) kvmMetadata := strings.Split(kvmFile, utils.DefaultFileSplitter) - clilog.Info.Printf("\tCreating KVM %s\n", proxyKVMFile) - if _, err = kvm.Create(kvmMetadata[1], kvmMetadata[2], true); err != nil { + apiclient.SetApigeeEnv(kvmMetadata[1]) + clilog.Info.Printf("\tCreating KVM %s\n", envKVMFile) + if _, err = kvm.Create("", kvmMetadata[2], true); proceedOnError(err) != nil { return err } - clilog.Info.Printf("\tImporting entries for %s\n", proxyKVMFile) - if err = kvm.ImportEntries(kvmMetadata[1], kvmMetadata[2], conn, proxyKVMFile); err != nil { + clilog.Info.Printf("\tImporting entries for %s\n", envKVMFile) + if err = kvm.ImportEntries("", kvmMetadata[2], conn, envKVMFile); proceedOnError(err) != nil { return err } } } - return err }, } -var folder string +var ( + folder string + continueOnErr bool +) func init() { ImpCmd.Flags().StringVarP(&folder, "folder", "f", "", "The absolute path to the folder containing KVM entries") ImpCmd.Flags().IntVarP(&conn, "conn", "c", 4, "Number of connections") + ImpCmd.Flags().BoolVarP(&continueOnErr, "continue-on-error", "", + false, "Ignore errors and continue importing data") _ = ImpCmd.MarkFlagRequired("folder") } + +func proceedOnError(e error) error { + if continueOnErr { + return nil + } + return e +}