mirror of
https://github.com/larksuite/cli.git
synced 2026-09-14 18:42:53 +08:00
be2a96f490
Aggregate the sheets work from feat/lark-sheets-develop: - Improve validation errors with schema hints, aggregated issues, enum guidance, and prescriptive flag/style-field messages. - Harden +batch-update input contracts, key normalization, style vocabulary handling, and resource-budget checks. - Add read offload and truncation handling for cells, csv, and table-get, with typed output-path errors and safer jq/output-path semantics. - Correct freeze semantics by emitting full-state freeze/unfreeze operations and adding --rows/--cols for +dim-freeze. - Improve +styles-put and shared --styles parsing for styles, merges, row/column sizing, freeze, and sheet-prefixed range validation. - Fix dim-insert inherit-style mapping, table-get date/time handling, table-put style anchors, and CSV path-shaped input guards. - Update lark-sheets skill docs, scripts, tests, and generated flag data. Tested with: - go test ./shortcuts/common ./shortcuts/sheets/... - go test ./shortcuts/... ./internal/... - python3 -m py_compile skills/lark-sheets/scripts/*.py
107 lines
3.1 KiB
Go
107 lines
3.1 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.
|
|
// All paths go through the caller's fileIO provider and its relative-to-cwd
|
|
// policy — no absolute-path side door: a trust root defined by the process
|
|
// environment (TMPDIR) is not a security boundary, and reading outside the
|
|
// provider would break sidecar/custom-FileIO ownership. Out-of-tree content
|
|
// reaches flags via stdin ("-").
|
|
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)
|
|
}
|