github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/release/formula/main.go (about)

     1  // Copyright 2019 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io"
    21  	"net/http"
    22  	"os"
    23  	"os/exec"
    24  	"path/filepath"
    25  	"strings"
    26  	"text/template"
    27  )
    28  
    29  func main() {
    30  	if len(os.Args) < 2 {
    31  		fmt.Fprintf(os.Stderr, "must specify new version\n")
    32  		os.Exit(1)
    33  	}
    34  	input := Input{Version: os.Args[1]}
    35  	var err error
    36  	input.Sha, err = getSha(input.Version)
    37  	if err != nil {
    38  		os.Exit(1)
    39  	}
    40  
    41  	// generate the formula text
    42  	t, err := template.New("formula").Parse(formula)
    43  	if err != nil {
    44  		fmt.Fprintf(os.Stderr, "%v", err)
    45  		os.Exit(1)
    46  	}
    47  
    48  	// write the new formula
    49  	b := &bytes.Buffer{}
    50  	if err = t.Execute(b, input); err != nil {
    51  		fmt.Fprintf(os.Stderr, "%v", err)
    52  		os.Exit(1)
    53  	}
    54  
    55  	err = os.WriteFile(filepath.Join("Formula", "kpt.rb"), b.Bytes(), 0644)
    56  	if err != nil {
    57  		fmt.Fprintf(os.Stderr, "%v", err)
    58  		os.Exit(1)
    59  	}
    60  }
    61  
    62  func getSha(version string) (string, error) {
    63  	// create the dir for the data
    64  	d, err := os.MkdirTemp("", "kpt-bin")
    65  	if err != nil {
    66  		fmt.Fprintf(os.Stderr, "%v", err)
    67  		return "", err
    68  	}
    69  	defer os.RemoveAll(d)
    70  
    71  	fmt.Println(
    72  		"fetching https://github.com/GoogleContainerTools/kpt/archive/" + version + ".tar.gz")
    73  	// get the content
    74  	resp, err := http.Get(
    75  		"https://github.com/GoogleContainerTools/kpt/archive/" + version + ".tar.gz")
    76  	if err != nil {
    77  		fmt.Fprintf(os.Stderr, "%v", err)
    78  		return "", err
    79  	}
    80  	defer resp.Body.Close()
    81  
    82  	// write the file
    83  	func() {
    84  		out, err := os.Create(filepath.Join(d, version+".tar.gz"))
    85  		if err != nil {
    86  			fmt.Fprintf(os.Stderr, "%v", err)
    87  			os.Exit(1)
    88  		}
    89  
    90  		if _, err = io.Copy(out, resp.Body); err != nil {
    91  			fmt.Fprintf(os.Stderr, "%v", err)
    92  			os.Exit(1)
    93  		}
    94  		out.Close()
    95  	}()
    96  
    97  	// calculate the sha
    98  	e := exec.Command("sha256sum", filepath.Join(d, version+".tar.gz"))
    99  	o, err := e.Output()
   100  	if err != nil {
   101  		fmt.Fprintf(os.Stderr, "%v", err)
   102  		return "", err
   103  	}
   104  	parts := strings.Split(string(o), " ")
   105  	fmt.Println("new sha: " + parts[0])
   106  	return parts[0], nil
   107  }
   108  
   109  type Input struct {
   110  	Version string
   111  	Sha     string
   112  }
   113  
   114  const formula = `# Copyright 2019 Google LLC
   115  #
   116  # Licensed under the Apache License, Version 2.0 (the "License");
   117  # you may not use this file except in compliance with the License.
   118  # You may obtain a copy of the License at
   119  #
   120  #      http://www.apache.org/licenses/LICENSE-2.0
   121  #
   122  # Unless required by applicable law or agreed to in writing, software
   123  # distributed under the License is distributed on an "AS IS" BASIS,
   124  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   125  # See the License for the specific language governing permissions and
   126  # limitations under the License.
   127  
   128  class Kpt < Formula
   129    desc "Toolkit to manage,and apply Kubernetes Resource config data files"
   130    homepage "https://googlecontainertools.github.io/kpt"
   131    url "https://github.com/GoogleContainerTools/kpt/archive/{{.Version}}.tar.gz"
   132    sha256 "{{.Sha}}"
   133  
   134    depends_on "go" => :build
   135  
   136    def install
   137      ENV["GO111MODULE"] = "on"
   138      system "go", "build", "-ldflags", "-X github.com/GoogleContainerTools/kpt/run.version=#{version}", *std_go_args
   139    end
   140  
   141    test do
   142      assert_match version.to_s, shell_output("#{bin}/kpt version")
   143    end
   144  end
   145  `