From 4958a980138a9a5de05e286338a9001b5f6b517b Mon Sep 17 00:00:00 2001 From: nobe4 Date: Thu, 3 Oct 2024 22:51:11 +0200 Subject: [PATCH] feat(json): implement json action for the REPL (#193) --- internal/actions/actions.go | 2 ++ internal/actions/json/json.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 internal/actions/json/json.go diff --git a/internal/actions/actions.go b/internal/actions/actions.go index 3301427..88d2cdd 100644 --- a/internal/actions/actions.go +++ b/internal/actions/actions.go @@ -7,6 +7,7 @@ import ( "github.com/nobe4/gh-not/internal/actions/debug" "github.com/nobe4/gh-not/internal/actions/done" "github.com/nobe4/gh-not/internal/actions/hide" + "github.com/nobe4/gh-not/internal/actions/json" "github.com/nobe4/gh-not/internal/actions/open" "github.com/nobe4/gh-not/internal/actions/pass" "github.com/nobe4/gh-not/internal/actions/print" @@ -27,6 +28,7 @@ func Map(client *gh.Client) ActionsMap { "done": &done.Runner{Client: client}, "open": &open.Runner{Client: client}, "assign": &assign.Runner{Client: client}, + "json": &json.Runner{}, } } diff --git a/internal/actions/json/json.go b/internal/actions/json/json.go new file mode 100644 index 0000000..00d5b4b --- /dev/null +++ b/internal/actions/json/json.go @@ -0,0 +1,29 @@ +/* +Package json implements an [actions.Runner] that prints a notification in JSON. +*/ +package json + +import ( + "encoding/json" + "fmt" + "io" + + "github.com/nobe4/gh-not/internal/notifications" +) + +type Runner struct{} + +func (_ *Runner) Run(n *notifications.Notification, _ []string, w io.Writer) error { + if n.Meta.Hidden { + return nil + } + + marshalled, err := json.MarshalIndent(n, "", " ") + if err != nil { + return err + } + + fmt.Fprint(w, string(marshalled)) + + return nil +}