diff --git a/pkg/cmd/flagoptions.go b/pkg/cmd/flagoptions.go index b314e9a..3b018fe 100644 --- a/pkg/cmd/flagoptions.go +++ b/pkg/cmd/flagoptions.go @@ -42,6 +42,9 @@ const ( func embedFiles(obj any, embedStyle FileEmbedStyle) (any, error) { v := reflect.ValueOf(obj) + if !v.IsValid() { + return nil, nil + } result, err := embedFilesValue(v, embedStyle) if err != nil { return nil, err @@ -51,6 +54,10 @@ func embedFiles(obj any, embedStyle FileEmbedStyle) (any, error) { // Replace "@file.txt" with the file's contents inside a value func embedFilesValue(v reflect.Value, embedStyle FileEmbedStyle) (reflect.Value, error) { + if !v.IsValid() { + return v, nil + } + // Unwrap interface values to get the concrete type if v.Kind() == reflect.Interface { if v.IsNil() { diff --git a/pkg/cmd/flagoptions_test.go b/pkg/cmd/flagoptions_test.go index e5dad4b..0567019 100644 --- a/pkg/cmd/flagoptions_test.go +++ b/pkg/cmd/flagoptions_test.go @@ -6,8 +6,10 @@ import ( "path/filepath" "testing" + "github.com/beeper/desktop-api-cli/internal/apiquery" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/urfave/cli/v3" ) func TestIsUTF8TextFile(t *testing.T) { @@ -212,6 +214,12 @@ func TestEmbedFiles(t *testing.T) { want: []any{1, 2, 3, 4, 5}, wantErr: false, }, + { + name: "nil input unchanged", + input: nil, + want: nil, + wantErr: false, + }, } for _, tt := range tests { @@ -236,6 +244,30 @@ func TestEmbedFiles(t *testing.T) { } } +func TestFlagOptionsWithEmptyPipedStdinAndNoFlags(t *testing.T) { + oldStdin := os.Stdin + r, w, err := os.Pipe() + require.NoError(t, err) + require.NoError(t, w.Close()) + os.Stdin = r + t.Cleanup(func() { + os.Stdin = oldStdin + _ = r.Close() + }) + + cmd := &cli.Command{Name: "test"} + + opts, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatRepeat, + EmptyBody, + false, + ) + require.NoError(t, err) + assert.Empty(t, opts) +} + func writeTestFile(t *testing.T, dir, filename, content string) { t.Helper() path := filepath.Join(dir, filename)