github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/release/formula/main.go (about)

     1  // Copyright 2019 The kpt Authors
     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  	"context"
    19  	"crypto/sha256"
    20  	"encoding/hex"
    21  	"fmt"
    22  	"hash"
    23  	"io"
    24  	"net/http"
    25  	"os"
    26  	"path/filepath"
    27  	"strings"
    28  )
    29  
    30  func main() {
    31  	err := run(context.Background())
    32  	if err != nil {
    33  		fmt.Fprintf(os.Stderr, "%v\n", err)
    34  		os.Exit(1)
    35  	}
    36  }
    37  
    38  func run(_ context.Context) error {
    39  	if len(os.Args) < 2 {
    40  		return fmt.Errorf("must specify new version")
    41  	}
    42  
    43  	version := os.Args[1]
    44  	url := "https://github.com/GoogleContainerTools/kpt/archive/" + version + ".tar.gz"
    45  
    46  	formula, err := buildFormula(http.DefaultClient, url)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	err = os.WriteFile(filepath.Join("Formula", "kpt.rb"), []byte(formula), 0644)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	return nil
    56  }
    57  
    58  func buildFormula(httpClient *http.Client, url string) (string, error) {
    59  	sha256, err := hashURL(httpClient, url, sha256.New())
    60  	if err != nil {
    61  		return "", err
    62  	}
    63  
    64  	// generate the formula text
    65  	formula := formulaTemplate
    66  	formula = strings.ReplaceAll(formula, "{{url}}", url)
    67  	formula = strings.ReplaceAll(formula, "{{sha256}}", sha256)
    68  
    69  	return formula, nil
    70  }
    71  
    72  func hashURL(httpClient *http.Client, url string, hasher hash.Hash) (string, error) {
    73  	fmt.Printf("fetching %q\n", url)
    74  
    75  	// get the content
    76  	resp, err := httpClient.Get(url)
    77  	if err != nil {
    78  		return "", fmt.Errorf("error getting %q: %w", url, err)
    79  	}
    80  	defer resp.Body.Close()
    81  
    82  	if resp.StatusCode != 200 {
    83  		return "", fmt.Errorf("unexpected response from %q: %v", url, resp.Status)
    84  	}
    85  
    86  	if _, err := io.Copy(hasher, resp.Body); err != nil {
    87  		return "", fmt.Errorf("error hashing response from %q: %w", url, err)
    88  	}
    89  
    90  	// calculate the sha
    91  	hash := hasher.Sum(nil)
    92  	return hex.EncodeToString(hash), nil
    93  }
    94  
    95  const formulaTemplate = `# Copyright 2019 The kpt Authors
    96  #
    97  # Licensed under the Apache License, Version 2.0 (the "License");
    98  # you may not use this file except in compliance with the License.
    99  # You may obtain a copy of the License at
   100  #
   101  #      http://www.apache.org/licenses/LICENSE-2.0
   102  #
   103  # Unless required by applicable law or agreed to in writing, software
   104  # distributed under the License is distributed on an "AS IS" BASIS,
   105  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   106  # See the License for the specific language governing permissions and
   107  # limitations under the License.
   108  
   109  class Kpt < Formula
   110    desc "Toolkit to manage,and apply Kubernetes Resource config data files"
   111    homepage "https://googlecontainertools.github.io/kpt"
   112    url "{{url}}"
   113    sha256 "{{sha256}}"
   114  
   115    depends_on "go" => :build
   116  
   117    def install
   118      ENV["GO111MODULE"] = "on"
   119      system "go", "build", "-ldflags", "-X github.com/GoogleContainerTools/kpt/run.version=#{version}", *std_go_args
   120    end
   121  
   122    test do
   123      assert_match version.to_s, shell_output("#{bin}/kpt version")
   124    end
   125  end
   126  `