mirror of
https://github.com/larksuite/cli.git
synced 2026-07-04 06:29:52 +08:00
* feat(cmdutil): support @file for --params/--data (issue #705) Inline JSON values for --params/--data are mangled by Windows PowerShell 5's CommandLineToArgvW. Stdin (-) was the only escape hatch but supports just one flag at a time. Extend ResolveInput to accept @<path> (read JSON from a file) and @@... (escape for a literal @-prefixed value), mirroring the shortcuts framework's resolveInputFlags semantics. With this, both --params and --data can be sourced from files in the same call, sidestepping shell quoting on every platform. - internal/cmdutil/resolve.go: add @path / @@ handling, trim file content like stdin does, error on empty path or empty file - internal/cmdutil/resolve_test.go: cover file read, whitespace trim, missing file, empty path, empty content, @@ escape, plus ParseJSONMap / ParseOptionalBody integration through @file - cmd/api/api.go, cmd/service/service.go: update --params/--data help text to mention @file Change-Id: I366aa0f5783fbec6f05403f7f542505098a98c82 * refactor(cmdutil): route @file through fileio.FileIO abstraction The first cut of @file support called os.ReadFile directly inside ResolveInput, bypassing the codebase's fileio.FileIO abstraction (SafeInputPath validation, pluggable provider). That diverged from how every other file-reading path works: BuildFormdata for --file uploads and the shortcuts framework's resolveInputFlags both go through fileio.FileIO.Open with explicit fileio.ErrPathValidation handling. Re-route @file through the same path: - ResolveInput, ParseJSONMap, ParseOptionalBody now take a fileio.FileIO; @path uses fileIO.Open which goes through SafeInputPath (control-char rejection, abs-path rejection, symlink-escape check) — same security posture as --file - cmd/api and cmd/service callsites pass Factory.ResolveFileIO(ctx); the upload path now reuses the resolved fileIO instead of resolving twice - Path-validation errors surface as `--params: invalid file path "...": ...` distinct from `--params: cannot read file "...": ...` for genuine I/O errors - Nil fileIO with an @path returns a clear "file input (@path) is not available" error - Tests use localfileio.LocalFileIO with TestChdir(t, dir), matching the existing fileupload_test.go pattern; absolute-path rejection and nil-fileIO are covered This makes the feature behave identically under any FileIO provider (including server mode) instead of being silently bound to the local filesystem. Change-Id: I878c4e8fb03f43f1f19afad75ec3af9cdab7a7f9 * refactor(cmdutil): share at-file input handling Change-Id: I92a6eb6ea8fd02054bf8f4925cd81807449d5e51
67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmdutil
|
|
|
|
import "testing"
|
|
|
|
func TestParseOptionalBody(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
method string
|
|
data string
|
|
wantNil bool
|
|
wantErr bool
|
|
}{
|
|
{"GET ignored", "GET", `{"a":1}`, true, false},
|
|
{"POST empty data", "POST", "", true, false},
|
|
{"POST valid", "POST", `{"key":"val"}`, false, false},
|
|
{"PUT valid", "PUT", `[1,2,3]`, false, false},
|
|
{"PATCH valid", "PATCH", `"hello"`, false, false},
|
|
{"DELETE valid", "DELETE", `{"id":"1"}`, false, false},
|
|
{"POST invalid json", "POST", `{bad}`, true, true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := ParseOptionalBody(tt.method, tt.data, nil, nil)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("ParseOptionalBody() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if tt.wantNil && got != nil {
|
|
t.Errorf("ParseOptionalBody() = %v, want nil", got)
|
|
}
|
|
if !tt.wantNil && !tt.wantErr && got == nil {
|
|
t.Error("ParseOptionalBody() = nil, want non-nil")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseJSONMap(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
label string
|
|
wantLen int
|
|
wantErr bool
|
|
}{
|
|
{"empty input", "", "--params", 0, false},
|
|
{"valid json", `{"a":"1","b":"2"}`, "--params", 2, false},
|
|
{"invalid json", `{bad}`, "--params", 0, true},
|
|
{"json array", `[1,2]`, "--data", 0, true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := ParseJSONMap(tt.input, tt.label, nil, nil)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("ParseJSONMap() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !tt.wantErr && len(got) != tt.wantLen {
|
|
t.Errorf("ParseJSONMap() returned map with %d keys, want %d", len(got), tt.wantLen)
|
|
}
|
|
})
|
|
}
|
|
}
|