From a8f2b832b4d6a679a737ccc3c97e9c5a45d1c054 Mon Sep 17 00:00:00 2001 From: Mason Payne Date: Sat, 22 Feb 2025 23:33:02 -0700 Subject: [PATCH] add system setup and verification --- cmd/cli/cli.go | 1 + cmd/cli/commands.go | 16 ++++++++++ cmd/cli/setup.go | 73 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 cmd/cli/setup.go diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index 2af3cd3..23310c0 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -12,6 +12,7 @@ func main() { generateCmd(), webappCmd(), tailwindCmd(), + setupCmd(), } app := &cli.App{ diff --git a/cmd/cli/commands.go b/cmd/cli/commands.go index 96c48b1..796057f 100644 --- a/cmd/cli/commands.go +++ b/cmd/cli/commands.go @@ -283,3 +283,19 @@ func tailwindCmd() *cli.Command { }, } } + +func setupCmd() *cli.Command { + return &cli.Command{ + Name: "setup", + Aliases: []string{"s"}, + Usage: "Set up masonry, makes sure all dependencies are installed so Masonry can do its job.", + Action: func(c *cli.Context) error { + fmt.Printf("Running on %s/%s\n", runtime.GOOS, runtime.GOARCH) + + if err := ensureDependencies(); err != nil { + return fmt.Errorf("error ensuring dependencies | %w", err) + } + return nil + }, + } +} diff --git a/cmd/cli/setup.go b/cmd/cli/setup.go new file mode 100644 index 0000000..1e619f4 --- /dev/null +++ b/cmd/cli/setup.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + "strings" +) + +// dependency contains the command name and a URL for installation instructions. +type dependency struct { + name string + installURL string +} + +// checkCommandExists returns true if the given command exists in the PATH. +// If it does not exist, then it prints an instruction message. +func checkCommandExists(dep dependency) bool { + if _, err := exec.LookPath(dep.name); err != nil { + fmt.Printf("Dependency missing: %s\n", dep.name) + fmt.Printf(" Please install from: %s\n\n", dep.installURL) + return false + } + return true +} + +// ensureDependencies checks that all required tools are installed and then +// runs the 'go install' command to install the specified packages. +func ensureDependencies() error { + // List your dependencies along with links to instructions. + dependencies := []dependency{ + {"protoc", "https://github.com/protocolbuffers/protobuf/releases"}, + {"go", "https://go.dev/doc/install"}, + {"node", "https://nodejs.org/en/download"}, + {"npm", "https://nodejs.org/en/download"}, + } + + missing := false + for _, dep := range dependencies { + if !checkCommandExists(dep) { + missing = true + } + } + if missing { + return fmt.Errorf("one or more dependencies are missing; please install them before proceeding") + } + + // Run the 'go install' command. + // We list all packages as arguments. This command is common on all platforms. + toInstall := []string{ + "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest", + "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest", + "google.golang.org/protobuf/cmd/protoc-gen-go@latest", + "google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest", + } + + fmt.Println("All dependencies found. Running:") + fmt.Printf(" go install \n\t%s\n", strings.Join(toInstall, "\n\t")) + + for _, pkg := range toInstall { + cmd := exec.Command("go", "install", pkg) + // Inherit stdout/stderr so you can see the output. + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + return fmt.Errorf("failed to run go install command for package %s: %w", pkg, err) + } + } + + fmt.Println("Installation of protoc plugins completed successfully.") + return nil +}