This commit is contained in:
ViViDboarder 2017-06-02 13:59:12 -04:00
parent c1a99e2cad
commit ad24c95903
1 changed files with 44 additions and 35 deletions

View File

@ -12,57 +12,62 @@ import (
"github.com/ViViDboarder/gopush/pushbullet" "github.com/ViViDboarder/gopush/pushbullet"
) )
const (
action_list_devs = iota
action_send_note = iota
action_send_link = iota
)
type Options struct { type Options struct {
Token string Token string
Message string Message string
Device string Device string
Push bool Action int
List bool
} }
func loadArgs() (options Options, err error) { func readOptions() (options Options, err error) {
options = Options{} tokenPtr := flag.String("token", "", "Your API Token (will be persisted to your home directory)")
token := flag.String("token", "", "Your API Token") devicePtr := flag.String("d", "", "Set default device (defaults to all devices)")
activeDevice := flag.String("d", "", "Set default device")
listDevices := flag.Bool("l", false, "List devices") listDevices := flag.Bool("l", false, "List devices")
pushType := flag.String("t", "note", "Push type (note or link)")
flag.Parse() flag.Parse()
options.Token = *token // Token is needed for any connection to PushBullet
options.Device = *activeDevice options = Options{}
options.List = *listDevices options.Token = *tokenPtr
options.Device = *devicePtr
if options.List { if *listDevices {
options.Action = action_list_devs
return return
} }
// Positional args switch *pushType {
switch flag.NArg() { case "link":
case 0: option.Action = action_send_link
break
default:
// TODO: Decide if this should warn and print usage
case "note":
options.Action = action_send_note
break
}
// Read message from stdin or args
if flag.NArg() == 0 {
data, err := ioutil.ReadAll(os.Stdin) data, err := ioutil.ReadAll(os.Stdin)
if err != nil { if err != nil {
return options, err return options, err
} }
message := string(data) options.Message = string(data)
if message == "" { } else {
options.List = true options.Message = strings.Join(flag.Args(), " ")
} else { }
fmt.Println(message)
options.Message = message if option.Message == "" {
options.Push = true options.Action = action_list_devs
}
break
case 1:
options.Message = flag.Args()[0]
options.Push = true
break
case 2:
options.Device = flag.Args()[0]
options.Message = flag.Args()[1]
options.Push = true
break
} }
return
} }
func printDevices(devices []pushbullet.Device, activeDevice pushbullet.Device) { func printDevices(devices []pushbullet.Device, activeDevice pushbullet.Device) {
@ -111,7 +116,7 @@ func persistDevice(activeDeviceIden string, config goson.Config) {
} }
func main() { func main() {
options, err := loadArgs() options, err := readOptions()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -129,10 +134,14 @@ func main() {
persistDevice(pb.ActiveDevice.Iden, config) persistDevice(pb.ActiveDevice.Iden, config)
config.Write() config.Write()
if options.Push { switch option.Action {
case action_send_note:
pb.PushNote(options.Message) pb.PushNote(options.Message)
} else if options.List { break
default:
case action_list_devs:
devices := pb.GetDevices() devices := pb.GetDevices()
printDevices(devices, pb.ActiveDevice) printDevices(devices, pb.ActiveDevice)
break
} }
} }