Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
Signed-off-by: Eddy Babetto <eddy.babetto@secomind.com>
Signed-off-by: Eddy Babetto <eddy.babetto@gmail.com>
  • Loading branch information
eddbbt committed Oct 16, 2023
1 parent 056932b commit 72a6616
Showing 1 changed file with 53 additions and 42 deletions.
95 changes: 53 additions & 42 deletions cmd/realm/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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
}

0 comments on commit 72a6616

Please sign in to comment.