mirror of
https://github.com/larksuite/cli.git
synced 2026-07-05 15:47:54 +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
219 lines
6.6 KiB
Go
219 lines
6.6 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package common
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/larksuite/cli/internal/cmdutil"
|
|
_ "github.com/larksuite/cli/internal/vfs/localfileio"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// newTestRuntimeWithStdin creates a RuntimeContext with string flags and a fake stdin.
|
|
func newTestRuntimeWithStdin(flags map[string]string, stdin string) *RuntimeContext {
|
|
cmd := &cobra.Command{Use: "test"}
|
|
for name := range flags {
|
|
cmd.Flags().String(name, "", "")
|
|
}
|
|
cmd.ParseFlags(nil)
|
|
for name, val := range flags {
|
|
cmd.Flags().Set(name, val)
|
|
}
|
|
return &RuntimeContext{
|
|
Cmd: cmd,
|
|
Factory: &cmdutil.Factory{
|
|
IOStreams: &cmdutil.IOStreams{
|
|
In: strings.NewReader(stdin),
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFlags_DirectValue(t *testing.T) {
|
|
rctx := newTestRuntimeWithStdin(map[string]string{"markdown": "hello world"}, "")
|
|
flags := []Flag{{Name: "markdown", Input: []string{File, Stdin}}}
|
|
|
|
if err := resolveInputFlags(rctx, flags); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got := rctx.Str("markdown"); got != "hello world" {
|
|
t.Errorf("expected %q, got %q", "hello world", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFlags_Stdin(t *testing.T) {
|
|
rctx := newTestRuntimeWithStdin(map[string]string{"markdown": "-"}, "content from stdin")
|
|
flags := []Flag{{Name: "markdown", Input: []string{File, Stdin}}}
|
|
|
|
if err := resolveInputFlags(rctx, flags); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got := rctx.Str("markdown"); got != "content from stdin" {
|
|
t.Errorf("expected %q, got %q", "content from stdin", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFlags_File(t *testing.T) {
|
|
dir := t.TempDir()
|
|
cmdutil.TestChdir(t, dir)
|
|
|
|
content := "## Hello\n\nThis is **markdown** from a file.\n"
|
|
if err := os.WriteFile("test.md", []byte(content), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rctx := newTestRuntimeWithStdin(map[string]string{"markdown": "@test.md"}, "")
|
|
flags := []Flag{{Name: "markdown", Input: []string{File, Stdin}}}
|
|
|
|
if err := resolveInputFlags(rctx, flags); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got := rctx.Str("markdown"); got != content {
|
|
t.Errorf("expected %q, got %q", content, got)
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFlags_EmptyFile(t *testing.T) {
|
|
dir := t.TempDir()
|
|
cmdutil.TestChdir(t, dir)
|
|
|
|
if err := os.WriteFile("empty.md", nil, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rctx := newTestRuntimeWithStdin(map[string]string{"markdown": "@empty.md"}, "")
|
|
flags := []Flag{{Name: "markdown", Input: []string{File, Stdin}}}
|
|
|
|
if err := resolveInputFlags(rctx, flags); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got := rctx.Str("markdown"); got != "" {
|
|
t.Errorf("expected empty string, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFlags_EmptyInput(t *testing.T) {
|
|
rctx := newTestRuntimeWithStdin(map[string]string{"markdown": ""}, "")
|
|
flags := []Flag{{Name: "markdown", Input: []string{File, Stdin}}}
|
|
|
|
if err := resolveInputFlags(rctx, flags); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got := rctx.Str("markdown"); got != "" {
|
|
t.Errorf("expected empty, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFlags_NoInputSpec(t *testing.T) {
|
|
rctx := newTestRuntimeWithStdin(map[string]string{"token": "@something"}, "")
|
|
flags := []Flag{{Name: "token"}} // no Input
|
|
|
|
if err := resolveInputFlags(rctx, flags); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
// value should be unchanged — no resolution
|
|
if got := rctx.Str("token"); got != "@something" {
|
|
t.Errorf("expected %q, got %q", "@something", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFlags_StdinNotSupported(t *testing.T) {
|
|
rctx := newTestRuntimeWithStdin(map[string]string{"data": "-"}, "stdin data")
|
|
flags := []Flag{{Name: "data", Input: []string{File}}} // only file, no stdin
|
|
|
|
err := resolveInputFlags(rctx, flags)
|
|
if err == nil {
|
|
t.Fatal("expected error for stdin not supported")
|
|
}
|
|
if !strings.Contains(err.Error(), "does not support stdin") {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFlags_FileNotSupported(t *testing.T) {
|
|
rctx := newTestRuntimeWithStdin(map[string]string{"data": "@file.txt"}, "")
|
|
flags := []Flag{{Name: "data", Input: []string{Stdin}}} // only stdin, no file
|
|
|
|
err := resolveInputFlags(rctx, flags)
|
|
if err == nil {
|
|
t.Fatal("expected error for file not supported")
|
|
}
|
|
if !strings.Contains(err.Error(), "does not support file input") {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFlags_FileNotFound(t *testing.T) {
|
|
dir := t.TempDir()
|
|
cmdutil.TestChdir(t, dir)
|
|
|
|
rctx := newTestRuntimeWithStdin(map[string]string{"markdown": "@nonexistent.md"}, "")
|
|
flags := []Flag{{Name: "markdown", Input: []string{File, Stdin}}}
|
|
|
|
err := resolveInputFlags(rctx, flags)
|
|
if err == nil {
|
|
t.Fatal("expected error for missing file")
|
|
}
|
|
if !strings.Contains(err.Error(), "cannot read file") {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFlags_EmptyFilePath(t *testing.T) {
|
|
rctx := newTestRuntimeWithStdin(map[string]string{"markdown": "@ "}, "")
|
|
flags := []Flag{{Name: "markdown", Input: []string{File, Stdin}}}
|
|
|
|
err := resolveInputFlags(rctx, flags)
|
|
if err == nil {
|
|
t.Fatal("expected error for empty file path")
|
|
}
|
|
if !strings.Contains(err.Error(), "file path cannot be empty after @") {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFlags_EscapeAtSign(t *testing.T) {
|
|
rctx := newTestRuntimeWithStdin(map[string]string{"text": "@@mention someone"}, "")
|
|
flags := []Flag{{Name: "text", Input: []string{File, Stdin}}}
|
|
|
|
if err := resolveInputFlags(rctx, flags); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got := rctx.Str("text"); got != "@mention someone" {
|
|
t.Errorf("expected %q, got %q", "@mention someone", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFlags_EscapeDoubleAt(t *testing.T) {
|
|
rctx := newTestRuntimeWithStdin(map[string]string{"text": "@@@triple"}, "")
|
|
flags := []Flag{{Name: "text", Input: []string{File, Stdin}}}
|
|
|
|
if err := resolveInputFlags(rctx, flags); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
// @@@ → strip first @, remaining is @@triple which is literal
|
|
if got := rctx.Str("text"); got != "@@triple" {
|
|
t.Errorf("expected %q, got %q", "@@triple", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFlags_DuplicateStdin(t *testing.T) {
|
|
rctx := newTestRuntimeWithStdin(map[string]string{"a": "-", "b": "-"}, "data")
|
|
flags := []Flag{
|
|
{Name: "a", Input: []string{Stdin}},
|
|
{Name: "b", Input: []string{Stdin}},
|
|
}
|
|
|
|
err := resolveInputFlags(rctx, flags)
|
|
if err == nil {
|
|
t.Fatal("expected error for duplicate stdin usage")
|
|
}
|
|
if !strings.Contains(err.Error(), "stdin (-) can only be used by one flag") {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
}
|