cmd/notifications.go
changeset 0 5abace724584
child 13 f862af8faf17
equal deleted inserted replaced
-1:000000000000 0:5abace724584
       
     1 // Copyright © 2017 Mikael Berthe <mikael@lilotux.net>
       
     2 //
       
     3 // Licensed under the MIT license.
       
     4 // Please see the LICENSE file is this directory.
       
     5 
       
     6 package cmd
       
     7 
       
     8 import (
       
     9 	"errors"
       
    10 
       
    11 	"github.com/spf13/cobra"
       
    12 )
       
    13 
       
    14 var notificationsOpts struct {
       
    15 	list, clear bool
       
    16 	notifID     int
       
    17 }
       
    18 
       
    19 // notificationsCmd represents the notifications subcommand
       
    20 var notificationsCmd = &cobra.Command{
       
    21 	Use:     "notifications", // XXX
       
    22 	Aliases: []string{"notification", "notif"},
       
    23 	Short:   "Manage notifications",
       
    24 	Example: `  madonctl accounts notifications --list
       
    25   madonctl accounts notifications --list --clear
       
    26   madonctl accounts notifications --notification-id N`,
       
    27 	//Long:    `TBW...`,
       
    28 	RunE: notificationRunE,
       
    29 }
       
    30 
       
    31 func init() {
       
    32 	accountsCmd.AddCommand(notificationsCmd)
       
    33 
       
    34 	notificationsCmd.Flags().BoolVar(&notificationsOpts.list, "list", false, "List all current notifications")
       
    35 	notificationsCmd.Flags().BoolVar(&notificationsOpts.clear, "clear", false, "Clear all current notifications")
       
    36 	notificationsCmd.Flags().IntVar(&notificationsOpts.notifID, "notification-id", 0, "Get a notification")
       
    37 }
       
    38 
       
    39 func notificationRunE(cmd *cobra.Command, args []string) error {
       
    40 	opt := notificationsOpts
       
    41 
       
    42 	if !opt.list && !opt.clear && opt.notifID < 1 {
       
    43 		return errors.New("missing parameters")
       
    44 	}
       
    45 
       
    46 	if err := madonInit(true); err != nil {
       
    47 		return err
       
    48 	}
       
    49 
       
    50 	var obj interface{}
       
    51 	var err error
       
    52 
       
    53 	if opt.list {
       
    54 		obj, err = gClient.GetNotifications()
       
    55 	} else if opt.notifID > 0 {
       
    56 		obj, err = gClient.GetNotification(opt.notifID)
       
    57 	}
       
    58 
       
    59 	if err == nil && opt.clear {
       
    60 		err = gClient.ClearNotifications()
       
    61 	}
       
    62 
       
    63 	if err != nil {
       
    64 		errPrint("Error: %s", err.Error())
       
    65 		return nil
       
    66 	}
       
    67 	if obj == nil {
       
    68 		return nil
       
    69 	}
       
    70 
       
    71 	p, err := getPrinter()
       
    72 	if err != nil {
       
    73 		return err
       
    74 	}
       
    75 	return p.PrintObj(obj, nil, "")
       
    76 }