Chromium Code Reviews| Index: go/src/infra/tools/cipd/apps/cipd/main.go |
| diff --git a/go/src/infra/tools/cipd/apps/cipd/main.go b/go/src/infra/tools/cipd/apps/cipd/main.go |
| index 132b83d606e49dd2d785ff0e64b551fa8e99672d..f433ef7c717d7ab6714f9d564072b42cb9fda77c 100644 |
| --- a/go/src/infra/tools/cipd/apps/cipd/main.go |
| +++ b/go/src/infra/tools/cipd/apps/cipd/main.go |
| @@ -41,14 +41,16 @@ var ( |
| // checkCommandLine ensures all required positional and flag-like parameters |
| // are set. Returns true if they are, or false (and prints to stderr) if not. |
| -func checkCommandLine(args []string, flags *flag.FlagSet, positionalCount int) bool { |
| +func checkCommandLine(args []string, flags *flag.FlagSet, minPositionalCount int, |
| + maxPositionalCount int) bool { |
|
Vadim Sh.
2015/06/23 00:08:35
nit: we don't limit go code to 80 chars. In this c
estaab
2015/06/23 02:26:13
Done.
|
| // Check number of expected positional arguments. |
| - if positionalCount == 0 && len(args) != 0 { |
| + if maxPositionalCount == 0 && len(args) != 0 { |
| log.Errorf("Unexpected arguments: %v", args) |
| return false |
| } |
| - if len(args) != positionalCount { |
| - log.Errorf("Expecting %d arguments, got %d", positionalCount, len(args)) |
| + if len(args) < minPositionalCount || len(args) > maxPositionalCount { |
| + log.Errorf("Expecting [%d, %d] arguments, got %d", minPositionalCount, |
| + maxPositionalCount, len(args)) |
| return false |
| } |
| // Check required unset flags. |
| @@ -314,6 +316,18 @@ func (opts *JSONOutputOptions) writeJSONOutput(result interface{}, err error) er |
| } |
| //////////////////////////////////////////////////////////////////////////////// |
| +// ListPackagesOptions mixin. |
|
Vadim Sh.
2015/06/23 00:08:35
no need for separate struct if it's not going to b
estaab
2015/06/23 02:26:13
Done.
|
| + |
| +// ListPackagesOptions defines a recursive option to list packages in subdirectories. |
| +type ListPackagesOptions struct { |
| + recursive bool |
| +} |
| + |
| +func (opts *ListPackagesOptions) registerFlags(f *flag.FlagSet) { |
| + f.BoolVar(&opts.recursive, "r", false, "Whether to list packages in subdirectories.") |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| // 'create' subcommand. |
| var cmdCreate = &subcommands.Command{ |
| @@ -339,7 +353,7 @@ type createRun struct { |
| } |
| func (c *createRun) Run(a subcommands.Application, args []string) int { |
| - if !checkCommandLine(args, c.GetFlags(), 0) { |
| + if !checkCommandLine(args, c.GetFlags(), 0, 0) { |
| return 1 |
| } |
| pin, err := buildAndUploadInstance(c.InputOptions, c.TagsOptions, c.ServiceOptions) |
| @@ -392,7 +406,7 @@ type ensureRun struct { |
| } |
| func (c *ensureRun) Run(a subcommands.Application, args []string) int { |
| - if !checkCommandLine(args, c.GetFlags(), 0) { |
| + if !checkCommandLine(args, c.GetFlags(), 0, 0) { |
| return 1 |
| } |
| err := ensurePackages(c.rootDir, c.listFile, c.ServiceOptions) |
| @@ -447,7 +461,7 @@ type resolveRun struct { |
| } |
| func (c *resolveRun) Run(a subcommands.Application, args []string) int { |
| - if !checkCommandLine(args, c.GetFlags(), 0) { |
| + if !checkCommandLine(args, c.GetFlags(), 0, 0) { |
| return 1 |
| } |
| pin, err := resolveVersion(c.packageName, c.version, c.ServiceOptions) |
| @@ -469,6 +483,62 @@ func resolveVersion(packageName, version string, serviceOpts ServiceOptions) (co |
| } |
| //////////////////////////////////////////////////////////////////////////////// |
| +// 'ls' subcommand. |
| + |
| +var cmdListPackages = &subcommands.Command{ |
| + UsageLine: "ls [-r] [<prefix string>]", |
| + ShortDesc: "List matching packages.", |
| + LongDesc: "List packages in the given path to which the user has access, optionally recursively", |
| + CommandRun: func() subcommands.CommandRun { |
| + c := &listPackagesRun{} |
| + c.ListPackagesOptions.registerFlags(&c.Flags) |
| + c.ServiceOptions.registerFlags(&c.Flags) |
| + return c |
| + }, |
| +} |
| + |
| +type listPackagesRun struct { |
| + subcommands.CommandRunBase |
| + ListPackagesOptions |
| + ServiceOptions |
| +} |
| + |
| +func (c *listPackagesRun) Run(a subcommands.Application, args []string) int { |
| + if !checkCommandLine(args, c.GetFlags(), 0, 1) { |
| + return 1 |
| + } |
| + path := "" |
| + if len(args) == 1 { |
| + path = args[0] |
| + } |
| + err := listPackages(path, c.ListPackagesOptions, c.ServiceOptions) |
| + if err != nil { |
| + log.Errorf("Error while listing packages: %s", err) |
| + return 1 |
| + } |
| + return 0 |
| +} |
| + |
| +func listPackages(path string, listOpts ListPackagesOptions, serviceOpts ServiceOptions) error { |
| + client, err := serviceOpts.makeCipdClient("") |
| + if err != nil { |
| + return err |
| + } |
| + packages, err := client.ListPackages(path, listOpts.recursive) |
| + if err != nil { |
| + return err |
| + } |
| + if len(packages) == 0 { |
| + log.Infof("No matching packages.") |
| + } else { |
| + for _, p := range packages { |
| + log.Infof("%s", p) |
| + } |
| + } |
| + return nil |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| // 'acl-list' subcommand. |
| var cmdListACL = &subcommands.Command{ |
| @@ -488,7 +558,7 @@ type listACLRun struct { |
| } |
| func (c *listACLRun) Run(a subcommands.Application, args []string) int { |
| - if !checkCommandLine(args, c.GetFlags(), 1) { |
| + if !checkCommandLine(args, c.GetFlags(), 1, 1) { |
| return 1 |
| } |
| err := listACL(args[0], c.ServiceOptions) |
| @@ -584,7 +654,7 @@ type editACLRun struct { |
| } |
| func (c *editACLRun) Run(a subcommands.Application, args []string) int { |
| - if !checkCommandLine(args, c.GetFlags(), 1) { |
| + if !checkCommandLine(args, c.GetFlags(), 1, 1) { |
| return 1 |
| } |
| err := editACL(args[0], c.owner, c.writer, c.reader, c.revoke, c.ServiceOptions) |
| @@ -657,7 +727,7 @@ type buildRun struct { |
| } |
| func (c *buildRun) Run(a subcommands.Application, args []string) int { |
| - if !checkCommandLine(args, c.GetFlags(), 0) { |
| + if !checkCommandLine(args, c.GetFlags(), 0, 0) { |
| return 1 |
| } |
| err := buildInstanceFile(c.outputFile, c.InputOptions) |
| @@ -720,7 +790,7 @@ type deployRun struct { |
| } |
| func (c *deployRun) Run(a subcommands.Application, args []string) int { |
| - if !checkCommandLine(args, c.GetFlags(), 1) { |
| + if !checkCommandLine(args, c.GetFlags(), 1, 1) { |
| return 1 |
| } |
| err := deployInstanceFile(c.rootDir, args[0]) |
| @@ -769,7 +839,7 @@ type fetchRun struct { |
| } |
| func (c *fetchRun) Run(a subcommands.Application, args []string) int { |
| - if !checkCommandLine(args, c.GetFlags(), 0) { |
| + if !checkCommandLine(args, c.GetFlags(), 0, 0) { |
| return 1 |
| } |
| err := fetchInstanceFile(c.packageName, c.version, c.outputPath, c.ServiceOptions) |
| @@ -840,7 +910,7 @@ type inspectRun struct { |
| } |
| func (c *inspectRun) Run(a subcommands.Application, args []string) int { |
| - if !checkCommandLine(args, c.GetFlags(), 1) { |
| + if !checkCommandLine(args, c.GetFlags(), 1, 1) { |
| return 1 |
| } |
| pin, err := inspectInstanceFile(args[0], true) |
| @@ -905,7 +975,7 @@ type registerRun struct { |
| } |
| func (c *registerRun) Run(a subcommands.Application, args []string) int { |
| - if !checkCommandLine(args, c.GetFlags(), 1) { |
| + if !checkCommandLine(args, c.GetFlags(), 1, 1) { |
| return 1 |
| } |
| pin, err := registerInstanceFile(args[0], c.TagsOptions, c.ServiceOptions) |
| @@ -949,6 +1019,7 @@ var application = &subcommands.DefaultApplication{ |
| subcommands.CmdHelp, |
| // High level commands. |
| + cmdListPackages, |
| cmdCreate, |
| cmdEnsure, |
| cmdResolve, |