From ade0a48d41b895fdcf0dc0298580e8a7006e45c8 Mon Sep 17 00:00:00 2001 From: Joshua Gilman Date: Sun, 23 Aug 2026 09:07:57 -0700 Subject: [PATCH] refactor: cut over to codemode library module --- .golangci.yml | 2 +- cmd/template-go/main.go | 47 ------------ doc.go | 2 + go.mod | 24 +----- go.sum | 57 +------------- internal/cli/root.go | 118 ----------------------------- internal/cli/root_test.go | 71 ----------------- internal/config/config.go | 27 ------- internal/templateinfo/info.go | 8 -- internal/templateinfo/info_test.go | 12 --- moon.yml | 8 +- 11 files changed, 12 insertions(+), 364 deletions(-) delete mode 100644 cmd/template-go/main.go create mode 100644 doc.go delete mode 100644 internal/cli/root.go delete mode 100644 internal/cli/root_test.go delete mode 100644 internal/config/config.go delete mode 100644 internal/templateinfo/info.go delete mode 100644 internal/templateinfo/info_test.go diff --git a/.golangci.yml b/.golangci.yml index 9362e87..0a0fbaa 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -36,7 +36,7 @@ formatters: # with the given prefixes are grouped after 3rd-party packages. # Default: [] local-prefixes: - - github.com/meigma/template-go + - github.com/meigma/codemode golines: # Target maximum line length. diff --git a/cmd/template-go/main.go b/cmd/template-go/main.go deleted file mode 100644 index 6062488..0000000 --- a/cmd/template-go/main.go +++ /dev/null @@ -1,47 +0,0 @@ -package main - -import ( - "context" - "fmt" - "os" - "os/signal" - "syscall" - - "github.com/meigma/template-go/internal/cli" -) - -//nolint:gochecknoglobals // GoReleaser injects these values with ldflags during releases. -var ( - version = "dev" - commit = "none" - date = "unknown" -) - -func main() { - os.Exit(run()) -} - -func run() int { - ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) - defer stop() - - root := cli.NewRootCommand(cli.Options{ - In: os.Stdin, - Build: cli.BuildInfo{ - Version: version, - Commit: commit, - Date: date, - }, - Out: os.Stdout, - Err: os.Stderr, - }) - if err := root.ExecuteContext(ctx); err != nil { - if _, writeErr := fmt.Fprintln(os.Stderr, err); writeErr != nil { - return 1 - } - - return 1 - } - - return 0 -} diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..4aab7b6 --- /dev/null +++ b/doc.go @@ -0,0 +1,2 @@ +// Package codemode is the root package of the CodeMode library. +package codemode diff --git a/go.mod b/go.mod index 0cff1e0..bdcf6e8 100644 --- a/go.mod +++ b/go.mod @@ -1,26 +1,8 @@ -module github.com/meigma/template-go +module github.com/meigma/codemode go 1.26.6 require ( - github.com/spf13/cobra v1.10.2 - github.com/spf13/viper v1.21.0 -) - -require ( - github.com/fsnotify/fsnotify v1.10.1 // indirect - github.com/go-viper/mapstructure/v2 v2.5.0 // indirect - github.com/google/go-cmp v0.7.0 // indirect - github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/pelletier/go-toml/v2 v2.3.1 // indirect - github.com/rogpeppe/go-internal v1.15.0 // indirect - github.com/sagikazarmark/locafero v0.12.0 // indirect - github.com/spf13/afero v1.15.0 // indirect - github.com/spf13/cast v1.10.0 // indirect - github.com/spf13/pflag v1.0.10 // indirect - github.com/subosito/gotenv v1.6.0 // indirect - go.yaml.in/yaml/v3 v3.0.4 // indirect - golang.org/x/sys v0.46.0 // indirect - golang.org/x/text v0.38.0 // indirect - gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + github.com/modelcontextprotocol/go-sdk v1.7.0 + go.starlark.net v0.0.0-20260708150628-5395d018f003 ) diff --git a/go.sum b/go.sum index 208233f..f43e535 100644 --- a/go.sum +++ b/go.sum @@ -1,55 +1,2 @@ -github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= -github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= -github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx59Ho= -github.com/fsnotify/fsnotify v1.10.1/go.mod h1:TLheqan6HD6GBK6PrDWyDPBaEV8LspOxvPSjC+bVfgo= -github.com/go-viper/mapstructure/v2 v2.5.0 h1:vM5IJoUAy3d7zRSVtIwQgBj7BiWtMPfmPEgAXnvj1Ro= -github.com/go-viper/mapstructure/v2 v2.5.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= -github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= -github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= -github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= -github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= -github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= -github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/pelletier/go-toml/v2 v2.3.1 h1:MYEvvGnQjeNkRF1qUuGolNtNExTDwct51yp7olPtrEc= -github.com/pelletier/go-toml/v2 v2.3.1/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.15.0 h1:D0RCU5rMAp+SpgkiNdrjfJ+LX4J1M32V2NeCY7EJ6hc= -github.com/rogpeppe/go-internal v1.15.0/go.mod h1:DrUVZyrJU+txYW5/1kwtXQSMFio52ZOxX7yM1VHvnxs= -github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/sagikazarmark/locafero v0.12.0 h1:/NQhBAkUb4+fH1jivKHWusDYFjMOOKU88eegjfxfHb4= -github.com/sagikazarmark/locafero v0.12.0/go.mod h1:sZh36u/YSZ918v0Io+U9ogLYQJ9tLLBmM4eneO6WwsI= -github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I= -github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg= -github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY= -github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo= -github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= -github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4= -github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= -github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU= -github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY= -github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= -github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= -github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= -github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= -go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= -go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= -golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw= -golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= -golang.org/x/text v0.38.0 h1:sXmwo9DwP3OK9EZ7PqAdaooSGozfl/3a6/xJcbzPRhE= -golang.org/x/text v0.38.0/go.mod h1:YXZt3QhHUKYT53r2lLKFIVi6Ao1jdzrTR/KQ09qyxF4= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +github.com/modelcontextprotocol/go-sdk v1.7.0/go.mod h1:dL7u98E/zjJTGzEq+j30jQ8K2k1mb6LeAH4inEcSGts= +go.starlark.net v0.0.0-20260708150628-5395d018f003/go.mod h1:Iue6g6iirlfLoVi/DYCi5/x0h/bAOuWF3dULTKpt2Vo= diff --git a/internal/cli/root.go b/internal/cli/root.go deleted file mode 100644 index 39c0314..0000000 --- a/internal/cli/root.go +++ /dev/null @@ -1,118 +0,0 @@ -package cli - -import ( - "fmt" - "io" - "strings" - - "github.com/spf13/cobra" - "github.com/spf13/viper" - - "github.com/meigma/template-go/internal/config" - "github.com/meigma/template-go/internal/templateinfo" -) - -// BuildInfo describes linker-injected build metadata printed by --version. -type BuildInfo struct { - // Version is the release version. - Version string - // Commit is the source commit used to build the binary. - Commit string - // Date is the build timestamp. - Date string -} - -// Options customizes root command construction. -type Options struct { - // In receives interactive command input. - In io.Reader - // Out receives machine-readable command output. - Out io.Writer - // Err receives diagnostics and human-readable status. - Err io.Writer - // Build controls the root command version output. - Build BuildInfo - // Viper is the configuration instance used by the command tree. - Viper *viper.Viper -} - -// NewRootCommand creates the template-go Cobra command tree. -func NewRootCommand(options Options) *cobra.Command { - if options.In == nil { - options.In = strings.NewReader("") - } - if options.Out == nil { - options.Out = io.Discard - } - if options.Err == nil { - options.Err = io.Discard - } - if options.Viper == nil { - options.Viper = viper.New() - } - options.Build = options.Build.withDefaults() - - root := &cobra.Command{ - Use: "template-go", - Short: "Meigma Go repository template application", - Version: options.Build.Version, - SilenceUsage: true, - SilenceErrors: true, - PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { - return initializeConfig(cmd, options.Viper) - }, - RunE: func(_ *cobra.Command, _ []string) error { - cfg := config.Load(options.Viper) - return printLine(options.Out, cfg.Message) - }, - } - root.SetVersionTemplate( - fmt.Sprintf( - "template-go %s (%s) built %s\n", - options.Build.Version, - options.Build.Commit, - options.Build.Date, - ), - ) - root.SetIn(options.In) - root.SetOut(options.Out) - root.SetErr(options.Err) - root.PersistentFlags().String("message", templateinfo.Summary(), "message to print") - return root -} - -func (b BuildInfo) withDefaults() BuildInfo { - if strings.TrimSpace(b.Version) == "" { - b.Version = "dev" - } - if strings.TrimSpace(b.Commit) == "" { - b.Commit = "none" - } - if strings.TrimSpace(b.Date) == "" { - b.Date = "unknown" - } - return b -} - -func initializeConfig(cmd *cobra.Command, vp *viper.Viper) error { - vp.SetEnvPrefix("TEMPLATE_GO") - vp.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_")) - vp.AutomaticEnv() - - if err := vp.BindPFlags(cmd.Root().PersistentFlags()); err != nil { - return fmt.Errorf("bind persistent flags: %w", err) - } - if err := vp.BindPFlags(cmd.Flags()); err != nil { - return fmt.Errorf("bind flags: %w", err) - } - - return nil -} - -func printLine(w io.Writer, line string) error { - if _, err := fmt.Fprintln(w, line); err != nil { - return fmt.Errorf("write output: %w", err) - } - - return nil -} diff --git a/internal/cli/root_test.go b/internal/cli/root_test.go deleted file mode 100644 index 252bcfd..0000000 --- a/internal/cli/root_test.go +++ /dev/null @@ -1,71 +0,0 @@ -package cli - -import ( - "bytes" - "context" - "testing" - - "github.com/spf13/viper" -) - -func TestVersionFlagPrintsBuildMetadata(t *testing.T) { - t.Parallel() - - var stdout bytes.Buffer - var stderr bytes.Buffer - root := NewRootCommand(Options{ - Out: &stdout, - Err: &stderr, - Build: BuildInfo{ - Version: "0.1.0", - Commit: "abc1234", - Date: "2026-05-08T10:00:00Z", - }, - }) - root.SetArgs([]string{"--version"}) - - if err := root.ExecuteContext(context.Background()); err != nil { - t.Fatalf("ExecuteContext returned an error: %v", err) - } - if got, want := stdout.String(), "template-go 0.1.0 (abc1234) built 2026-05-08T10:00:00Z\n"; got != want { - t.Fatalf("stdout = %q, want %q", got, want) - } - if got := stderr.String(); got != "" { - t.Fatalf("stderr = %q, want empty", got) - } -} - -func TestRootCommandPrintsConfiguredMessage(t *testing.T) { - t.Parallel() - - var stdout bytes.Buffer - root := NewRootCommand(Options{ - Out: &stdout, - Viper: viper.New(), - }) - root.SetArgs([]string{"--message", "hello from cobra"}) - - if err := root.ExecuteContext(context.Background()); err != nil { - t.Fatalf("ExecuteContext returned an error: %v", err) - } - if got, want := stdout.String(), "hello from cobra\n"; got != want { - t.Fatalf("stdout = %q, want %q", got, want) - } -} - -func TestRootCommandReadsMessageFromEnvironment(t *testing.T) { - t.Setenv("TEMPLATE_GO_MESSAGE", "hello from viper") - - var stdout bytes.Buffer - root := NewRootCommand(Options{ - Out: &stdout, - Viper: viper.New(), - }) - - if err := root.ExecuteContext(context.Background()); err != nil { - t.Fatalf("ExecuteContext returned an error: %v", err) - } - if got, want := stdout.String(), "hello from viper\n"; got != want { - t.Fatalf("stdout = %q, want %q", got, want) - } -} diff --git a/internal/config/config.go b/internal/config/config.go deleted file mode 100644 index 1f69bdb..0000000 --- a/internal/config/config.go +++ /dev/null @@ -1,27 +0,0 @@ -package config - -import ( - "strings" - - "github.com/spf13/viper" - - "github.com/meigma/template-go/internal/templateinfo" -) - -// Config contains runtime settings used by the starter CLI. -type Config struct { - // Message is printed when the root command runs without a subcommand. - Message string -} - -// Load reads runtime settings from Viper. -func Load(vp *viper.Viper) Config { - message := strings.TrimSpace(vp.GetString("message")) - if message == "" { - message = templateinfo.Summary() - } - - return Config{ - Message: message, - } -} diff --git a/internal/templateinfo/info.go b/internal/templateinfo/info.go deleted file mode 100644 index cc66add..0000000 --- a/internal/templateinfo/info.go +++ /dev/null @@ -1,8 +0,0 @@ -package templateinfo - -const name = "template-go" - -// Summary returns the default message printed by the starter CLI. -func Summary() string { - return name + " template application" -} diff --git a/internal/templateinfo/info_test.go b/internal/templateinfo/info_test.go deleted file mode 100644 index 7ab8b3f..0000000 --- a/internal/templateinfo/info_test.go +++ /dev/null @@ -1,12 +0,0 @@ -package templateinfo - -import "testing" - -func TestSummary(t *testing.T) { - t.Parallel() - - got := Summary() - if got == "" { - t.Fatal("Summary returned an empty string") - } -} diff --git a/moon.yml b/moon.yml index 478e8c7..351b8c9 100644 --- a/moon.yml +++ b/moon.yml @@ -16,7 +16,9 @@ toolchains: fileGroups: goSources: - - 'cmd/**/*.go' + - '*.go' + - 'authz/**/*.go' + - 'mcpserver/**/*.go' - 'internal/**/*.go' - 'go.mod' - 'go.sum' @@ -57,11 +59,9 @@ tasks: cache: false build: - command: 'go build -o bin/template-go ./cmd/template-go' + command: 'go build ./...' inputs: - '@group(goSources)' - outputs: - - 'bin/template-go' test: command: 'go test ./...'