From a6b03fa4bb89a8f0c3546f720d5b35178c29f499 Mon Sep 17 00:00:00 2001 From: Andinus Date: Mon, 23 Mar 2020 13:32:16 +0530 Subject: Add notify functionality to cetus --- cmd/cetus/apod.go | 13 +++++++++++++ cmd/cetus/bpod.go | 13 +++++++++++++ cmd/cetus/main.go | 9 +++++++-- go.mod | 2 ++ go.sum | 2 ++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 go.sum diff --git a/cmd/cetus/apod.go b/cmd/cetus/apod.go index b99d899..cb5abae 100644 --- a/cmd/cetus/apod.go +++ b/cmd/cetus/apod.go @@ -7,6 +7,7 @@ import ( "framagit.org/andinus/cetus/pkg/apod" "framagit.org/andinus/cetus/pkg/background" + "framagit.org/andinus/indus/notification" ) func execAPOD() { @@ -77,6 +78,18 @@ func execAPOD() { apod.Print(res) } + // Send a desktop notification if notify flag was passed + if *apodNotify { + n := notification.Notif{} + n.Title = res.Title + n.Message = fmt.Sprintf("%s\n\n%s", + res.Date, + res.Explanation) + + err = n.Notify() + chkErr(err) + } + // Proceed only if the command was set because if it was fetch // then it's already finished & should exit now. if os.Args[1] == "fetch" { diff --git a/cmd/cetus/bpod.go b/cmd/cetus/bpod.go index f308d47..cf0a4bb 100644 --- a/cmd/cetus/bpod.go +++ b/cmd/cetus/bpod.go @@ -8,6 +8,7 @@ import ( "framagit.org/andinus/cetus/pkg/background" "framagit.org/andinus/cetus/pkg/bpod" + "framagit.org/andinus/indus/notification" ) func execBPOD() { @@ -107,6 +108,18 @@ func execBPOD() { bpod.Print(res) } + // Send a desktop notification if notify flag was passed + if *bpodNotify { + n := notification.Notif{} + n.Title = res.Title + n.Message = fmt.Sprintf("%s\n\n%s", + res.StartDate, + res.Copyright) + + err = n.Notify() + chkErr(err) + } + // Proceed only if the command was set because if it was fetch // then it's already finished & should exit now. if os.Args[1] == "fetch" { diff --git a/cmd/cetus/main.go b/cmd/cetus/main.go index b039f7a..0ff6b44 100644 --- a/cmd/cetus/main.go +++ b/cmd/cetus/main.go @@ -22,6 +22,9 @@ var ( bpodPathOnly *bool bpodQuiet *bool bpodDump *bool + + apodNotify *bool + bpodNotify *bool ) func main() { @@ -52,6 +55,8 @@ func main() { rand.Seed(time.Now().Unix()) apodCmd := flag.NewFlagSet("apod", flag.ExitOnError) + bpodCmd := flag.NewFlagSet("bpod", flag.ExitOnError) + defDate := time.Now().UTC().Format("2006-01-02") // Flags to parse for apod service. @@ -62,8 +67,7 @@ func main() { apodPathOnly = apodCmd.Bool("path-only", false, "Print only the path") apodQuiet = apodCmd.Bool("quiet", false, "Stay quiet") apodDump = apodCmd.Bool("dump", false, "Dump received response") - - bpodCmd := flag.NewFlagSet("bpod", flag.ExitOnError) + apodNotify = apodCmd.Bool("notify", false, "Send a desktop notification with background information") // Flags to parse for bpod service. bpodAPI = bpodCmd.String("api", "https://www.bing.com/HPImageArchive.aspx", "BPOD API") @@ -71,6 +75,7 @@ func main() { bpodPathOnly = bpodCmd.Bool("path-only", false, "Print only the path") bpodQuiet = bpodCmd.Bool("quiet", false, "Stay quiet") bpodDump = bpodCmd.Bool("dump", false, "Dump received response") + bpodNotify = bpodCmd.Bool("notify", false, "Send a desktop notification with background information") // Switching on commands will cause more repetition than // switching on service. If we switch on commands then switch diff --git a/go.mod b/go.mod index fd85b01..2831fa0 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module framagit.org/andinus/cetus go 1.13 + +require framagit.org/andinus/indus v0.1.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6410ae6 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +framagit.org/andinus/indus v0.1.0 h1:3aY1smToiF4rdEYlvEeAVMU/9SEzq+3pqZpI2MUgbfA= +framagit.org/andinus/indus v0.1.0/go.mod h1:a281k0YF06Cral0R7LK0EAVs7f2SB4MrTp+L3BMEIT8= -- cgit 1.4.1-2-gfad0