Repository: digitalocean/doctl
Pull request #1317 (2022-11-28 17:34:26)

function triggers: Use displayer for get sub-command and set displayer type.
This changes the triggers get sub-command to use the displayer rather than marshaling JSON itself. By default, it will use the table display and the output flag is supported for JSON. It also sets the displayer type so that the `--format` flag is now supported.

ddebarros: This may be okay, but `--output=json` prints out a list of triggers, where as the original behavior prints out a single item. 
andrewsomething: @ddebarros  That's true. The intention here is to make this command consistent with other doctl commands. That is how the JSON output is generally formatted. I suppose this might be considered a breaking change, but the trigger commands are all still hidden from the menus/documentation. I'd rather make these consistent now than carry the inconsistency into GA. Are there any beta docs that would need updating?

ddebarros: > @ddebarros That's true. The intention here is to make this command consistent with other doctl commands. That is how the JSON output is generally formatted. I suppose this might be considered a breaking change, but the trigger commands are all still hidden from the menus/documentation. I'd rather make these consistent now than carry the inconsistency into GA. Are there any beta docs that would need updating?

I think that should be fine. Yes, this feature is still hidden so it shouldn't break anything for the customer.

Patch:
diff --git a/commands/triggers.go b/commands/triggers.go
index d236eb00..2478e804 100644
--- a/commands/triggers.go
+++ b/commands/triggers.go
@@ -15,8 +15,6 @@ package commands
 
 import (
 	"context"
-	"encoding/json"
-	"fmt"
 
 	"github.com/digitalocean/doctl"
 	"github.com/digitalocean/doctl/commands/displayers"
@@ -43,12 +41,16 @@ when events from that source type occur.  Currently, only the ` + "`" + `schedul
 		Writer, aliasOpt("ls"), displayerType(&displayers.Triggers{}))
 	AddStringFlag(list, "function", "f", "", "list only triggers for the chosen function")
 
-	CmdBuilder(cmd, RunTriggerToggle(true), "enable <triggerName>", "Enable a trigger", "Use `doctl serverless triggers enable <triggerName>` to enable a trigger", Writer)
-	CmdBuilder(cmd, RunTriggerToggle(false), "disable <triggerName>", "Disable a trigger", "Use `doctl serverless triggers disable <triggerName>` to disable a trigger", Writer)
+	CmdBuilder(cmd, RunTriggerToggle(true), "enable <triggerName>",
+		"Enable a trigger", "Use `doctl serverless triggers enable <triggerName>` to enable a trigger",
+		Writer, displayerType(&displayers.Triggers{}))
+	CmdBuilder(cmd, RunTriggerToggle(false), "disable <triggerName>",
+		"Disable a trigger", "Use `doctl serverless triggers disable <triggerName>` to disable a trigger",
+		Writer, displayerType(&displayers.Triggers{}))
 
 	CmdBuilder(cmd, RunTriggersGet, "get <triggerName>", "Get the details for a trigger",
 		`Use `+"`"+`doctl serverless triggers get <triggerName>`+"`"+` for details about <triggerName>.`,
-		Writer)
+		Writer, displayerType(&displayers.Triggers{}))
 
 	return cmd
 }
@@ -76,12 +78,8 @@ func RunTriggersGet(c *CmdConfig) error {
 	if err != nil {
 		return err
 	}
-	json, err := json.MarshalIndent(&trigger, "", "  ")
-	if err != nil {
-		return err
-	}
-	fmt.Fprintln(c.Out, string(json))
-	return nil
+
+	return c.Display(&displayers.Triggers{List: []do.ServerlessTrigger{trigger}})
 }
 
 // RunTriggerToggle provides the logic for 'doctl sls trig enabled/disabled'


Test patch:
diff --git a/commands/triggers_test.go b/commands/triggers_test.go
index bfd1f164..ec9dc13c 100644
--- a/commands/triggers_test.go
+++ b/commands/triggers_test.go
@@ -65,24 +65,8 @@ func TestTriggersGet(t *testing.T) {
 				NextRunAt: &nextRunAt,
 			},
 		}
-		expect := `{
-  "namespace": "123-456",
-  "function": "misc/pollStatus",
-  "type": "SCHEDULED",
-  "name": "firePoll1",
-  "is_enabled": true,
-  "created_at": "2022-10-05T13:46:59Z",
-  "updated_at": "2022-10-17T18:41:30Z",
-  "scheduled_details": {
-    "cron": "5 * * * *",
-    "body": {
-      "foo": "bar"
-    }
-  },
-  "scheduled_runs": {
-    "next_run_at": "2022-11-03T17:03:02Z"
-  }
-}
+		expect := `Name         Cron Expression    Invokes            Enabled    Last Run At
+firePoll1    5 * * * *          misc/pollStatus    true       _
 `
 		tm.serverless.EXPECT().GetTrigger(context.TODO(), "aTrigger").Return(theTrigger, nil)