gopush/gopush.go

139 lines
2.7 KiB
Go
Raw Permalink Normal View History

2014-09-20 01:32:10 +00:00
package main
import (
2017-05-30 22:30:49 +00:00
"errors"
2014-09-20 01:32:10 +00:00
"flag"
"fmt"
2017-05-13 01:05:07 +00:00
"io/ioutil"
2017-05-30 22:30:49 +00:00
"log"
2014-09-20 01:32:10 +00:00
"os"
2017-05-30 22:30:49 +00:00
"github.com/ViViDboarder/gopush/goson"
"github.com/ViViDboarder/gopush/pushbullet"
2014-09-20 01:32:10 +00:00
)
type Options struct {
2017-05-30 22:30:49 +00:00
Token string
Message string
Device string
Push bool
List bool
2014-09-20 01:32:10 +00:00
}
2017-05-30 22:30:49 +00:00
func loadArgs() (options Options, err error) {
options = Options{}
2014-09-20 01:32:10 +00:00
token := flag.String("token", "", "Your API Token")
activeDevice := flag.String("d", "", "Set default device")
2017-05-30 22:30:49 +00:00
listDevices := flag.Bool("l", false, "List devices")
2014-09-20 01:32:10 +00:00
flag.Parse()
options.Token = *token
options.Device = *activeDevice
2017-05-30 22:30:49 +00:00
options.List = *listDevices
2014-09-20 01:32:10 +00:00
2017-05-30 22:30:49 +00:00
if options.List {
return
2014-09-20 01:32:10 +00:00
}
// Positional args
2017-05-13 01:05:07 +00:00
switch flag.NArg() {
case 0:
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
2017-05-30 22:30:49 +00:00
return options, err
2017-05-13 01:05:07 +00:00
}
message := string(data)
if message == "" {
options.List = true
} else {
fmt.Println(message)
options.Message = message
options.Push = true
}
break
case 1:
2014-09-20 01:32:10 +00:00
options.Message = flag.Args()[0]
options.Push = true
2017-05-13 01:05:07 +00:00
break
case 2:
2014-09-20 01:32:10 +00:00
options.Device = flag.Args()[0]
options.Message = flag.Args()[1]
options.Push = true
2017-05-13 01:05:07 +00:00
break
2014-09-20 01:32:10 +00:00
}
2017-05-30 22:30:49 +00:00
return
2014-09-20 01:32:10 +00:00
}
2017-05-30 22:30:49 +00:00
func printDevices(devices []pushbullet.Device, activeDevice pushbullet.Device) {
fmt.Println("Devices:")
var prefix string
for _, device := range devices {
if device.Iden == activeDevice.Iden {
prefix = " *"
} else {
prefix = " "
}
2014-09-20 01:32:10 +00:00
2017-05-30 22:30:49 +00:00
fmt.Println(prefix + device.Format())
}
}
2014-09-20 01:32:10 +00:00
2017-05-30 22:30:49 +00:00
func getToken(options Options, config goson.Config) (token string, err error) {
2014-09-20 01:32:10 +00:00
if options.Token != "" {
2017-05-30 22:30:49 +00:00
token = options.Token
2014-09-20 01:32:10 +00:00
config.Set("token", options.Token)
} else {
2017-05-30 22:30:49 +00:00
var ok bool
token, ok = config.GetString("token")
2014-09-20 01:32:10 +00:00
if !ok {
2017-05-30 22:30:49 +00:00
err = errors.New("No token found")
2014-09-20 01:32:10 +00:00
}
}
2017-05-30 22:30:49 +00:00
return
}
2014-09-20 01:32:10 +00:00
2017-05-30 22:30:49 +00:00
func getDevice(options Options, config goson.Config) (device string) {
2014-09-20 01:32:10 +00:00
if options.Device == "" {
activeDeviceIden, ok := config.GetString("activeDeviceIden")
if ok {
2017-05-30 22:30:49 +00:00
device = activeDeviceIden
2014-09-20 01:32:10 +00:00
}
}
2017-05-30 22:30:49 +00:00
return
}
2014-09-20 01:32:10 +00:00
2017-05-30 22:30:49 +00:00
func persistDevice(activeDeviceIden string, config goson.Config) {
storedDeviceIden, ok := config.GetString("activeDeviceIden")
if activeDeviceIden != "" && ok && activeDeviceIden != storedDeviceIden {
config.Set("activeDeviceIden", activeDeviceIden)
}
}
2014-09-20 01:32:10 +00:00
2017-05-30 22:30:49 +00:00
func main() {
options, err := loadArgs()
if err != nil {
log.Fatal(err)
2014-09-20 01:32:10 +00:00
}
2017-05-30 22:30:49 +00:00
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)
2014-09-20 01:32:10 +00:00
2017-05-30 22:30:49 +00:00
persistDevice(pb.ActiveDevice.Iden, config)
2014-09-20 01:32:10 +00:00
config.Write()
if options.Push {
2017-05-12 23:13:50 +00:00
pb.PushNote(options.Message)
2014-09-20 01:32:10 +00:00
} else if options.List {
devices := pb.GetDevices()
2017-05-30 22:30:49 +00:00
printDevices(devices, pb.ActiveDevice)
2014-09-20 01:32:10 +00:00
}
}