feat(cli): add interactive config input and Makefile
- support interactive value input for `config set` - hide API key input and output - add Makefile for build, test, vet, coverage, and cleanup - add golang.org/x/term dependency - update changelog and gitignore
This commit is contained in:
@@ -41,3 +41,8 @@ opencode.json
|
|||||||
.claude/
|
.claude/
|
||||||
.opencode/
|
.opencode/
|
||||||
.hermers/
|
.hermers/
|
||||||
|
|
||||||
|
# Build output (Makefile)
|
||||||
|
bin/
|
||||||
|
coverage.out
|
||||||
|
coverage.html
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
.PHONY: all build test vet fmt lint cover install tidy clean help
|
||||||
|
|
||||||
|
BINARY := ry
|
||||||
|
MAIN_PKG := ./cmd/ry
|
||||||
|
GO := go
|
||||||
|
GOFLAGS :=
|
||||||
|
OUTPUT := bin/$(BINARY)
|
||||||
|
|
||||||
|
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
|
||||||
|
COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
|
||||||
|
LDFLAGS := -X main.version=$(VERSION) -X main.commit=$(COMMIT)
|
||||||
|
BUILD_FLAGS := -ldflags "$(LDFLAGS)"
|
||||||
|
|
||||||
|
all: build
|
||||||
|
|
||||||
|
build: ## Build the CLI binary into bin/
|
||||||
|
$(GO) build $(GOFLAGS) $(BUILD_FLAGS) -o $(OUTPUT) $(MAIN_PKG)
|
||||||
|
|
||||||
|
install: ## Install the CLI binary to GOPATH/bin
|
||||||
|
$(GO) install $(GOFLAGS) $(BUILD_FLAGS) $(MAIN_PKG)
|
||||||
|
|
||||||
|
test: ## Run all tests
|
||||||
|
$(GO) test $(GOFLAGS) -race ./...
|
||||||
|
|
||||||
|
vet: ## Run go vet
|
||||||
|
$(GO) vet ./...
|
||||||
|
|
||||||
|
fmt: ## Format all Go source files
|
||||||
|
gofmt -s -w .
|
||||||
|
$(GO) mod tidy
|
||||||
|
|
||||||
|
cover: ## Run tests with coverage and generate HTML report
|
||||||
|
$(GO) test $(GOFLAGS) -race -coverprofile=coverage.out ./...
|
||||||
|
$(GO) tool cover -html=coverage.out -o coverage.html
|
||||||
|
|
||||||
|
tidy: ## Run go mod tidy
|
||||||
|
$(GO) mod tidy
|
||||||
|
|
||||||
|
clean: ## Remove build artifacts and coverage files
|
||||||
|
rm -rf bin/ coverage.out coverage.html
|
||||||
|
|
||||||
|
help: ## Show this help message
|
||||||
|
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-10s\033[0m %s\n", $$1, $$2}'
|
||||||
@@ -34,12 +34,33 @@ func Cmd(cfg **config.Config, cfgPath string) *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
configSetCmd := &cobra.Command{
|
configSetCmd := &cobra.Command{
|
||||||
Use: "set <key> <value>",
|
Use: "set <key> [value]",
|
||||||
Short: "Set a config value",
|
Short: "Set a config value",
|
||||||
Args: cobra.ExactArgs(2),
|
Args: cobra.RangeArgs(1, 2),
|
||||||
Annotations: map[string]string{"skipSDK": "true"},
|
Annotations: map[string]string{"skipSDK": "true"},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
key, value := args[0], args[1]
|
key := args[0]
|
||||||
|
var value string
|
||||||
|
|
||||||
|
if len(args) == 2 {
|
||||||
|
value = args[1]
|
||||||
|
} else {
|
||||||
|
// Interactive mode — prompt for the value.
|
||||||
|
if key == "apikey" {
|
||||||
|
v, err := config.ReadSecret("Enter API key: ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("read API key: %w", err)
|
||||||
|
}
|
||||||
|
value = v
|
||||||
|
} else {
|
||||||
|
v, err := config.ReadLine("Enter " + key + ": ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("read %s: %w", key, err)
|
||||||
|
}
|
||||||
|
value = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
c := *cfg
|
c := *cfg
|
||||||
switch key {
|
switch key {
|
||||||
case "apikey":
|
case "apikey":
|
||||||
@@ -52,7 +73,11 @@ func Cmd(cfg **config.Config, cfgPath string) *cobra.Command {
|
|||||||
if err := config.Save(cfgPath, c); err != nil {
|
if err := config.Save(cfgPath, c); err != nil {
|
||||||
return fmt.Errorf("save config: %w", err)
|
return fmt.Errorf("save config: %w", err)
|
||||||
}
|
}
|
||||||
|
if key == "apikey" {
|
||||||
|
fmt.Printf("Set %s = %s\n", key, "(hidden)")
|
||||||
|
} else {
|
||||||
fmt.Printf("Set %s = %s\n", key, value)
|
fmt.Printf("Set %s = %s\n", key, value)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/term"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ReadSecret prompts the user with msg and reads a line from stdin.
|
||||||
|
// When stdin is a terminal, echo is disabled so the typed value stays
|
||||||
|
// invisible. When stdin is piped (non-TTY) it falls back to a normal
|
||||||
|
// read so scripting still works.
|
||||||
|
func ReadSecret(msg string) (string, error) {
|
||||||
|
fmt.Fprint(os.Stderr, msg)
|
||||||
|
var value string
|
||||||
|
if term.IsTerminal(int(os.Stdin.Fd())) {
|
||||||
|
b, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
value = string(b)
|
||||||
|
} else {
|
||||||
|
sc := bufio.NewScanner(os.Stdin)
|
||||||
|
if !sc.Scan() {
|
||||||
|
return "", sc.Err()
|
||||||
|
}
|
||||||
|
value = sc.Text()
|
||||||
|
}
|
||||||
|
fmt.Fprintln(os.Stderr)
|
||||||
|
return value, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadLine prompts the user with msg and reads a normal (echo-enabled) line.
|
||||||
|
func ReadLine(msg string) (string, error) {
|
||||||
|
fmt.Fprint(os.Stderr, msg)
|
||||||
|
sc := bufio.NewScanner(os.Stdin)
|
||||||
|
if !sc.Scan() {
|
||||||
|
return "", sc.Err()
|
||||||
|
}
|
||||||
|
return sc.Text(), nil
|
||||||
|
}
|
||||||
@@ -10,6 +10,9 @@ The format loosely follows Keep a Changelog and can be adapted to the team's hab
|
|||||||
|
|
||||||
### Added / 新增
|
### Added / 新增
|
||||||
|
|
||||||
|
- `config set <key>` 支持交互式输入:省略 value 时交互式提示,`apikey` 使用隐藏输入避免敏感信息暴露在终端/历史中。
|
||||||
|
- 新增 `Makefile`(build/test/vet/fmt/cover/tidy/clean/help)。
|
||||||
|
|
||||||
- 迁移 rainyun-cli 到仓库内 `cmd/ry/`:新增 `main.go`、`root.go`、`commands/{server,domain,storage,billing,configcmd}`、`internal/{config,model,output}`。
|
- 迁移 rainyun-cli 到仓库内 `cmd/ry/`:新增 `main.go`、`root.go`、`commands/{server,domain,storage,billing,configcmd}`、`internal/{config,model,output}`。
|
||||||
- CLI 直接调用 `sdk.New(apiKey)`,删除 CLI 自带 provider/HTTP 层。
|
- CLI 直接调用 `sdk.New(apiKey)`,删除 CLI 自带 provider/HTTP 层。
|
||||||
- 配置简化为单一 `apikey` + `output`(table/json/yaml),落盘到 `~/.config/ry/config.toml`。
|
- 配置简化为单一 `apikey` + `output`(table/json/yaml),落盘到 `~/.config/ry/config.toml`。
|
||||||
@@ -24,6 +27,8 @@ The format loosely follows Keep a Changelog and can be adapted to the team's hab
|
|||||||
|
|
||||||
- 命令 ID 参数统一为 `int`,显示层 model ID 保持 `string`。
|
- 命令 ID 参数统一为 `int`,显示层 model ID 保持 `string`。
|
||||||
- `server reinstall` flag 从 `--os` 改为 `--os-id int`。
|
- `server reinstall` flag 从 `--os` 改为 `--os-id int`。
|
||||||
|
- `.gitignore` 新增 `bin/`、`coverage.out`、`coverage.html`。
|
||||||
|
- 新增 `golang.org/x/term` 依赖(交互式隐藏输入)。
|
||||||
|
|
||||||
## [0.2.0] - 2026-08-05
|
## [0.2.0] - 2026-08-05
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ require (
|
|||||||
github.com/bytedance/sonic v1.15.2
|
github.com/bytedance/sonic v1.15.2
|
||||||
github.com/go-resty/resty/v2 v2.17.2
|
github.com/go-resty/resty/v2 v2.17.2
|
||||||
github.com/spf13/cobra v1.10.2
|
github.com/spf13/cobra v1.10.2
|
||||||
|
golang.org/x/term v0.34.0
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
|
|||||||
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
|
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
|
||||||
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
|
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
|
||||||
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||||
|
golang.org/x/term v0.34.0 h1:O/2T7POpk0ZZ7MAzMeWFSg6S5IpWd/RXDlM9hgM3DR4=
|
||||||
|
golang.org/x/term v0.34.0/go.mod h1:5jC53AEywhIVebHgPVeg0mj8OD3VO9OzclacVrqpaAw=
|
||||||
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
|
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
|
||||||
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
|
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
|||||||
Reference in New Issue
Block a user