github.com/stevenmatthewt/agent@v3.5.4+incompatible/clicommand/artifact_shasum.go (about)

     1  package clicommand
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/buildkite/agent/agent"
     7  	"github.com/buildkite/agent/cliconfig"
     8  	"github.com/buildkite/agent/logger"
     9  	"github.com/urfave/cli"
    10  )
    11  
    12  var ShasumHelpDescription = `Usage:
    13  
    14     buildkite-agent artifact shasum [arguments...]
    15  
    16  Description:
    17  
    18     Prints to STDOUT the SHA-1 for the artifact provided. If your search query
    19     for artifacts matches multiple agents, and error will be raised.
    20  
    21     Note: You need to ensure that your search query is surrounded by quotes if
    22     using a wild card as the built-in shell path globbing will provide files,
    23     which will break the download.
    24  
    25  Example:
    26  
    27     $ buildkite-agent artifact shasum "pkg/release.tar.gz" --build xxx
    28  
    29     This will search for all the files in the build with the path "pkg/release.tar.gz" and will
    30     print to STDOUT it's SHA-1 checksum.
    31  
    32     If you would like to target artifacts from a specific build step, you can do
    33     so by using the --step argument.
    34  
    35     $ buildkite-agent artifact shasum "pkg/release.tar.gz" --step "release" --build xxx
    36  
    37     You can also use the step's job id (provided by the environment variable $BUILDKITE_JOB_ID)`
    38  
    39  type ArtifactShasumConfig struct {
    40  	Query            string `cli:"arg:0" label:"artifact search query" validate:"required"`
    41  	Step             string `cli:"step"`
    42  	Build            string `cli:"build" validate:"required"`
    43  	AgentAccessToken string `cli:"agent-access-token" validate:"required"`
    44  	Endpoint         string `cli:"endpoint" validate:"required"`
    45  	NoColor          bool   `cli:"no-color"`
    46  	Debug            bool   `cli:"debug"`
    47  	DebugHTTP        bool   `cli:"debug-http"`
    48  }
    49  
    50  var ArtifactShasumCommand = cli.Command{
    51  	Name:        "shasum",
    52  	Usage:       "Prints the SHA-1 checksum for the artifact provided to STDOUT",
    53  	Description: ShasumHelpDescription,
    54  	Flags: []cli.Flag{
    55  		cli.StringFlag{
    56  			Name:  "step",
    57  			Value: "",
    58  			Usage: "Scope the search to a paticular step by using either it's name of job ID",
    59  		},
    60  		cli.StringFlag{
    61  			Name:   "build",
    62  			Value:  "",
    63  			EnvVar: "BUILDKITE_BUILD_ID",
    64  			Usage:  "The build that the artifacts were uploaded to",
    65  		},
    66  		AgentAccessTokenFlag,
    67  		EndpointFlag,
    68  		NoColorFlag,
    69  		DebugFlag,
    70  		DebugHTTPFlag,
    71  	},
    72  	Action: func(c *cli.Context) {
    73  		// The configuration will be loaded into this struct
    74  		cfg := ArtifactShasumConfig{}
    75  
    76  		// Load the configuration
    77  		if err := cliconfig.Load(c, &cfg); err != nil {
    78  			logger.Fatal("%s", err)
    79  		}
    80  
    81  		// Setup the any global configuration options
    82  		HandleGlobalFlags(cfg)
    83  
    84  		// Find the artifact we want to show the SHASUM for
    85  		searcher := agent.ArtifactSearcher{
    86  			APIClient: agent.APIClient{
    87  				Endpoint: cfg.Endpoint,
    88  				Token:    cfg.AgentAccessToken,
    89  			}.Create(),
    90  			BuildID: cfg.Build,
    91  		}
    92  
    93  		artifacts, err := searcher.Search(cfg.Query, cfg.Step)
    94  		if err != nil {
    95  			logger.Fatal("Failed to find artifacts: %s", err)
    96  		}
    97  
    98  		artifactsFoundLength := len(artifacts)
    99  
   100  		if artifactsFoundLength == 0 {
   101  			logger.Fatal("No artifacts found for downloading")
   102  		} else if artifactsFoundLength > 1 {
   103  			logger.Fatal("Multiple artifacts were found. Try being more specific with the search or scope by step")
   104  		} else {
   105  			logger.Debug("Artifact \"%s\" found", artifacts[0].Path)
   106  
   107  			fmt.Printf("%s\n", artifacts[0].Sha1Sum)
   108  		}
   109  	},
   110  }