add system setup and verification

This commit is contained in:
2025-02-22 23:33:02 -07:00
parent 60d7e07114
commit a8f2b832b4
3 changed files with 90 additions and 0 deletions

View File

@ -12,6 +12,7 @@ func main() {
generateCmd(),
webappCmd(),
tailwindCmd(),
setupCmd(),
}
app := &cli.App{

View File

@ -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
},
}
}

73
cmd/cli/setup.go Normal file
View File

@ -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
}