Skip to content

Commit

Permalink
Option to output config with regctl artifact get
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Mitchell <[email protected]>
  • Loading branch information
sudo-bmitch committed Jul 26, 2024
1 parent 435bf9c commit cedfa5a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
36 changes: 26 additions & 10 deletions cmd/regctl/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type artifactCmd struct {
formatList string
formatPut string
formatTree string
getConfig bool
index bool
latest bool
outputDir string
Expand Down Expand Up @@ -104,7 +105,10 @@ regctl artifact get registry.example.org/helm-charts/chart:0.0.1 > chart.tgz
regctl artifact get \
--subject ghcr.io/regclient/regsync:latest \
--filter-artifact-type application/spdx+json \
--platform local | jq .`,
--platform local | jq .
# retrieve the artifact config rather than the artifact itself
regctl artifact get registry.example.org/artifact:0.0.1 --config`,
Args: cobra.RangeArgs(0, 1),
ValidArgs: []string{}, // do not auto complete repository/tag
RunE: artifactOpts.runArtifactGet,
Expand Down Expand Up @@ -177,7 +181,8 @@ regctl artifact tree --digest-tags ghcr.io/regclient/regsync:latest`,
artifactGetCmd.Flags().StringVarP(&artifactOpts.platform, "platform", "p", "", "Specify platform of a subject (e.g. linux/amd64 or local)")
artifactGetCmd.Flags().StringVar(&artifactOpts.filterAT, "filter-artifact-type", "", "Filter referrers by artifactType")
artifactGetCmd.Flags().StringArrayVar(&artifactOpts.filterAnnot, "filter-annotation", []string{}, "Filter referrers by annotation (key=value)")
artifactGetCmd.Flags().StringVar(&artifactOpts.artifactConfig, "config-file", "", "Config filename to output")
artifactGetCmd.Flags().BoolVar(&artifactOpts.getConfig, "config", false, "Show the config, overrides file options")
artifactGetCmd.Flags().StringVar(&artifactOpts.artifactConfig, "config-file", "", "Output config to a file")
artifactGetCmd.Flags().StringArrayVarP(&artifactOpts.artifactFile, "file", "f", []string{}, "Filter by artifact filename")
artifactGetCmd.Flags().StringArrayVarP(&artifactOpts.artifactFileMT, "file-media-type", "m", []string{}, "Filter by artifact media-type")
_ = artifactGetCmd.RegisterFlagCompletionFunc("file-media-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down Expand Up @@ -360,7 +365,7 @@ func (artifactOpts *artifactCmd) runArtifactGet(cmd *cobra.Command, args []strin
}

// if config-file defined, create file as writer, perform a blob get
if artifactOpts.artifactConfig != "" {
if artifactOpts.artifactConfig != "" || artifactOpts.getConfig {
d, err := mi.GetConfig()
if err != nil {
return err
Expand All @@ -370,14 +375,25 @@ func (artifactOpts *artifactCmd) runArtifactGet(cmd *cobra.Command, args []strin
return err
}
defer rdr.Close()
fh, err := os.Create(artifactOpts.artifactConfig)
if err != nil {
return err
if artifactOpts.artifactConfig != "" {
fh, err := os.Create(artifactOpts.artifactConfig)
if err != nil {
return err
}
defer fh.Close()
_, err = io.Copy(fh, rdr)
if err != nil {
return err
}
} else {
_, err = io.Copy(cmd.OutOrStdout(), rdr)
if err != nil {
return err
}
}
defer fh.Close()
_, err = io.Copy(fh, rdr)
if err != nil {
return err
if artifactOpts.getConfig {
// do not return layer contents if request is only for a config
return nil
}
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/regctl/artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func TestArtifactGet(t *testing.T) {
args: []string{"artifact", "get", "ocidir://../../testdata/testrepo:ai", "--filter-annotation", "type=sbom"},
expectOut: "eggs",
},
{
name: "Get Config",
args: []string{"artifact", "get", "ocidir://../../testdata/testrepo:ai", "--filter-annotation", "type=sbom", "--config"},
expectOut: "{}",
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit cedfa5a

Please sign in to comment.