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 }