github.com/haagen/force@v0.19.6-0.20140911230915-22addd930b34/notify.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/ViViDboarder/gotifier" 8 ) 9 10 var cmdNotifySet = &Command{ 11 Run: notifySet, 12 Usage: "notify (true | false)", 13 Short: "Should notifications be used", 14 Long: ` 15 Determines if notifications should be used 16 `, 17 } 18 19 func notifySet(cmd *Command, args []string) { 20 var err error 21 shouldNotify := true 22 if len(args) == 0 { 23 shouldNotify = getShouldNotify() 24 fmt.Println("Show notifications: " + strconv.FormatBool(shouldNotify)) 25 } else if len(args) == 1 { 26 shouldNotify, err = strconv.ParseBool(args[0]) 27 if err != nil { 28 fmt.Println("Expecting a boolean parameter.") 29 } 30 } else { 31 fmt.Println("Expecting only one parameter. true/false") 32 } 33 34 setShouldNotify(shouldNotify) 35 } 36 37 func setShouldNotify(shouldNotify bool) { 38 // Set config 39 Config.Save("notifications", "shouldNotify", strconv.FormatBool(shouldNotify)) 40 } 41 42 func getShouldNotify() bool { 43 shouldNotify := false 44 notifStr, err := Config.Load("notifications", "shouldNotify") 45 if err == nil { 46 shouldNotify, err = strconv.ParseBool(notifStr) 47 } 48 49 return shouldNotify 50 } 51 52 func notify(method string, message string) { 53 shouldNotify := getShouldNotify() 54 if shouldNotify { 55 gotifier.Notification{Title: "Force Cli", Subtitle: method, Message: message}.Push() 56 } 57 } 58 59 func notifySuccess(method string, success bool) { 60 if success { 61 notify(method, "SUCCESS") 62 } else { 63 notify(method, "FAILURE") 64 } 65 }