feat(cli): scaffold weknora CLI module foundation (PR-1)
Empty cli/ Go module with Factory + Options skeleton that all v0.x commands plug into. Foundation packages (10 internal/): - cmdutil: Factory(3 closure: Config/Client/Prompter) + Options + Errors with namespaced ErrorCode + typed predicates (IsAuthError / IsNotFound / IsTransient / IsAuthExpired) + Exporter + AddJSONFlags + ExitCode/PrintError - iostreams: package singleton with TTY detection; ColorEnabled honors NO_COLOR / TERM=dumb on demand - agent: mode + detect + annotations (Stripe pkg/useragent pattern; CLAUDECODE / CURSOR_AGENT / CODEX_* / AIDER_* / CONTINUE_* / OPENCODE_* / GEMINICODER env detection) - build: ldflags-injected version/commit/date - safepaths: separator-aware path-traversal protection - config: yaml.v3 schema + atomic save 0600 (XDG-style) - format: success/failure envelope (code/hint/request_id/risk/retryable/ console_url) with typed RiskLevel - prompt: Prompter interface + AgentPrompter (rejects in agent mode) - secrets: file 0600 store with weknora:<context>:<key> namespace - testutil: XDGTempDir(t) test helper Smoke command: weknora version (-/--json). Build/test/vet matrix in .github/workflows/cli.yml across linux x macos x windows x Go 1.24. Factory.Client returns CodeLocalUnimplemented until PR-3 wires the SDK adapter; PR-1 commands (version) don't need it.
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// AIAgentHelpKey is the cobra Annotation key used to attach an AI-agent-specific
|
||||
// help blurb to a command. The blurb is appended to --help output only when
|
||||
// agent mode is active. Mirrors Stripe's pkg/cmd/templates.go AIAgentHelpAnnotationKey.
|
||||
const AIAgentHelpKey = "ai_agent_help"
|
||||
|
||||
// FormatAgentGuidance returns the agent-targeted help text registered on cmd,
|
||||
// or "" if none. Render it after the standard help when ShouldUseAgentMode reports true.
|
||||
func FormatAgentGuidance(cmd *cobra.Command) string {
|
||||
if cmd == nil {
|
||||
return ""
|
||||
}
|
||||
v, ok := cmd.Annotations[AIAgentHelpKey]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(v)
|
||||
}
|
||||
|
||||
// SetAgentHelp is a small helper that initializes Annotations and stores the
|
||||
// blurb under AIAgentHelpKey. Use it in NewCmdX constructors before returning.
|
||||
func SetAgentHelp(cmd *cobra.Command, blurb string) {
|
||||
if cmd.Annotations == nil {
|
||||
cmd.Annotations = map[string]string{}
|
||||
}
|
||||
cmd.Annotations[AIAgentHelpKey] = strings.TrimSpace(blurb)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Package agent handles AI agent integration: --agent flag sugar, env detection,
|
||||
// and per-command help annotations.
|
||||
package agent
|
||||
|
||||
import "os"
|
||||
|
||||
// AIAgentName identifies the detected coding agent invoking the CLI. Empty
|
||||
// string means no agent detected (or detection is disabled).
|
||||
type AIAgentName string
|
||||
|
||||
// aiAgentEnvs maps environment variable presence to a coding agent name.
|
||||
// Modeled on Stripe's pkg/useragent/useragent.go DetectAIAgent.
|
||||
var aiAgentEnvs = []struct {
|
||||
env string
|
||||
name AIAgentName
|
||||
}{
|
||||
{"CLAUDECODE", "claude-code"},
|
||||
{"CURSOR_AGENT", "cursor"},
|
||||
{"CODEX_PROMPT_HOSTNAME", "codex"},
|
||||
{"AIDER_PROMPT", "aider"},
|
||||
{"CONTINUE_GLOBAL_DIR", "continue"},
|
||||
{"OPENCODE_RUNNING", "opencode"},
|
||||
{"GEMINICODER_PROFILE", "gemini-coder"},
|
||||
}
|
||||
|
||||
// DetectAIAgent returns the first known agent name whose env var is set to a
|
||||
// non-empty value, or "" if none are present. Detection is suppressed when
|
||||
// WEKNORA_NO_AGENT_AUTODETECT is truthy. Tests substitute via t.Setenv.
|
||||
func DetectAIAgent() AIAgentName {
|
||||
if v := os.Getenv("WEKNORA_NO_AGENT_AUTODETECT"); v != "" && v != "0" && v != "false" {
|
||||
return ""
|
||||
}
|
||||
for _, a := range aiAgentEnvs {
|
||||
if os.Getenv(a.env) != "" {
|
||||
return a.name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// clearAgentEnv unsets every env var DetectAIAgent inspects, so a developer
|
||||
// running these tests from inside Claude Code / Cursor doesn't see false
|
||||
// positives.
|
||||
func clearAgentEnv(t *testing.T) {
|
||||
t.Helper()
|
||||
for _, a := range aiAgentEnvs {
|
||||
t.Setenv(a.env, "")
|
||||
}
|
||||
t.Setenv("WEKNORA_NO_AGENT_AUTODETECT", "")
|
||||
t.Setenv("WEKNORA_AGENT", "")
|
||||
}
|
||||
|
||||
func TestDetectAIAgent(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
set map[string]string
|
||||
want AIAgentName
|
||||
}{
|
||||
{name: "empty env", set: nil, want: ""},
|
||||
{name: "unrelated env", set: map[string]string{"PATH": "/usr/bin"}, want: ""},
|
||||
{name: "claude code", set: map[string]string{"CLAUDECODE": "1"}, want: "claude-code"},
|
||||
{name: "cursor", set: map[string]string{"CURSOR_AGENT": "yes"}, want: "cursor"},
|
||||
{name: "codex", set: map[string]string{"CODEX_PROMPT_HOSTNAME": "host"}, want: "codex"},
|
||||
{name: "aider", set: map[string]string{"AIDER_PROMPT": "y"}, want: "aider"},
|
||||
{name: "continue", set: map[string]string{"CONTINUE_GLOBAL_DIR": "/tmp"}, want: "continue"},
|
||||
{name: "opencode", set: map[string]string{"OPENCODE_RUNNING": "1"}, want: "opencode"},
|
||||
{name: "gemini-coder", set: map[string]string{"GEMINICODER_PROFILE": "default"}, want: "gemini-coder"},
|
||||
{
|
||||
name: "first-match precedence",
|
||||
set: map[string]string{"CLAUDECODE": "1", "CURSOR_AGENT": "1"},
|
||||
want: "claude-code",
|
||||
},
|
||||
{name: "empty value treated as unset", set: map[string]string{"CLAUDECODE": ""}, want: ""},
|
||||
{
|
||||
name: "WEKNORA_NO_AGENT_AUTODETECT=1 suppresses detection",
|
||||
set: map[string]string{"CLAUDECODE": "1", "WEKNORA_NO_AGENT_AUTODETECT": "1"},
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "WEKNORA_NO_AGENT_AUTODETECT=0 still allows detection",
|
||||
set: map[string]string{"CLAUDECODE": "1", "WEKNORA_NO_AGENT_AUTODETECT": "0"},
|
||||
want: "claude-code",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
clearAgentEnv(t)
|
||||
for k, v := range tc.set {
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
assert.Equal(t, tc.want, DetectAIAgent())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldUseAgentMode_Triggers(t *testing.T) {
|
||||
t.Run("WEKNORA_AGENT=1 triggers", func(t *testing.T) {
|
||||
clearAgentEnv(t)
|
||||
t.Setenv("WEKNORA_AGENT", "1")
|
||||
assert.True(t, ShouldUseAgentMode(nil))
|
||||
})
|
||||
t.Run("WEKNORA_AGENT=true triggers", func(t *testing.T) {
|
||||
clearAgentEnv(t)
|
||||
t.Setenv("WEKNORA_AGENT", "true")
|
||||
assert.True(t, ShouldUseAgentMode(nil))
|
||||
})
|
||||
t.Run("WEKNORA_AGENT=0 does not trigger", func(t *testing.T) {
|
||||
clearAgentEnv(t)
|
||||
t.Setenv("WEKNORA_AGENT", "0")
|
||||
assert.False(t, ShouldUseAgentMode(nil))
|
||||
})
|
||||
t.Run("AI agent env triggers", func(t *testing.T) {
|
||||
clearAgentEnv(t)
|
||||
t.Setenv("CLAUDECODE", "1")
|
||||
assert.True(t, ShouldUseAgentMode(nil))
|
||||
})
|
||||
t.Run("no triggers", func(t *testing.T) {
|
||||
clearAgentEnv(t)
|
||||
assert.False(t, ShouldUseAgentMode(nil))
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// agentSugarFlags lists the flags ApplyAgentSugar sets to their agent-friendly
|
||||
// defaults when triggered. Each entry is no-op'd at runtime if the leaf hasn't
|
||||
// registered the flag (e.g. version doesn't have --no-progress).
|
||||
var agentSugarFlags = []struct{ name, value string }{
|
||||
{"json", "true"},
|
||||
{"no-interactive", "true"},
|
||||
{"no-progress", "true"},
|
||||
}
|
||||
|
||||
// ApplyAgentSugar evaluates --agent / WEKNORA_AGENT / env-based AI agent
|
||||
// detection, then sets json / no-interactive / no-progress to their
|
||||
// agent-friendly defaults unless the user already set them.
|
||||
func ApplyAgentSugar(cmd *cobra.Command) {
|
||||
if !ShouldUseAgentMode(cmd) {
|
||||
return
|
||||
}
|
||||
for _, kv := range agentSugarFlags {
|
||||
if f := cmd.Flags().Lookup(kv.name); f != nil && !cmd.Flags().Changed(kv.name) {
|
||||
_ = cmd.Flags().Set(kv.name, kv.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ShouldUseAgentMode returns true if any of the three triggers are active:
|
||||
// (1) --agent flag, (2) WEKNORA_AGENT=1 env, (3) a known agent env var is set.
|
||||
func ShouldUseAgentMode(cmd *cobra.Command) bool {
|
||||
if cmd != nil {
|
||||
if v, err := cmd.Flags().GetBool("agent"); err == nil && v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if truthy(os.Getenv("WEKNORA_AGENT")) {
|
||||
return true
|
||||
}
|
||||
return DetectAIAgent() != ""
|
||||
}
|
||||
|
||||
// truthy treats any non-empty value other than "0" / "false" / "FALSE" as
|
||||
// truthy. This is wider than strconv.ParseBool by design — users set
|
||||
// WEKNORA_AGENT=1, =true, or =yes interchangeably across CI shells.
|
||||
func truthy(v string) bool {
|
||||
switch v {
|
||||
case "", "0", "false", "FALSE":
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// newTestCmd returns a cobra command with the flags ApplyAgentSugar
|
||||
// expects to be present (`--agent`, `--json`, `--no-interactive`,
|
||||
// `--no-progress`).
|
||||
func newTestCmd() *cobra.Command {
|
||||
c := &cobra.Command{Use: "x"}
|
||||
c.Flags().Bool("agent", false, "")
|
||||
c.Flags().Bool("json", false, "")
|
||||
c.Flags().Bool("no-interactive", false, "")
|
||||
c.Flags().Bool("no-progress", false, "")
|
||||
return c
|
||||
}
|
||||
|
||||
func TestApplyAgentSugar_AgentFlagSet(t *testing.T) {
|
||||
c := newTestCmd()
|
||||
require.NoError(t, c.Flags().Set("agent", "true"))
|
||||
ApplyAgentSugar(c)
|
||||
j, _ := c.Flags().GetBool("json")
|
||||
assert.True(t, j)
|
||||
ni, _ := c.Flags().GetBool("no-interactive")
|
||||
assert.True(t, ni)
|
||||
np, _ := c.Flags().GetBool("no-progress")
|
||||
assert.True(t, np)
|
||||
}
|
||||
|
||||
func TestApplyAgentSugar_NoTrigger(t *testing.T) {
|
||||
clearAgentEnv(t)
|
||||
c := newTestCmd()
|
||||
ApplyAgentSugar(c)
|
||||
j, _ := c.Flags().GetBool("json")
|
||||
assert.False(t, j, "without --agent or env, --json must remain unset")
|
||||
}
|
||||
|
||||
func TestApplyAgentSugar_ExplicitJSONFalseWins(t *testing.T) {
|
||||
clearAgentEnv(t)
|
||||
c := newTestCmd()
|
||||
require.NoError(t, c.Flags().Set("agent", "true"))
|
||||
require.NoError(t, c.Flags().Set("json", "false"))
|
||||
ApplyAgentSugar(c)
|
||||
j, _ := c.Flags().GetBool("json")
|
||||
assert.False(t, j, "explicit --json=false must override agent sugar")
|
||||
}
|
||||
|
||||
func TestFormatAgentGuidance(t *testing.T) {
|
||||
c := &cobra.Command{Use: "x"}
|
||||
assert.Equal(t, "", FormatAgentGuidance(c))
|
||||
SetAgentHelp(c, " use --idempotency-key for safe retries ")
|
||||
assert.Equal(t, "use --idempotency-key for safe retries", FormatAgentGuidance(c))
|
||||
}
|
||||
|
||||
func TestSetAgentHelp_InitsAnnotations(t *testing.T) {
|
||||
c := &cobra.Command{Use: "x"}
|
||||
assert.Nil(t, c.Annotations)
|
||||
SetAgentHelp(c, "blurb")
|
||||
require.NotNil(t, c.Annotations)
|
||||
assert.Equal(t, "blurb", c.Annotations[AIAgentHelpKey])
|
||||
}
|
||||
|
||||
func TestTruthy(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
want bool
|
||||
}{
|
||||
{"", false},
|
||||
{"0", false},
|
||||
{"false", false},
|
||||
{"FALSE", false},
|
||||
{"1", true},
|
||||
{"true", true},
|
||||
{"yes", true},
|
||||
{"anything-else", true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.in, func(t *testing.T) {
|
||||
assert.Equal(t, tc.want, truthy(tc.in))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user