From 28e3c9e8caf29af3ff703b9167ed0173991fa2fe Mon Sep 17 00:00:00 2001 From: Lanusse Morais Date: Wed, 25 Nov 2026 16:50:59 -0300 Subject: [PATCH 1/2] ci: add build, vet and test workflow --- .github/workflows/test.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..aaff31ef --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,21 @@ +name: Test + +on: + pull_request: + push: + branches: [main] + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + - run: go build ./... + - run: go vet ./... + - run: go test ./... From 240f74610ff098dd5da2a0ae9a2842642a4ef1b4 Mon Sep 17 00:00:00 2001 From: Lanusse Morais Date: Wed, 25 Nov 2026 16:50:59 -0300 Subject: [PATCH 2/2] refactor(cmd): build the command tree once in Root() --- cmd/create_output_test.go | 14 ++----------- cmd/root.go | 42 +++++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/cmd/create_output_test.go b/cmd/create_output_test.go index a95fe491..4dca9098 100644 --- a/cmd/create_output_test.go +++ b/cmd/create_output_test.go @@ -6,10 +6,8 @@ import ( "net/http" "os" "strings" - "sync" "testing" - "github.com/latitudesh/lsh/cli" "github.com/spf13/viper" ) @@ -27,18 +25,10 @@ func (c cannedAPI) RoundTrip(req *http.Request) (*http.Response, error) { }, nil } -var ( - testRootOnce sync.Once - testRootErr error -) - func testRoot(t *testing.T) { t.Helper() - testRootOnce.Do(func() { - _, testRootErr = cli.MakeRootCmd(rootCmd) - }) - if testRootErr != nil { - t.Fatalf("MakeRootCmd: %v", testRootErr) + if _, err := Root(); err != nil { + t.Fatalf("Root: %v", err) } } diff --git a/cmd/root.go b/cmd/root.go index 671f2e9a..3eadbe1f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ import ( "log" "os" "path/filepath" + "sync" "github.com/latitudesh/lsh/cli" "github.com/latitudesh/lsh/cmd/lsh" @@ -21,20 +22,41 @@ var ( } ) +var ( + rootOnce sync.Once + rootErr error +) + +// Root builds the complete command tree without running anything. Execute +// uses it, and so does anything that needs the tree without executing a +// command, such as tests and tooling. The tree hangs off the package-level +// rootCmd and registers flags on it, so it is built once: a second +// MakeRootCmd would panic on the redefined flags. +func Root() (*cobra.Command, error) { + rootOnce.Do(func() { + if _, rootErr = cli.MakeRootCmd(rootCmd); rootErr != nil { + return + } + + rootCmd.PersistentFlags().BoolVar(&lsh.DryRun, "dry-run", false, "log the request that would be sent and exit without calling the API") + rootCmd.PersistentFlags().BoolVar(&lsh.Debug, "debug", false, "print debug logs to stderr") + + // Attach the SDK-based project-scoped groups (`projects ssh-keys`, + // `projects user-data`) to the generated `projects` command, which + // only exists after MakeRootCmd has run. + wireProjectScopedCommands(rootCmd) + }) + if rootErr != nil { + return nil, rootErr + } + return rootCmd, nil +} + // Execute executes the root command. func Execute() (*cobra.Command, error) { - cmd, err := cli.MakeRootCmd(rootCmd) + cmd, err := Root() if err != nil { log.Fatal("Cmd construction error: ", err) } - - rootCmd.PersistentFlags().BoolVar(&lsh.DryRun, "dry-run", false, "log the request that would be sent and exit without calling the API") - rootCmd.PersistentFlags().BoolVar(&lsh.Debug, "debug", false, "print debug logs to stderr") - - // Attach the SDK-based project-scoped groups (`projects ssh-keys`, - // `projects user-data`) to the generated `projects` command, which only - // exists after MakeRootCmd has run. - wireProjectScopedCommands(cmd) - return cmd, cmd.Execute() }