...

Source file src/sigs.k8s.io/release-utils/mage/cosign.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  	"log"
    22  	"runtime"
    23  
    24  	"github.com/uwu-tools/magex/pkg"
    25  	"github.com/uwu-tools/magex/pkg/downloads"
    26  )
    27  
    28  const defaultCosignVersion = "v2.2.0"
    29  
    30  // EnsureCosign makes sure that the specified cosign version is available
    31  func EnsureCosign(version string) error {
    32  	if version == "" {
    33  		version = defaultCosignVersion
    34  	}
    35  
    36  	log.Printf("Checking if `cosign` version %s is installed\n", version)
    37  	found, err := pkg.IsCommandAvailable("cosign", "version", version)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	if !found {
    43  		fmt.Println("`cosign` not found")
    44  		return InstallCosign(version)
    45  	}
    46  
    47  	fmt.Println("`cosign` is installed!")
    48  	return nil
    49  }
    50  
    51  // InstallCosign installs the required cosign version
    52  func InstallCosign(version string) error {
    53  	fmt.Println("Will install `cosign`")
    54  	target := "cosign"
    55  	if runtime.GOOS == "windows" {
    56  		target = "cosign.exe"
    57  	}
    58  
    59  	opts := downloads.DownloadOptions{
    60  		UrlTemplate: "https://github.com/sigstore/cosign/releases/download/{{.VERSION}}/cosign-{{.GOOS}}-{{.GOARCH}}",
    61  		Name:        target,
    62  		Version:     version,
    63  		Ext:         "",
    64  	}
    65  
    66  	return downloads.DownloadToGopathBin(opts)
    67  }
    68  

View as plain text