2
0
mirror of https://github.com/ViViDboarder/gopush.git synced 2024-11-22 02:06:33 +00:00

A little cleanup

This commit is contained in:
ViViDboarder 2017-05-30 15:30:49 -07:00
parent 72fb44da01
commit d87e00a4a1
2 changed files with 78 additions and 59 deletions

View File

@ -9,6 +9,7 @@ General usage:
gopush -token="yourapitokenfrompushbullet" gopush -token="yourapitokenfrompushbullet"
gopush "Whatever you want pushed" gopush "Whatever you want pushed"
``` ```
Defaults to all devices, but you can also specify with `-d="Device name"`
Defaults to all devices, but you can also specify with `gopush -d="Device name"`. Can't remember your devices? `gopush -l` will list them.
I often use this combined with a bash or fish script (maybe I'll post later) to push me successes, failures, or results for long running commands. I often use this combined with a bash or fish script (maybe I'll post later) to push me successes, failures, or results for long running commands.

134
gopush.go
View File

@ -1,37 +1,39 @@
package main package main
import ( import (
"errors"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"./goson" "github.com/ViViDboarder/gopush/goson"
"./pushbullet" "github.com/ViViDboarder/gopush/pushbullet"
) )
type Options struct { type Options struct {
Token string Token string
Message string Message string
Device string Device string
Push bool Push bool
List bool List bool
SetActive bool
} }
var options = Options{} func loadArgs() (options Options, err error) {
options = Options{}
func loadArgs() {
token := flag.String("token", "", "Your API Token") token := flag.String("token", "", "Your API Token")
activeDevice := flag.String("d", "", "Set default device") activeDevice := flag.String("d", "", "Set default device")
listDevices := flag.Bool("l", false, "List devices")
flag.Parse() flag.Parse()
options.Token = *token options.Token = *token
options.Device = *activeDevice options.Device = *activeDevice
options.List = *listDevices
if options.Device != "" { if options.List {
options.SetActive = true return
} }
// Positional args // Positional args
@ -39,8 +41,7 @@ func loadArgs() {
case 0: case 0:
data, err := ioutil.ReadAll(os.Stdin) data, err := ioutil.ReadAll(os.Stdin)
if err != nil { if err != nil {
// TODO: Make errors great again! return options, err
os.Exit(1)
} }
message := string(data) message := string(data)
if message == "" { if message == "" {
@ -61,51 +62,10 @@ func loadArgs() {
options.Push = true options.Push = true
break break
} }
return
} }
func main() { func printDevices(devices []pushbullet.Device, activeDevice pushbullet.Device) {
loadArgs()
config := goson.LoadConfig("gopush")
var ok bool
if options.Token != "" {
config.Set("token", options.Token)
} else {
options.Token, ok = config.GetString("token")
if !ok {
fmt.Println("No token found")
os.Exit(1)
}
}
pb := pushbullet.New(options.Token)
if options.Device == "" {
activeDeviceIden, ok := config.GetString("activeDeviceIden")
if ok {
options.Device = activeDeviceIden
}
}
pb.SetActiveDevice(options.Device)
if options.SetActive {
config.Set("activeDeviceIden", pb.ActiveDevice.Iden)
}
config.Write()
if options.Push {
pb.PushNote(options.Message)
} else if options.List {
devices := pb.GetDevices()
PrintDevices(devices, pb.ActiveDevice)
}
}
func PrintDevices(devices []pushbullet.Device, activeDevice pushbullet.Device) {
fmt.Println("Devices:") fmt.Println("Devices:")
var prefix string var prefix string
for _, device := range devices { for _, device := range devices {
@ -118,3 +78,61 @@ func PrintDevices(devices []pushbullet.Device, activeDevice pushbullet.Device) {
fmt.Println(prefix + device.Format()) fmt.Println(prefix + device.Format())
} }
} }
func getToken(options Options, config goson.Config) (token string, err error) {
if options.Token != "" {
token = options.Token
config.Set("token", options.Token)
} else {
var ok bool
token, ok = config.GetString("token")
if !ok {
err = errors.New("No token found")
}
}
return
}
func getDevice(options Options, config goson.Config) (device string) {
if options.Device == "" {
activeDeviceIden, ok := config.GetString("activeDeviceIden")
if ok {
device = activeDeviceIden
}
}
return
}
func persistDevice(activeDeviceIden string, config goson.Config) {
storedDeviceIden, ok := config.GetString("activeDeviceIden")
if activeDeviceIden != "" && ok && activeDeviceIden != storedDeviceIden {
config.Set("activeDeviceIden", activeDeviceIden)
}
}
func main() {
options, err := loadArgs()
if err != nil {
log.Fatal(err)
}
config := goson.LoadConfig("gopush")
token, err := getToken(options, config)
if err != nil {
fmt.Println("Token required and not provided by config or command line")
log.Fatal(err)
}
device := getDevice(options, config)
pb := pushbullet.New(token)
pb.SetActiveDevice(device)
persistDevice(pb.ActiveDevice.Iden, config)
config.Write()
if options.Push {
pb.PushNote(options.Message)
} else if options.List {
devices := pb.GetDevices()
printDevices(devices, pb.ActiveDevice)
}
}