Skip to content

Commit

Permalink
OPCT-292: cmd/publish - add metadata option when publishing artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
mtulio committed Jul 26, 2024
1 parent 1d0e7b4 commit 755f473
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions pkg/cmd/exp/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type submitInput struct {
bucketName string
bucketRegion string
objectKey string
metadata string
}

var argsPublish submitInput
Expand All @@ -37,6 +38,10 @@ func init() {
&argsPublish.objectKey, "key", "k", "",
"Object key to use when uploading the archive to the bucket, when not set the uploads/ path will be prepended to the filename.",
)
cmdPublish.Flags().StringVarP(
&argsPublish.metadata, "metadata", "m", "",
"Object metadata.",
)
}

func cmdPublishRun(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -71,6 +76,12 @@ func checkRequiredParams(input *submitInput) bool {

input.bucketName = "openshift-provider-certification"
input.bucketRegion = "us-west-2"
if os.Getenv("OPCT_EXP_BUCKET_NAME") != "" {
input.bucketName = os.Getenv("OPCT_EXP_BUCKET_NAME")
}
if os.Getenv("OPCT_EXP_BUCKET_REGION") != "" {
input.bucketRegion = os.Getenv("OPCT_EXP_BUCKET_REGION")
}

return true
}
Expand Down Expand Up @@ -126,10 +137,26 @@ func publishResult(input *submitInput) error {
}
objectKey = input.objectKey
}

// when metadata is set, parse it and add it to the object
metadata := make(map[string]string)
if input.metadata != "" {
metadataParts := strings.Split(input.metadata, ",")
for _, part := range metadataParts {
kv := strings.Split(part, "=")
if len(kv) != 2 {
return fmt.Errorf("metadata must be in the form key1=value1,key2=value2")
}
metadata[kv[0]] = kv[1]
}
}

// upload artifact to bucket
_, err = svc.PutObject(&s3.PutObjectInput{
Bucket: aws.String(input.bucketName),
Key: aws.String(objectKey),
Body: file,
Bucket: aws.String(input.bucketName),
Key: aws.String(objectKey),
Body: file,
Metadata: aws.StringMap(metadata),
})
if err != nil {
return errors.Wrapf(err, "failed to upload file %s to bucket %s", filename, input.bucketName)
Expand Down

0 comments on commit 755f473

Please sign in to comment.