15
15
package main
16
16
17
17
import (
18
- "bytes"
18
+ "context"
19
+ "crypto/sha256"
20
+ "encoding/hex"
19
21
"fmt"
22
+ "hash"
20
23
"io"
21
24
"net/http"
22
25
"os"
23
- "os/exec"
24
26
"path/filepath"
25
27
"strings"
26
- "text/template"
27
28
)
28
29
29
30
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 )
31
+ err := run (context .Background ())
37
32
if err != nil {
33
+ fmt .Fprintf (os .Stderr , "%v\n " , err )
38
34
os .Exit (1 )
39
35
}
36
+ }
40
37
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 )
38
+ func run (ctx context.Context ) error {
39
+ if len (os .Args ) < 2 {
40
+ return fmt .Errorf ("must specify new version" )
46
41
}
47
42
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 )
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
53
49
}
54
50
55
- err = os .WriteFile (filepath .Join ("Formula" , "kpt.rb" ), b . Bytes ( ), 0644 )
51
+ err = os .WriteFile (filepath .Join ("Formula" , "kpt.rb" ), [] byte ( formula ), 0644 )
56
52
if err != nil {
57
- fmt .Fprintf (os .Stderr , "%v" , err )
58
- os .Exit (1 )
53
+ return err
59
54
}
55
+ return nil
60
56
}
61
57
62
- func getSha (version string ) (string , error ) {
63
- // create the dir for the data
64
- d , err := os .MkdirTemp ("" , "kpt-bin" )
58
+ func buildFormula (httpClient * http.Client , url string ) (string , error ) {
59
+ sha256 , err := hashURL (httpClient , url , sha256 .New ())
65
60
if err != nil {
66
- fmt .Fprintf (os .Stderr , "%v" , err )
67
61
return "" , err
68
62
}
69
- defer os .RemoveAll (d )
70
63
71
- fmt .Println (
72
- "fetching https://github.com/GoogleContainerTools/kpt/archive/" + version + ".tar.gz" )
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
+
73
75
// get the content
74
- resp , err := http .Get (
75
- "https://github.com/GoogleContainerTools/kpt/archive/" + version + ".tar.gz" )
76
+ resp , err := httpClient .Get (url )
76
77
if err != nil {
77
- fmt .Fprintf (os .Stderr , "%v" , err )
78
- return "" , err
78
+ return "" , fmt .Errorf ("error getting %q: %w" , url , err )
79
79
}
80
80
defer resp .Body .Close ()
81
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
- }()
82
+ if resp .StatusCode != 200 {
83
+ return "" , fmt .Errorf ("unexpected response from %q: %v" , url , resp .Status )
84
+ }
96
85
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
86
+ if _ , err := io .Copy (hasher , resp .Body ); err != nil {
87
+ return "" , fmt .Errorf ("error hashing response from %q: %w" , url , err )
103
88
}
104
- parts := strings .Split (string (o ), " " )
105
- fmt .Println ("new sha: " + parts [0 ])
106
- return parts [0 ], nil
107
- }
108
89
109
- type Input struct {
110
- Version string
111
- Sha string
90
+ // calculate the sha
91
+ hash := hasher . Sum ( nil )
92
+ return hex . EncodeToString ( hash ), nil
112
93
}
113
94
114
- const formula = `# Copyright 2019 Google LLC
95
+ const formulaTemplate = `# Copyright 2019 Google LLC
115
96
#
116
97
# Licensed under the Apache License, Version 2.0 (the "License");
117
98
# you may not use this file except in compliance with the License.
@@ -128,8 +109,8 @@ const formula = `# Copyright 2019 Google LLC
128
109
class Kpt < Formula
129
110
desc "Toolkit to manage,and apply Kubernetes Resource config data files"
130
111
homepage "https://googlecontainertools.github.io/kpt"
131
- url "https://github.com/GoogleContainerTools/kpt/archive/{{.Version}}.tar.gz "
132
- sha256 "{{.Sha }}"
112
+ url "{{url}} "
113
+ sha256 "{{sha256 }}"
133
114
134
115
depends_on "go" => :build
135
116
0 commit comments