go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/artifacts/list.go (about) 1 // Copyright 2019 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package main 6 7 import ( 8 "context" 9 "flag" 10 "fmt" 11 "log" 12 13 "github.com/google/subcommands" 14 "go.chromium.org/luci/auth/client/authcli" 15 "go.chromium.org/luci/hardcoded/chromeinfra" 16 "go.fuchsia.dev/infra/artifacts" 17 "go.fuchsia.dev/infra/buildbucket" 18 ) 19 20 type ListCommand struct { 21 build string 22 authFlags authcli.Flags 23 } 24 25 func (*ListCommand) Name() string { 26 return "ls" 27 } 28 29 func (*ListCommand) Usage() string { 30 return "ls [flags]" 31 } 32 33 func (*ListCommand) Synopsis() string { 34 return "lists the set of artifacts produced by a build" 35 } 36 37 func (cmd *ListCommand) SetFlags(f *flag.FlagSet) { 38 cmd.authFlags.Register(flag.CommandLine, chromeinfra.DefaultAuthOptions()) 39 f.StringVar(&cmd.build, "build", "", "the ID of the build that produced the artifacts") 40 } 41 42 func (cmd *ListCommand) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) subcommands.ExitStatus { 43 if err := cmd.validateAndExecute(ctx); err != nil { 44 log.Println(err) 45 return subcommands.ExitFailure 46 } 47 return subcommands.ExitSuccess 48 } 49 50 func (cmd *ListCommand) validateAndExecute(ctx context.Context) error { 51 opts, err := cmd.authFlags.Options() 52 if err != nil { 53 return err 54 } 55 56 buildsCli, err := buildbucket.NewBuildsClient(ctx, buildbucket.DefaultHost, opts) 57 if err != nil { 58 return fmt.Errorf("failed to create builds client: %w", err) 59 } 60 61 artifactsCli, err := artifacts.NewClient(ctx, opts) 62 if err != nil { 63 return fmt.Errorf("failed to create artifacts client: %w", err) 64 } 65 66 return cmd.execute(ctx, buildsCli, artifactsCli) 67 } 68 69 func (cmd *ListCommand) execute(ctx context.Context, buildsCli buildsClient, artifactsCli *artifacts.Client) error { 70 bucket, err := getStorageBucket(ctx, buildsCli, cmd.build) 71 if err != nil { 72 return err 73 } 74 75 dir := artifactsCli.GetBuildDir(bucket, cmd.build) 76 items, err := dir.List(ctx, "") 77 if err != nil { 78 return err 79 } 80 81 for _, item := range items { 82 fmt.Println(item) 83 } 84 85 return nil 86 }