Skip to content

Commit

Permalink
Move dependency checking code into a standalone package
Browse files Browse the repository at this point in the history
  • Loading branch information
jerargus committed Nov 15, 2023
1 parent 756530e commit 55e0b4e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 53 deletions.
7 changes: 7 additions & 0 deletions cmd/world/cardinal/cardinal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cardinal
import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"pkg.world.dev/world-cli/common/dependency"
"pkg.world.dev/world-cli/tea/style"
)

Expand All @@ -18,6 +19,12 @@ var BaseCmd = &cobra.Command{
Short: "Manage your Cardinal game shard project",
Long: style.CLIHeader("World CLI — CARDINAL", "Manage your Cardinal game shard project"),
GroupID: "Core",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return dependency.Check(
dependency.Go,
dependency.Git,
)
},
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Fatal().Err(err).Msg("Failed to execute cardinal command")
Expand Down
8 changes: 5 additions & 3 deletions cmd/world/cardinal/create.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package cardinal

import (
"strings"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
"pkg.world.dev/world-cli/common/dependency"
"pkg.world.dev/world-cli/common/tea_cmd"
"pkg.world.dev/world-cli/tea/component/steps"
"pkg.world.dev/world-cli/tea/style"
"strings"
)

const TemplateGitUrl = "https://github.com/Argus-Labs/starter-game-template.git"

var CreateDeps = []tea_cmd.Dependency{
tea_cmd.GitDependency,
var CreateDeps = []dependency.Dependency{
dependency.Git,
}

/////////////////
Expand Down
13 changes: 7 additions & 6 deletions cmd/world/root/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package root
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
"pkg.world.dev/world-cli/common/dependency"
"pkg.world.dev/world-cli/common/tea_cmd"
"pkg.world.dev/world-cli/tea/style"
)

var DoctorDeps = []tea_cmd.Dependency{
tea_cmd.GitDependency,
tea_cmd.GoDependency,
tea_cmd.DockerDependency,
tea_cmd.DockerComposeDependency,
tea_cmd.DockerDaemonDependency,
var DoctorDeps = []dependency.Dependency{
dependency.Git,
dependency.Go,
dependency.Docker,
dependency.DockerCompose,
dependency.DockerDaemon,
}

/////////////////
Expand Down
5 changes: 4 additions & 1 deletion cmd/world/root/roo_test.go → cmd/world/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package root
import (
"bytes"
"errors"
"fmt"
"strings"
"testing"

Expand All @@ -27,7 +28,9 @@ func outputFromCmd(t *testing.T, cmd string) (lines []string, err error) {
rootCmd.SetArgs(nil)
}()

assert.NilError(t, rootCmd.Execute())
if err = rootCmd.Execute(); err != nil {
return nil, fmt.Errorf("root command failed with: %w", err)
}
lines = strings.Split(stdOut.String(), "\n")
errorStr := stdErr.String()
if len(errorStr) > 0 {
Expand Down
66 changes: 66 additions & 0 deletions common/dependency/dependency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dependency

import (
"errors"
"fmt"
"os/exec"
)

type Dependency struct {
Name string
Cmd *exec.Cmd
Help string
}

var (
Git = Dependency{
Name: "Git",
Cmd: exec.Command("git", "--version"),
Help: `Git is required to clone the starter-game-template.
Learn how to install Git: https://github.com/git-guides/install-git`,
}
Go = Dependency{
Name: "Go",
Cmd: exec.Command("go", "version"),
Help: `Go is required to build and run World Engine game shards.
Learn how to install Go: https://go.dev/doc/install`,
}
Docker = Dependency{
Name: "Docker",
Cmd: exec.Command("docker", "--version"),
Help: `Docker is required to build and run World Engine game shards.
Learn how to install Docker: https://docs.docker.com/engine/install/`,
}
DockerCompose = Dependency{
Name: "Docker Compose",
Cmd: exec.Command("docker", "compose", "version"),
Help: `Docker Compose is required to build and run World Engine game shards.
Learn how to install Docker: https://docs.docker.com/engine/install/`,
}
DockerDaemon = Dependency{
Name: "Docker daemon is running",
Cmd: exec.Command("docker", "info"),
Help: `Docker daemon needs to be running.
If you use Docker Desktop, make sure that you have ran it`,
}
AlwaysFail = Dependency{
Name: "Always fails",
Cmd: exec.Command("false"),
Help: `This dependency check will always fail. It can be used for testing.`,
}
)

func (d Dependency) Check() error {
if err := d.Cmd.Run(); err != nil {
return fmt.Errorf("dependency check %q failed with: %w", d.Name, err)
}
return nil
}

func Check(deps ...Dependency) error {
var errs []error
for _, dep := range deps {
errs = append(errs, dep.Check())
}
return errors.Join(errs...)
}
46 changes: 3 additions & 43 deletions common/tea_cmd/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,14 @@ package tea_cmd

import (
"errors"
"os/exec"

tea "github.com/charmbracelet/bubbletea"
"pkg.world.dev/world-cli/common/dependency"
"pkg.world.dev/world-cli/tea/style"
)

var (
GitDependency = Dependency{
Name: "Git",
Cmd: exec.Command("git", "--version"),
Help: `Git is required to clone the starter-game-template.
Learn how to install Git: https://github.com/git-guides/install-git`,
}
GoDependency = Dependency{
Name: "Go",
Cmd: exec.Command("go"+
"", "version"),
Help: `Go is required to build and run World Engine game shards.
Learn how to install Go: https://go.dev/doc/install`,
}
DockerDependency = Dependency{
Name: "Docker",
Cmd: exec.Command("docker", "--version"),
Help: `Docker is required to build and run World Engine game shards.
Learn how to install Docker: https://docs.docker.com/engine/install/`,
}
DockerComposeDependency = Dependency{
Name: "Docker Compose",
Cmd: exec.Command("docker", "compose", "version"),
Help: `Docker Compose is required to build and run World Engine game shards.
Learn how to install Docker: https://docs.docker.com/engine/install/`,
}
DockerDaemonDependency = Dependency{
Name: "Docker daemon is running",
Cmd: exec.Command("docker", "info"),
Help: `Docker daemon needs to be running.
If you use Docker Desktop, make sure that you have ran it`,
}
)

type Dependency struct {
Name string
Cmd *exec.Cmd
Help string
}

type DependencyStatus struct {
Dependency
dependency.Dependency
IsInstalled bool
}

Expand All @@ -60,7 +20,7 @@ type CheckDependenciesMsg struct {

// CheckDependenciesCmd Iterate through required dependencies and check if they are installed.
// Dispatch CheckDependenciesMsg if any dependency is missing.
func CheckDependenciesCmd(deps []Dependency) tea.Cmd {
func CheckDependenciesCmd(deps []dependency.Dependency) tea.Cmd {
return func() tea.Msg {
var res []DependencyStatus
var resErr error
Expand Down

0 comments on commit 55e0b4e

Please sign in to comment.