-
Notifications
You must be signed in to change notification settings - Fork 2
feat(features): add info manifest and info tags subcommands #289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6cb3fd8
0718774
f1977e5
b3f39ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package features | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/devsy-org/devsy/cmd/flags" | ||
| "github.com/devsy-org/devsy/pkg/devcontainer/feature" | ||
| "github.com/google/go-containerregistry/pkg/name" | ||
| v1 "github.com/google/go-containerregistry/pkg/v1" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type InfoManifestCmd struct { | ||
| *flags.GlobalFlags | ||
|
|
||
| Output string | ||
| } | ||
|
|
||
| func NewInfoManifestCmd(globalFlags *flags.GlobalFlags) *cobra.Command { | ||
| cmd := &InfoManifestCmd{GlobalFlags: globalFlags} | ||
| manifestCmd := &cobra.Command{ | ||
| Use: "manifest <feature-id>", | ||
| Short: "Display the OCI manifest for a published feature", | ||
| Long: `Fetch and display the raw OCI manifest for a published dev container feature. | ||
|
|
||
| Accepts a feature ID like ghcr.io/devcontainers/features/go or | ||
| ghcr.io/devcontainers/features/go:1 and outputs the OCI image manifest.`, | ||
| Args: cobra.ExactArgs(1), | ||
| SilenceUsage: true, | ||
| SilenceErrors: true, | ||
| RunE: func(_ *cobra.Command, args []string) error { | ||
| return cmd.Run(args[0]) | ||
| }, | ||
| } | ||
|
|
||
| manifestCmd.Flags().StringVar(&cmd.Output, "output", "json", "Output format (text or json)") | ||
|
|
||
| return manifestCmd | ||
| } | ||
|
|
||
| func (cmd *InfoManifestCmd) Run(featureID string) error { | ||
| if err := validateOutputFormat(cmd.Output); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| ref, err := name.ParseReference(featureID) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid feature reference %q: %w", featureID, err) | ||
| } | ||
|
|
||
| manifest, err := feature.FetchOCIManifest(ref) | ||
| if err != nil { | ||
| return fmt.Errorf("fetch manifest: %w", err) | ||
| } | ||
|
|
||
| if cmd.Output == outputJSON { | ||
| return writeJSON(os.Stdout, manifest) | ||
| } | ||
| return cmd.printText(manifest) | ||
| } | ||
|
|
||
| func (cmd *InfoManifestCmd) printText(manifest *v1.Manifest) error { | ||
| w := os.Stdout | ||
| _, _ = fmt.Fprintf(w, "Schema Version: %d\n", manifest.SchemaVersion) | ||
| _, _ = fmt.Fprintf(w, "Media Type: %s\n", manifest.MediaType) | ||
|
|
||
| _, _ = fmt.Fprintf(w, "\nConfig:\n") | ||
| _, _ = fmt.Fprintf(w, " Media Type: %s\n", manifest.Config.MediaType) | ||
| _, _ = fmt.Fprintf(w, " Digest: %s\n", manifest.Config.Digest) | ||
| _, _ = fmt.Fprintf(w, " Size: %d\n", manifest.Config.Size) | ||
|
|
||
| if len(manifest.Layers) > 0 { | ||
| _, _ = fmt.Fprintf(w, "\nLayers:\n") | ||
| for i, layer := range manifest.Layers { | ||
| _, _ = fmt.Fprintf(w, " [%d] Media Type: %s\n", i, layer.MediaType) | ||
| _, _ = fmt.Fprintf(w, " Digest: %s\n", layer.Digest) | ||
| _, _ = fmt.Fprintf(w, " Size: %d\n", layer.Size) | ||
| } | ||
| } | ||
|
|
||
| if len(manifest.Annotations) > 0 { | ||
| _, _ = fmt.Fprintf(w, "\nAnnotations:\n") | ||
| for k, v := range manifest.Annotations { | ||
| _, _ = fmt.Fprintf(w, " %s: %s\n", k, v) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package features | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestInfoManifestCmd_FlagDefaults(t *testing.T) { | ||
| cmd := NewInfoManifestCmd(nil) | ||
|
|
||
| outputFlag := cmd.Flags().Lookup("output") | ||
| require.NotNil(t, outputFlag) | ||
| assert.Equal(t, "json", outputFlag.DefValue) | ||
| } | ||
|
|
||
| func TestInfoManifestCmd_RequiresExactlyOneArg(t *testing.T) { | ||
| cmd := NewInfoManifestCmd(nil) | ||
| assert.NotNil(t, cmd.Args) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| args []string | ||
| wantErr bool | ||
| }{ | ||
| {name: "no args", args: []string{}, wantErr: true}, | ||
| {name: "one arg", args: []string{"ghcr.io/devcontainers/features/go"}, wantErr: false}, | ||
| {name: "two args", args: []string{"a", "b"}, wantErr: true}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := cmd.Args(cmd, tt.args) | ||
| if tt.wantErr { | ||
| assert.Error(t, err) | ||
| } else { | ||
| assert.NoError(t, err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestInfoManifestCmd_InvalidOutputFormat(t *testing.T) { | ||
| cmd := &InfoManifestCmd{Output: "xml"} | ||
| err := cmd.Run("ghcr.io/devcontainers/features/go:1") | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "invalid output format") | ||
| } | ||
|
|
||
| func TestInfoManifestCmd_InvalidFeatureReference(t *testing.T) { | ||
| cmd := &InfoManifestCmd{Output: outputJSON} | ||
| err := cmd.Run("not a valid reference!!!") | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "invalid feature reference") | ||
| } | ||
|
|
||
| func TestInfoManifestCmd_CommandMetadata(t *testing.T) { | ||
| cmd := NewInfoManifestCmd(nil) | ||
| assert.Equal(t, "manifest <feature-id>", cmd.Use) | ||
| assert.NotEmpty(t, cmd.Short) | ||
| assert.NotEmpty(t, cmd.Long) | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,73 @@ | ||||||||||||||||||||||||||||||||||||||||||
| package features | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| "github.com/devsy-org/devsy/cmd/flags" | ||||||||||||||||||||||||||||||||||||||||||
| "github.com/google/go-containerregistry/pkg/name" | ||||||||||||||||||||||||||||||||||||||||||
| "github.com/spf13/cobra" | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| type InfoTagsCmd struct { | ||||||||||||||||||||||||||||||||||||||||||
| *flags.GlobalFlags | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| Output string | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| type tagsOutput struct { | ||||||||||||||||||||||||||||||||||||||||||
| Tags []string `json:"tags"` | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func NewInfoTagsCmd(globalFlags *flags.GlobalFlags) *cobra.Command { | ||||||||||||||||||||||||||||||||||||||||||
| cmd := &InfoTagsCmd{GlobalFlags: globalFlags} | ||||||||||||||||||||||||||||||||||||||||||
| tagsCmd := &cobra.Command{ | ||||||||||||||||||||||||||||||||||||||||||
| Use: "tags <feature-id>", | ||||||||||||||||||||||||||||||||||||||||||
| Short: "List available tags for a published feature", | ||||||||||||||||||||||||||||||||||||||||||
| Long: `List all available tags from the registry for a published dev container feature. | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| Accepts a feature ID like ghcr.io/devcontainers/features/go and | ||||||||||||||||||||||||||||||||||||||||||
| queries the registry for all available image tags.`, | ||||||||||||||||||||||||||||||||||||||||||
| Args: cobra.ExactArgs(1), | ||||||||||||||||||||||||||||||||||||||||||
| SilenceUsage: true, | ||||||||||||||||||||||||||||||||||||||||||
| SilenceErrors: true, | ||||||||||||||||||||||||||||||||||||||||||
| RunE: func(_ *cobra.Command, args []string) error { | ||||||||||||||||||||||||||||||||||||||||||
| return cmd.Run(args[0]) | ||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| tagsCmd.Flags().StringVar(&cmd.Output, "output", "text", "Output format (text or json)") | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return tagsCmd | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func (cmd *InfoTagsCmd) Run(featureID string) error { | ||||||||||||||||||||||||||||||||||||||||||
| if err := validateOutputFormat(cmd.Output); err != nil { | ||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ref, err := name.ParseReference(featureID) | ||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("invalid feature reference %q: %w", featureID, err) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| tags, err := listTags(ref) | ||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("list tags: %w", err) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if cmd.Output == outputJSON { | ||||||||||||||||||||||||||||||||||||||||||
| return writeJSON(os.Stdout, &tagsOutput{Tags: tags}) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| w := os.Stdout | ||||||||||||||||||||||||||||||||||||||||||
| if len(tags) == 0 { | ||||||||||||||||||||||||||||||||||||||||||
| _, _ = fmt.Fprintln(w, "No tags found.") | ||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| _, _ = fmt.Fprintln(w, "Available Tags:") | ||||||||||||||||||||||||||||||||||||||||||
| _, _ = fmt.Fprintf(w, " %s\n", strings.Join(tags, ", ")) | ||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+66
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Propagate stdout write failures. Text output ignores write errors, so broken pipe/I/O failures can still return success. Please return these errors so CLI exit status stays accurate. Proposed fix w := os.Stdout
if len(tags) == 0 {
- _, _ = fmt.Fprintln(w, "No tags found.")
+ if _, err := fmt.Fprintln(w, "No tags found."); err != nil {
+ return err
+ }
return nil
}
- _, _ = fmt.Fprintln(w, "Available Tags:")
- _, _ = fmt.Fprintf(w, " %s\n", strings.Join(tags, ", "))
+ if _, err := fmt.Fprintln(w, "Available Tags:"); err != nil {
+ return err
+ }
+ if _, err := fmt.Fprintf(w, " %s\n", strings.Join(tags, ", ")); err != nil {
+ return err
+ }
return nil
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package features | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestInfoTagsCmd_FlagDefaults(t *testing.T) { | ||
| cmd := NewInfoTagsCmd(nil) | ||
|
|
||
| outputFlag := cmd.Flags().Lookup("output") | ||
| require.NotNil(t, outputFlag) | ||
| assert.Equal(t, outputText, outputFlag.DefValue) | ||
| } | ||
|
|
||
| func TestInfoTagsCmd_RequiresExactlyOneArg(t *testing.T) { | ||
| cmd := NewInfoTagsCmd(nil) | ||
| assert.NotNil(t, cmd.Args) | ||
| assert.Error(t, cmd.Args(cmd, nil)) | ||
| assert.NoError(t, cmd.Args(cmd, []string{"ghcr.io/devcontainers/features/go"})) | ||
| assert.Error(t, cmd.Args(cmd, []string{"first", "second"})) | ||
| } | ||
|
|
||
| func TestInfoTagsCmd_InvalidOutputFormat(t *testing.T) { | ||
| cmd := &InfoTagsCmd{Output: "csv"} | ||
| err := cmd.Run("ghcr.io/devcontainers/features/go:1") | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "invalid output format") | ||
| } | ||
|
|
||
| func TestInfoTagsCmd_InvalidFeatureReference(t *testing.T) { | ||
| cmd := &InfoTagsCmd{Output: outputText} | ||
| err := cmd.Run("not a valid reference!!!") | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "invalid feature reference") | ||
| } | ||
|
|
||
| func TestInfoTagsCmd_CommandMetadata(t *testing.T) { | ||
| cmd := NewInfoTagsCmd(nil) | ||
| assert.Equal(t, "tags <feature-id>", cmd.Use) | ||
| assert.Contains(t, cmd.Short, "tags") | ||
| assert.NotEmpty(t, cmd.Long) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 783
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 1051
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 166
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 1477
Handle stdout write failures instead of always returning success.
printTextdeclares an error return type but always returnsnil, ignoring allfmt.Fprintferrors. If output fails (broken pipe, disk full, etc.), the command will still report success. All 12 fprintf calls on lines 65–85 use_, _to discard errors.🤖 Prompt for AI Agents