github.com/zaquestion/lab@v0.25.1/cmd/ci_artifacts.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 8 "github.com/MakeNowJust/heredoc/v2" 9 "github.com/rsteube/carapace" 10 "github.com/spf13/cobra" 11 "github.com/zaquestion/lab/internal/action" 12 lab "github.com/zaquestion/lab/internal/gitlab" 13 ) 14 15 var ciArtifactsCmd = &cobra.Command{ 16 Use: "artifacts [remote] [branch][:job]", 17 Short: "Download artifacts of a ci job", 18 Long: heredoc.Doc(` 19 Download the CI pipeline job artifacts for the given or current branch if 20 none provided. 21 22 The branch name, when using with the --merge-request option, can be the 23 merge request number, which matches the branch name internally. The "job" 24 portion is the given job name, which may contain whitespace characters 25 and which, for this specific case, must be quoted.`), 26 Example: heredoc.Doc(` 27 lab ci artifacts upstream feature_branch 28 lab ci artifacts upstream :'my custom stage' 29 lab ci artifacts upstream 125 --merge-request 30 lab ci artifacts upstream 125:'my custom stage' --merge-request 31 lab ci artifacts upstream 125:'build' --merge-request --bridge 'security-tests'`), 32 PersistentPreRun: labPersistentPreRun, 33 Run: func(cmd *cobra.Command, args []string) { 34 var ( 35 rn string 36 jobName string 37 err error 38 ) 39 jobName, branchArgs, err := filterJobArg(args) 40 if err != nil { 41 log.Fatal(err) 42 } 43 44 forMR, err := cmd.Flags().GetBool("merge-request") 45 if err != nil { 46 log.Fatal(err) 47 } 48 49 bridgeName, err = cmd.Flags().GetString("bridge") 50 if err != nil { 51 log.Fatal(err) 52 } else if bridgeName != "" { 53 followBridge = true 54 } else { 55 followBridge, err = cmd.Flags().GetBool("follow") 56 if err != nil { 57 log.Fatal(err) 58 } 59 } 60 61 path, err := cmd.Flags().GetString("artifact-path") 62 if err != nil { 63 log.Fatal(err) 64 } 65 66 rn, pipelineID, err := getPipelineFromArgs(branchArgs, forMR) 67 if err != nil { 68 log.Fatal(err) 69 } 70 71 r, outpath, err := lab.CIArtifacts(rn, pipelineID, jobName, path, followBridge, bridgeName) 72 if err != nil { 73 log.Fatal(err) 74 } 75 76 dst, err := os.Create(outpath) 77 if err != nil { 78 log.Fatal(err) 79 } 80 81 _, err = io.Copy(dst, r) 82 if err != nil { 83 log.Fatal(err) 84 } 85 86 fmt.Printf("Downloaded %s\n", outpath) 87 }, 88 } 89 90 func init() { 91 ciArtifactsCmd.Flags().Bool("merge-request", false, "use merge request pipeline if enabled") 92 ciArtifactsCmd.Flags().StringP("artifact-path", "p", "", "only download specified file from archive") 93 ciCmd.AddCommand(ciArtifactsCmd) 94 carapace.Gen(ciArtifactsCmd).PositionalCompletion( 95 action.Remotes(), 96 action.RemoteBranches(0), 97 ) 98 }