mirror of
https://github.com/larksuite/cli.git
synced 2026-07-03 22:24:31 +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
102 lines
2.8 KiB
Go
102 lines
2.8 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmdutil
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/larksuite/cli/extension/fileio"
|
|
)
|
|
|
|
// ResolveInput resolves special input conventions for a raw flag value:
|
|
// - "-" → read all bytes from stdin
|
|
// - "@<path>" → read all bytes from the file at <path> via fileIO
|
|
// - "@@..." → strip leading @ (escape for a literal @-prefixed value)
|
|
// - "'...'" → strip surrounding single quotes (Windows cmd.exe compatibility)
|
|
// - other → return as-is
|
|
//
|
|
// fileIO is required for "@<path>" inputs and goes through path validation
|
|
// (SafeInputPath); pass nil only when callers know "@" inputs are not possible.
|
|
//
|
|
// Allows callers to bypass shell quoting issues (especially Windows PowerShell 5)
|
|
// by reading JSON from a file (@path) or piping via stdin (-).
|
|
func ResolveInput(raw string, stdin io.Reader, fileIO fileio.FileIO) (string, error) {
|
|
if raw == "" {
|
|
return "", nil
|
|
}
|
|
|
|
// stdin
|
|
if raw == "-" {
|
|
if stdin == nil {
|
|
return "", fmt.Errorf("stdin is not available")
|
|
}
|
|
data, err := io.ReadAll(stdin)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to read stdin: %w", err)
|
|
}
|
|
s := strings.TrimSpace(string(data))
|
|
if s == "" {
|
|
return "", fmt.Errorf("stdin is empty (did you forget to pipe input?)")
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
// escape: @@... → literal @... (no file read)
|
|
if strings.HasPrefix(raw, "@@") {
|
|
return raw[1:], nil
|
|
}
|
|
|
|
// file: @path
|
|
if strings.HasPrefix(raw, "@") {
|
|
path := strings.TrimSpace(raw[1:])
|
|
if path == "" {
|
|
return "", fmt.Errorf("file path cannot be empty after @")
|
|
}
|
|
data, err := ReadInputFile(fileIO, path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
s := strings.TrimSpace(string(data))
|
|
if s == "" {
|
|
return "", fmt.Errorf("file %q is empty", path)
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
// strip surrounding single quotes (Windows cmd.exe passes them literally)
|
|
if len(raw) >= 2 && raw[0] == '\'' && raw[len(raw)-1] == '\'' {
|
|
raw = raw[1 : len(raw)-1]
|
|
}
|
|
|
|
return raw, nil
|
|
}
|
|
|
|
// ReadInputFile reads path through fileIO. Open/read failures are wrapped with
|
|
// path context; fileio.ErrPathValidation remains matchable with errors.Is.
|
|
func ReadInputFile(fileIO fileio.FileIO, path string) ([]byte, error) {
|
|
if fileIO == nil {
|
|
return nil, fmt.Errorf("file input is not available in this context")
|
|
}
|
|
f, err := fileIO.Open(path)
|
|
if err != nil {
|
|
return nil, wrapInputFileError(path, err)
|
|
}
|
|
defer f.Close()
|
|
data, err := io.ReadAll(f)
|
|
if err != nil {
|
|
return nil, wrapInputFileError(path, err)
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
func wrapInputFileError(path string, err error) error {
|
|
if errors.Is(err, fileio.ErrPathValidation) {
|
|
return fmt.Errorf("invalid file path %q: %w", path, err)
|
|
}
|
|
return fmt.Errorf("cannot read file %q: %w", path, err)
|
|
}
|