cmd/domainblocks.go
changeset 151 466aabf6c90a
child 155 0fd940f16084
equal deleted inserted replaced
150:7f46bcaf240c 151:466aabf6c90a
       
     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 	"os"
       
    10 
       
    11 	"github.com/pkg/errors"
       
    12 	"github.com/spf13/cobra"
       
    13 
       
    14 	"github.com/McKael/madon"
       
    15 )
       
    16 
       
    17 var domainBlocksOpts struct {
       
    18 	show, block, unblock bool
       
    19 
       
    20 	limit          uint  // Limit the results
       
    21 	sinceID, maxID int64 // Query boundaries
       
    22 	all            bool  // Try to fetch all results
       
    23 }
       
    24 
       
    25 // timelinesCmd represents the timelines command
       
    26 var domainBlocksCmd = &cobra.Command{
       
    27 	Use:     "domain-blocks --show|--block|--unblock [DOMAINNAME]",
       
    28 	Aliases: []string{"domain-block"},
       
    29 	Short:   "Display, add or remove user-blocked domains",
       
    30 	RunE:    domainBlocksRunE,
       
    31 	Example: `  madonctl domain-blocks --show
       
    32   madonctl domain-blocks --block   example.com
       
    33   madonctl domain-blocks --unblock example.com`,
       
    34 }
       
    35 
       
    36 func init() {
       
    37 	RootCmd.AddCommand(domainBlocksCmd)
       
    38 
       
    39 	domainBlocksCmd.Flags().BoolVar(&domainBlocksOpts.show, "show", false, "List current user-blocked domains")
       
    40 	domainBlocksCmd.Flags().BoolVar(&domainBlocksOpts.block, "block", false, "Block domain")
       
    41 	domainBlocksCmd.Flags().BoolVar(&domainBlocksOpts.unblock, "unblock", false, "Unblock domain")
       
    42 
       
    43 	domainBlocksCmd.Flags().UintVarP(&domainBlocksOpts.limit, "limit", "l", 0, "Limit number of results")
       
    44 	domainBlocksCmd.Flags().Int64Var(&domainBlocksOpts.sinceID, "since-id", 0, "Request IDs greater than a value")
       
    45 	domainBlocksCmd.Flags().Int64Var(&domainBlocksOpts.maxID, "max-id", 0, "Request IDs less (or equal) than a value")
       
    46 	domainBlocksCmd.Flags().BoolVar(&domainBlocksOpts.all, "all", false, "Fetch all results")
       
    47 }
       
    48 
       
    49 func domainBlocksRunE(cmd *cobra.Command, args []string) error {
       
    50 	opt := domainBlocksOpts
       
    51 	var domName madon.DomainName
       
    52 
       
    53 	// Check flags
       
    54 	if opt.block && opt.unblock {
       
    55 		return errors.New("cannot use both --block and --unblock")
       
    56 	}
       
    57 
       
    58 	if opt.block || opt.unblock {
       
    59 		if opt.show {
       
    60 			return errors.New("cannot use both --[un]block and --show")
       
    61 		}
       
    62 		if len(args) != 1 {
       
    63 			return errors.New("missing domain name")
       
    64 		}
       
    65 		domName = madon.DomainName(args[0])
       
    66 	}
       
    67 
       
    68 	if !opt.show && !opt.block && !opt.unblock {
       
    69 		return errors.New("missing flag: please provide --show, --block or --unblock")
       
    70 	}
       
    71 
       
    72 	// Set up LimitParams
       
    73 	var limOpts *madon.LimitParams
       
    74 	if opt.all || opt.limit > 0 || opt.sinceID > 0 || opt.maxID > 0 {
       
    75 		limOpts = new(madon.LimitParams)
       
    76 		limOpts.All = opt.all
       
    77 	}
       
    78 	if opt.limit > 0 {
       
    79 		limOpts.Limit = int(opt.limit)
       
    80 	}
       
    81 	if opt.maxID > 0 {
       
    82 		limOpts.MaxID = opt.maxID
       
    83 	}
       
    84 	if opt.sinceID > 0 {
       
    85 		limOpts.SinceID = opt.sinceID
       
    86 	}
       
    87 
       
    88 	// Log in
       
    89 	if err := madonInit(true); err != nil {
       
    90 		return err
       
    91 	}
       
    92 
       
    93 	var obj interface{}
       
    94 	var err error
       
    95 
       
    96 	switch {
       
    97 	case opt.show:
       
    98 		var domainList []madon.DomainName
       
    99 		domainList, err = gClient.GetDomainBlocks(limOpts)
       
   100 		obj = domainList
       
   101 	case opt.block:
       
   102 		err = gClient.DomainBlock(domName)
       
   103 	case opt.unblock:
       
   104 		err = gClient.DomainUnblock(domName)
       
   105 	default:
       
   106 		return errors.New("domainBlocksCmd: internal error")
       
   107 	}
       
   108 
       
   109 	if err != nil {
       
   110 		errPrint("Error: %s", err.Error())
       
   111 		os.Exit(1)
       
   112 	}
       
   113 	if obj == nil {
       
   114 		return nil
       
   115 	}
       
   116 
       
   117 	p, err := getPrinter()
       
   118 	if err != nil {
       
   119 		errPrint("Error: %v", err)
       
   120 		os.Exit(1)
       
   121 	}
       
   122 	return p.printObj(obj)
       
   123 }