diff --git a/cmd/realm/triggers.go b/cmd/realm/triggers.go index 2621bea..1b71aaa 100644 --- a/cmd/realm/triggers.go +++ b/cmd/realm/triggers.go @@ -225,7 +225,7 @@ func triggersSaveF(command *cobra.Command, args []string) error { } // retrieve triggers list - realmTriggers, err := ListTriggers(realm) + realmTriggers, err := listTriggers(realm) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) @@ -261,49 +261,8 @@ func triggersSaveF(command *cobra.Command, args []string) error { } return nil } -func ListTriggers(realm string) ([]string, error) { - listTriggersCall, err := astarteAPIClient.ListTriggers(realm) - if err != nil { - return []string{}, err - } - - utils.MaybeCurlAndExit(listTriggersCall, astarteAPIClient) - - listTriggersRes, err := listTriggersCall.Run(astarteAPIClient) - if err != nil { - return []string{}, err - } - rawlistTriggers, err := listTriggersRes.Parse() - if err != nil { - return []string{}, err - } - return rawlistTriggers.([]string), nil -} -func getTriggerDefinition(realm, triggerName string) (map[string]interface{}, error) { - getTriggerCall, err := astarteAPIClient.GetTrigger(realm, triggerName) - if err != nil { - return nil, err - } - - // When we're here in the context of `interfaces sync`, the to-curl flag - // is always false (`interfaces sync` has no `--to-curl` flag) - // and thus the call will never exit unexpectedly - utils.MaybeCurlAndExit(getTriggerCall, astarteAPIClient) - - getTriggerRes, err := getTriggerCall.Run(astarteAPIClient) - if err != nil { - return nil, err - } - rawTRigger, err := getTriggerRes.Parse() - if err != nil { - return nil, err - } - triggerDefinition, _ := rawTRigger.(map[string]interface{}) - return triggerDefinition, nil -} func triggersSyncF(command *cobra.Command, args []string) error { - if viper.GetBool("to-curl") { fmt.Println(`'triggers sync' does not support the --to-curl option. Install or update your triggers one by one with 'triggers install' or 'interface update'.`) os.Exit(1) @@ -338,12 +297,15 @@ func triggersSyncF(command *cobra.Command, args []string) error { // Notify the user about what we're about to do } + list := []string{} + for _, v := range triggerToInstall { list = append(list, v["name"].(string)) } list_existing := []string{} + for _, v := range triggerToUpdate { list_existing = append(list_existing, v["name"].(string)) } @@ -352,9 +314,11 @@ func triggersSyncF(command *cobra.Command, args []string) error { //install new triggers if len(triggerToInstall) > 0 { + fmt.Printf("\n") fmt.Printf("The following new triggers will be installed: %+q \n", list) fmt.Printf("\n") + if ok, err := utils.AskForConfirmation("Do you want to continue?"); !ok || err != nil { fmt.Printf("aborting") return nil @@ -367,15 +331,18 @@ func triggersSyncF(command *cobra.Command, args []string) error { fmt.Printf("trigger %s installed successfully\n", v["name"].(string)) } } + fmt.Printf("\n") } if len(triggerToUpdate) > 0 { + y, err := command.Flags().GetBool("force") if err != nil { return err } + if y { fmt.Printf("The following triggers already exists and WILL be DELETED and RECREATED: %+q \n", list_existing) fmt.Printf("\n") @@ -393,7 +360,9 @@ func triggersSyncF(command *cobra.Command, args []string) error { } fmt.Printf("\n") fmt.Printf("\n") + } else { + // Start syncing. fmt.Printf("The following triggers already exists and WILL NOT be updated: %+q \n", list_existing) fmt.Printf("\n") @@ -455,3 +424,45 @@ func updateTrigger(realm string, triggername string, newtrig map[string]interfac _, _ = updateTriggerRes.Parse() return nil } + +func listTriggers(realm string) ([]string, error) { + listTriggersCall, err := astarteAPIClient.ListTriggers(realm) + if err != nil { + return []string{}, err + } + + utils.MaybeCurlAndExit(listTriggersCall, astarteAPIClient) + + listTriggersRes, err := listTriggersCall.Run(astarteAPIClient) + if err != nil { + return []string{}, err + } + rawlistTriggers, err := listTriggersRes.Parse() + if err != nil { + return []string{}, err + } + return rawlistTriggers.([]string), nil +} + +func getTriggerDefinition(realm, triggerName string) (map[string]interface{}, error) { + getTriggerCall, err := astarteAPIClient.GetTrigger(realm, triggerName) + if err != nil { + return nil, err + } + + // When we're here in the context of `interfaces sync`, the to-curl flag + // is always false (`interfaces sync` has no `--to-curl` flag) + // and thus the call will never exit unexpectedly + utils.MaybeCurlAndExit(getTriggerCall, astarteAPIClient) + + getTriggerRes, err := getTriggerCall.Run(astarteAPIClient) + if err != nil { + return nil, err + } + rawTRigger, err := getTriggerRes.Parse() + if err != nil { + return nil, err + } + triggerDefinition, _ := rawTRigger.(map[string]interface{}) + return triggerDefinition, nil +}