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:
2026-08-13 13:10:53 +08:00
parent 627ea7110a
commit f04bad5f18
7 changed files with 129 additions and 5 deletions
+29 -4
View File
@@ -34,12 +34,33 @@ func Cmd(cfg **config.Config, cfgPath string) *cobra.Command {
}
configSetCmd := &cobra.Command{
Use: "set <key> <value>",
Use: "set <key> [value]",
Short: "Set a config value",
Args: cobra.ExactArgs(2),
Args: cobra.RangeArgs(1, 2),
Annotations: map[string]string{"skipSDK": "true"},
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
switch key {
case "apikey":
@@ -52,7 +73,11 @@ func Cmd(cfg **config.Config, cfgPath string) *cobra.Command {
if err := config.Save(cfgPath, c); err != nil {
return fmt.Errorf("save config: %w", err)
}
fmt.Printf("Set %s = %s\n", key, value)
if key == "apikey" {
fmt.Printf("Set %s = %s\n", key, "(hidden)")
} else {
fmt.Printf("Set %s = %s\n", key, value)
}
return nil
},
}
+43
View File
@@ -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
}