cmd/lists.go
changeset 267 5b91a65ba95a
parent 239 605a00e9d1ab
child 268 4dd196a4ee7c
equal deleted inserted replaced
264:8f478162d991 267:5b91a65ba95a
    13 
    13 
    14 	"github.com/McKael/madon/v2"
    14 	"github.com/McKael/madon/v2"
    15 )
    15 )
    16 
    16 
    17 var listsOpts struct {
    17 var listsOpts struct {
    18 	listID     int64
    18 	listID     madon.ActivityID
    19 	accountID  int64
    19 	accountID  madon.ActivityID
    20 	accountIDs string
    20 	accountIDs string
    21 	title      string
    21 	title      string
    22 
    22 
    23 	// Used for several subcommands to limit the number of results
    23 	// Used for several subcommands to limit the number of results
    24 	limit, keep uint
    24 	limit, keep uint
    49 
    49 
    50 	listsCmd.PersistentFlags().UintVarP(&listsOpts.limit, "limit", "l", 0, "Limit number of API results")
    50 	listsCmd.PersistentFlags().UintVarP(&listsOpts.limit, "limit", "l", 0, "Limit number of API results")
    51 	listsCmd.PersistentFlags().UintVarP(&listsOpts.keep, "keep", "k", 0, "Limit number of results")
    51 	listsCmd.PersistentFlags().UintVarP(&listsOpts.keep, "keep", "k", 0, "Limit number of results")
    52 	listsCmd.PersistentFlags().BoolVar(&listsOpts.all, "all", false, "Fetch all results")
    52 	listsCmd.PersistentFlags().BoolVar(&listsOpts.all, "all", false, "Fetch all results")
    53 
    53 
    54 	listsCmd.PersistentFlags().Int64VarP(&listsOpts.listID, "list-id", "G", 0, "List ID")
    54 	listsCmd.PersistentFlags().StringVarP(&listsOpts.listID, "list-id", "G", "", "List ID")
    55 
    55 
    56 	listsGetSubcommand.Flags().Int64VarP(&listsOpts.accountID, "account-id", "a", 0, "Account ID number")
    56 	listsGetSubcommand.Flags().StringVarP(&listsOpts.accountID, "account-id", "a", "", "Account ID number")
    57 	// XXX accountUID?
    57 	// XXX accountUID?
    58 
    58 
    59 	listsGetAccountsSubcommand.Flags().Int64VarP(&listsOpts.listID, "list-id", "G", 0, "List ID")
    59 	listsGetAccountsSubcommand.Flags().StringVarP(&listsOpts.listID, "list-id", "G", "", "List ID")
    60 
    60 
    61 	listsCreateSubcommand.Flags().StringVar(&listsOpts.title, "title", "", "List title")
    61 	listsCreateSubcommand.Flags().StringVar(&listsOpts.title, "title", "", "List title")
    62 	listsUpdateSubcommand.Flags().StringVar(&listsOpts.title, "title", "", "List title")
    62 	listsUpdateSubcommand.Flags().StringVar(&listsOpts.title, "title", "", "List title")
    63 
    63 
    64 	listsAddAccountsSubcommand.Flags().StringVar(&listsOpts.accountIDs, "account-ids", "", "Comma-separated list of account IDs")
    64 	listsAddAccountsSubcommand.Flags().StringVar(&listsOpts.accountIDs, "account-ids", "", "Comma-separated list of account IDs")
    65 	listsAddAccountsSubcommand.Flags().Int64VarP(&listsOpts.accountID, "account-id", "a", 0, "Account ID number")
    65 	listsAddAccountsSubcommand.Flags().StringVarP(&listsOpts.accountID, "account-id", "a", "", "Account ID number")
    66 	listsRemoveAccountsSubcommand.Flags().StringVar(&listsOpts.accountIDs, "account-ids", "", "Comma-separated list of account IDs")
    66 	listsRemoveAccountsSubcommand.Flags().StringVar(&listsOpts.accountIDs, "account-ids", "", "Comma-separated list of account IDs")
    67 	listsRemoveAccountsSubcommand.Flags().Int64VarP(&listsOpts.accountID, "account-id", "a", 0, "Account ID number")
    67 	listsRemoveAccountsSubcommand.Flags().StringVarP(&listsOpts.accountID, "account-id", "a", "", "Account ID number")
    68 }
    68 }
    69 
    69 
    70 var listsSubcommands = []*cobra.Command{
    70 var listsSubcommands = []*cobra.Command{
    71 	listsGetSubcommand,
    71 	listsGetSubcommand,
    72 	listsCreateSubcommand,
    72 	listsCreateSubcommand,
   143 	}
   143 	}
   144 
   144 
   145 	var obj interface{}
   145 	var obj interface{}
   146 	var err error
   146 	var err error
   147 
   147 
   148 	if opt.listID > 0 {
   148 	if opt.listID != "" {
   149 		var list *madon.List
   149 		var list *madon.List
   150 		list, err = gClient.GetList(opt.listID)
   150 		list, err = gClient.GetList(opt.listID)
   151 		obj = list
   151 		obj = list
   152 	} else {
   152 	} else {
   153 		var lists []madon.List
   153 		var lists []madon.List
   176 }
   176 }
   177 
   177 
   178 func listsGetAccountsRunE(cmd *cobra.Command, args []string) error {
   178 func listsGetAccountsRunE(cmd *cobra.Command, args []string) error {
   179 	opt := listsOpts
   179 	opt := listsOpts
   180 
   180 
   181 	if opt.listID <= 0 {
   181 	if opt.listID == "" {
   182 		return errors.New("missing list ID")
   182 		return errors.New("missing list ID")
   183 	}
   183 	}
   184 
   184 
   185 	// Log in
   185 	// Log in
   186 	if err := madonInit(true); err != nil {
   186 	if err := madonInit(true); err != nil {
   235 	var action int
   235 	var action int
   236 	opt := listsOpts
   236 	opt := listsOpts
   237 
   237 
   238 	switch cmd.Name() {
   238 	switch cmd.Name() {
   239 	case "create":
   239 	case "create":
   240 		if opt.listID > 0 {
   240 		if opt.listID != "" {
   241 			return errors.New("list ID should not be provided with create")
   241 			return errors.New("list ID should not be provided with create")
   242 		}
   242 		}
   243 		action = actionCreate
   243 		action = actionCreate
   244 	case "update":
   244 	case "update":
   245 		if opt.listID <= 0 {
   245 		if opt.listID == "" {
   246 			return errors.New("list ID is required")
   246 			return errors.New("list ID is required")
   247 		}
   247 		}
   248 		action = actionUpdate
   248 		action = actionUpdate
   249 	case "delete", "rm", "del":
   249 	case "delete", "rm", "del":
   250 		action = actionDelete
   250 		action = actionDelete
   298 }
   298 }
   299 
   299 
   300 func listsAddRemoveAccountsRunE(cmd *cobra.Command, args []string) error {
   300 func listsAddRemoveAccountsRunE(cmd *cobra.Command, args []string) error {
   301 	opt := listsOpts
   301 	opt := listsOpts
   302 
   302 
   303 	if opt.listID <= 0 {
   303 	if opt.listID == "" {
   304 		return errors.New("missing list ID")
   304 		return errors.New("missing list ID")
   305 	}
   305 	}
   306 
   306 
   307 	var ids []int64
   307 	var ids []madon.ActivityID
   308 	var err error
   308 	var err error
   309 	ids, err = splitIDs(opt.accountIDs)
   309 	ids, err = splitIDs(opt.accountIDs)
   310 	if err != nil {
   310 	if err != nil {
   311 		return errors.New("cannot parse account IDs")
   311 		return errors.New("cannot parse account IDs")
   312 	}
   312 	}
   313 
   313 
   314 	if opt.accountID > 0 { // Allow --account-id
   314 	if opt.accountID != "" { // Allow --account-id
   315 		ids = []int64{opt.accountID}
   315 		ids = []madon.ActivityID{opt.accountID}
   316 	}
   316 	}
   317 	if len(ids) < 1 {
   317 	if len(ids) < 1 {
   318 		return errors.New("missing account IDs")
   318 		return errors.New("missing account IDs")
   319 	}
   319 	}
   320 
   320