Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/features/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ to display metadata.`,
&cmd.ShowDependencies, "show-dependencies", false, "Show declared dependencies",
)

infoCmd.AddCommand(NewInfoManifestCmd(globalFlags))
infoCmd.AddCommand(NewInfoTagsCmd(globalFlags))

return infoCmd
}

Expand Down
90 changes: 90 additions & 0 deletions cmd/features/info_manifest.go
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
Comment on lines +64 to +89

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify ignored write errors in this command implementation.
rg -nP '_,\s*_\s*=\s*fmt\.Fprintf' cmd/features/info_manifest.go

Repository: devsy-org/devsy

Length of output: 783


🏁 Script executed:

# Check the full printText function implementation and its signature
sed -n '60,95p' cmd/features/info_manifest.go

Repository: devsy-org/devsy

Length of output: 1051


🏁 Script executed:

# Search for calls to printText to see if error handling is done at the call site
rg -n 'printText' cmd/features/info_manifest.go

Repository: devsy-org/devsy

Length of output: 166


🏁 Script executed:

# Check the Run method to see how printText errors are handled
sed -n '20,65p' cmd/features/info_manifest.go

Repository: devsy-org/devsy

Length of output: 1477


Handle stdout write failures instead of always returning success.

printText declares an error return type but always returns nil, ignoring all fmt.Fprintf errors. 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
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cmd/features/info_manifest.go` around lines 64 - 89, The printText function
currently discards all fmt.Fprintf errors (using "_, _") when writing manifest
fields to w (stdout), so it always returns nil even if writes fail; change each
fmt.Fprintf call (the ones printing Schema Version, Media Type, Config fields,
Layers loop, and Annotations loop that reference manifest.SchemaVersion,
manifest.MediaType, manifest.Config, manifest.Layers, and manifest.Annotations)
to capture the error (e.g., "_, err :=" or "if _, err := fmt.Fprintf(...); err
!= nil { return err }") and immediately return the error when any write fails so
the command surfaces write failures instead of always reporting success.

}
62 changes: 62 additions & 0 deletions cmd/features/info_manifest_test.go
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)
}
73 changes: 73 additions & 0 deletions cmd/features/info_tags.go
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
_, _ = fmt.Fprintln(w, "No tags found.")
return nil
}
_, _ = fmt.Fprintln(w, "Available Tags:")
_, _ = fmt.Fprintf(w, " %s\n", strings.Join(tags, ", "))
return nil
if _, err := fmt.Fprintln(w, "No tags found."); err != nil {
return err
}
return nil
}
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
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cmd/features/info_tags.go` around lines 66 - 72, The printing code in the
info_tags handler ignores write errors from fmt.Fprintln and fmt.Fprintf (using
"_, _ ="), which hides broken pipe/I/O failures; change the three writes that
target writer w to capture their errors (e.g., err := fmt.Fprintln(w, ...)) and
propagate them by returning the first non-nil error so the CLI exits with
failure on write errors—update the writes that print "No tags found.",
"Available Tags:", and the tags line (the calls using w and strings.Join(tags,
", ")) to return any write error instead of discarding it.

}
45 changes: 45 additions & 0 deletions cmd/features/info_tags_test.go
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)
}
10 changes: 10 additions & 0 deletions cmd/features/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,13 @@ func TestInfoCmd_InvalidFeatureReference(t *testing.T) {
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid feature reference")
}

func TestInfoCmd_HasSubcommands(t *testing.T) {
cmd := NewInfoCmd(nil)
subcommands := make(map[string]bool)
for _, sub := range cmd.Commands() {
subcommands[sub.Name()] = true
}
assert.True(t, subcommands["manifest"], "info should have 'manifest' subcommand")
assert.True(t, subcommands["tags"], "info should have 'tags' subcommand")
}
Loading
Loading