...

Source file src/sigs.k8s.io/release-utils/mage/ko.go

Documentation: sigs.k8s.io/release-utils/mage

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mage
    18  
    19  import (
    20  	"fmt"
    21  	"runtime"
    22  
    23  	"github.com/uwu-tools/magex/pkg"
    24  	"github.com/uwu-tools/magex/pkg/archive"
    25  	"github.com/uwu-tools/magex/pkg/downloads"
    26  )
    27  
    28  const defaultKoVersion = "0.15.0"
    29  
    30  // EnsureKO
    31  func EnsureKO(version string) error {
    32  	if version == "" {
    33  		version = defaultKoVersion
    34  	}
    35  
    36  	fmt.Printf("Checking if `ko` version %s is installed\n", version)
    37  	found, err := pkg.IsCommandAvailable("ko", "version", version)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	if !found {
    43  		fmt.Println("`ko` not found")
    44  		return InstallKO(version)
    45  	}
    46  
    47  	fmt.Println("`ko` is installed!")
    48  	return nil
    49  }
    50  
    51  // Maybe we can  move this to release-utils
    52  func InstallKO(version string) error {
    53  	fmt.Println("Will install `ko`")
    54  	target := "ko"
    55  	if runtime.GOOS == "windows" {
    56  		target = "ko.exe"
    57  	}
    58  
    59  	opts := archive.DownloadArchiveOptions{
    60  		DownloadOptions: downloads.DownloadOptions{
    61  			UrlTemplate: "https://github.com/ko-build/ko/releases/download/v{{.VERSION}}/ko_{{.VERSION}}_{{.GOOS}}_{{.GOARCH}}{{.EXT}}",
    62  			Name:        "ko",
    63  			Version:     version,
    64  			OsReplacement: map[string]string{
    65  				"darwin":  "Darwin",
    66  				"linux":   "Linux",
    67  				"windows": "Windows",
    68  			},
    69  			ArchReplacement: map[string]string{
    70  				"amd64": "x86_64",
    71  			},
    72  		},
    73  		ArchiveExtensions: map[string]string{
    74  			"linux":   ".tar.gz",
    75  			"darwin":  ".tar.gz",
    76  			"windows": ".tar.gz",
    77  		},
    78  		TargetFileTemplate: target,
    79  	}
    80  
    81  	return archive.DownloadToGopathBin(opts)
    82  }
    83  

View as plain text