Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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 ./...
14 changes: 2 additions & 12 deletions cmd/create_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"net/http"
"os"
"strings"
"sync"
"testing"

"github.com/latitudesh/lsh/cli"
"github.com/spf13/viper"
)

Expand All @@ -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)
}
}

Expand Down
42 changes: 32 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"os"
"path/filepath"
"sync"

"github.com/latitudesh/lsh/cli"
"github.com/latitudesh/lsh/cmd/lsh"
Expand All @@ -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()
}
Loading