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,68 @@
|
||||
// Package iostreams wraps stdout/stderr/stdin with TTY detection and color awareness.
|
||||
//
|
||||
// Tests swap the package-level singleton via SetForTest; production code reads
|
||||
// the global IO directly. (Tests must not call SetForTest from t.Parallel(),
|
||||
// since the swap is process-global.)
|
||||
package iostreams
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/mattn/go-isatty"
|
||||
)
|
||||
|
||||
// IOStreams holds standard I/O handles plus environment-derived flags.
|
||||
type IOStreams struct {
|
||||
In io.Reader
|
||||
Out io.Writer
|
||||
Err io.Writer
|
||||
|
||||
stdoutTTY bool
|
||||
stderrTTY bool
|
||||
}
|
||||
|
||||
// IO is the package-level singleton.
|
||||
var IO *IOStreams = newProduction()
|
||||
|
||||
func newProduction() *IOStreams {
|
||||
return &IOStreams{
|
||||
In: os.Stdin,
|
||||
Out: os.Stdout,
|
||||
Err: os.Stderr,
|
||||
stdoutTTY: isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()),
|
||||
stderrTTY: isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd()),
|
||||
}
|
||||
}
|
||||
|
||||
// IsStdoutTTY reports whether stdout is a terminal.
|
||||
func (s *IOStreams) IsStdoutTTY() bool { return s.stdoutTTY }
|
||||
|
||||
// IsStderrTTY reports whether stderr is a terminal.
|
||||
func (s *IOStreams) IsStderrTTY() bool { return s.stderrTTY }
|
||||
|
||||
// ColorEnabled honors NO_COLOR (https://no-color.org) and TERM=dumb.
|
||||
// Computed on demand so env changes after init are picked up by callers.
|
||||
func (s *IOStreams) ColorEnabled() bool {
|
||||
if !s.stdoutTTY {
|
||||
return false
|
||||
}
|
||||
if os.Getenv("NO_COLOR") != "" {
|
||||
return false
|
||||
}
|
||||
return os.Getenv("TERM") != "dumb"
|
||||
}
|
||||
|
||||
// SetForTest replaces the package singleton with in-memory buffers for the
|
||||
// duration of t. Call it once per test. Not safe under t.Parallel().
|
||||
func SetForTest(t *testing.T) (out, errBuf *bytes.Buffer) {
|
||||
t.Helper()
|
||||
saved := IO
|
||||
out = &bytes.Buffer{}
|
||||
errBuf = &bytes.Buffer{}
|
||||
IO = &IOStreams{In: bytes.NewReader(nil), Out: out, Err: errBuf}
|
||||
t.Cleanup(func() { IO = saved })
|
||||
return out, errBuf
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package iostreams
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSetForTest(t *testing.T) {
|
||||
out, errBuf := SetForTest(t)
|
||||
IO.Out.Write([]byte("stdout"))
|
||||
IO.Err.Write([]byte("stderr"))
|
||||
assert.Equal(t, "stdout", out.String())
|
||||
assert.Equal(t, "stderr", errBuf.String())
|
||||
}
|
||||
|
||||
func TestSetForTest_RestoresSingleton(t *testing.T) {
|
||||
original := IO
|
||||
t.Run("nested", func(t *testing.T) {
|
||||
SetForTest(t)
|
||||
assert.NotSame(t, original, IO)
|
||||
})
|
||||
// After the subtest's t.Cleanup runs, IO must be restored.
|
||||
assert.Same(t, original, IO)
|
||||
}
|
||||
|
||||
func TestProductionGetters(t *testing.T) {
|
||||
// We can't reliably assert the *value* of these (depends on whether tests
|
||||
// run in a TTY) but we can ensure the methods don't panic and return
|
||||
// consistent values across calls.
|
||||
p := newProduction()
|
||||
a, b := p.IsStdoutTTY(), p.IsStderrTTY()
|
||||
assert.Equal(t, a, p.IsStdoutTTY())
|
||||
assert.Equal(t, b, p.IsStderrTTY())
|
||||
_ = p.ColorEnabled() // smoke
|
||||
}
|
||||
Reference in New Issue
Block a user