get.porter.sh/porter@v1.3.0/mage/setup/tools.go (about)

     1  package setup
     2  
     3  import (
     4  	"os/exec"
     5  	"runtime"
     6  
     7  	"github.com/magefile/mage/mg"
     8  	"github.com/uwu-tools/magex/mgx"
     9  	"github.com/uwu-tools/magex/pkg"
    10  	"github.com/uwu-tools/magex/pkg/archive"
    11  	"github.com/uwu-tools/magex/pkg/downloads"
    12  )
    13  
    14  func EnsureProtobufTools() {
    15  	//TODO: add more tools
    16  	// https://github.com/bufbuild/buf/releases protoc-gen-buf-breaking, protoc-gen-buf-lint
    17  	// protoc https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protoc-21.12-linux-x86_64.zip``
    18  	//TODO: add more tools
    19  	// https://github.com/bufbuild/buf/releases protoc-gen-buf-breaking, protoc-gen-buf-lint
    20  	// protoc https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protoc-21.12-linux-x86_64.zip``
    21  	mgx.Must(pkg.EnsurePackageWith(pkg.EnsurePackageOptions{
    22  		Name:           "google.golang.org/protobuf/cmd/protoc-gen-go",
    23  		DefaultVersion: "v1.28",
    24  		VersionCommand: "--version",
    25  	}))
    26  	mgx.Must(pkg.EnsurePackageWith(pkg.EnsurePackageOptions{
    27  		Name:           "google.golang.org/grpc/cmd/protoc-gen-go-grpc",
    28  		DefaultVersion: "v1.2",
    29  		VersionCommand: "--version",
    30  	}))
    31  }
    32  
    33  // IsCommandInPath determines if a command can be called based on the current PATH.
    34  func IsCommandInPath(cmd string) (bool, error) {
    35  	_, err := exec.LookPath(cmd)
    36  	if err != nil {
    37  		return false, err
    38  	}
    39  	return true, nil
    40  }
    41  
    42  func EnsureBufBuild() {
    43  	mg.Deps(EnsureProtobufTools)
    44  	if ok, _ := pkg.IsCommandAvailable("buf", "--version", "1.11.0"); ok {
    45  		return
    46  	}
    47  	opts := downloads.DownloadOptions{
    48  		UrlTemplate: "https://github.com/bufbuild/buf/releases/download/v{{.VERSION}}/buf-{{.GOOS}}-{{.GOARCH}}{{.EXT}}",
    49  		Name:        "buf",
    50  		Version:     "1.11.0",
    51  		OsReplacement: map[string]string{
    52  			"darwin":  "Darwin",
    53  			"linux":   "Linux",
    54  			"windows": "Windows",
    55  		},
    56  		ArchReplacement: map[string]string{
    57  			"amd64": "x86_64",
    58  		},
    59  	}
    60  
    61  	if runtime.GOOS == "windows" {
    62  		opts.Ext = ".exe"
    63  	}
    64  
    65  	err := downloads.DownloadToGopathBin(opts)
    66  	mgx.Must(err)
    67  }
    68  
    69  func EnsureGRPCurl() {
    70  	if ok, _ := IsCommandInPath("grpcurl"); ok {
    71  		return
    72  	}
    73  
    74  	target := "grpcurl{{.EXT}}"
    75  	if runtime.GOOS == "windows" {
    76  		target = "grpcurl.exe"
    77  	}
    78  
    79  	opts := archive.DownloadArchiveOptions{
    80  		DownloadOptions: downloads.DownloadOptions{
    81  			UrlTemplate: "https://github.com/fullstorydev/grpcurl/releases/download/v{{.VERSION}}/grpcurl_{{.VERSION}}_{{.GOOS}}_{{.GOARCH}}{{.EXT}}",
    82  			Name:        "grpcurl",
    83  			Version:     "1.8.7",
    84  			OsReplacement: map[string]string{
    85  				"darwin": "osx",
    86  			},
    87  			ArchReplacement: map[string]string{
    88  				"amd64": "x86_64",
    89  			},
    90  		},
    91  		ArchiveExtensions: map[string]string{
    92  			"linux":   ".tar.gz",
    93  			"darwin":  ".tar.gz",
    94  			"windows": ".zip",
    95  		},
    96  		TargetFileTemplate: target,
    97  	}
    98  	err := archive.DownloadToGopathBin(opts)
    99  	mgx.Must(err)
   100  }