This repository was archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
107 lines (88 loc) · 2.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"github.com/chartmuseum/helm-push/pkg/helm"
"github.com/spf13/cobra"
)
var version = ""
func main() {
cmd := &cobra.Command{
Use: "helm push [chart archive/directory] [repository name]",
Short: "push chart package to Coding artifact",
Long: "push chart package to Coding artifact",
SilenceUsage: false,
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return errors.New("chart archive/directory and repository name required")
}
return push(args[0], args[1])
},
}
flags := cmd.Flags()
flags.Parse(os.Args[1:])
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
func push(chartName, repository string) error {
chart, err := helm.GetChartByName(chartName)
if err != nil {
return err
}
repo, err := helm.GetRepoByName(repository)
if err != nil {
return err
}
tmp, err := ioutil.TempDir("", "helm-push-")
if err != nil {
return err
}
defer os.RemoveAll(tmp)
chartPackagePath, err := helm.CreateChartPackage(chart, tmp)
if err != nil {
return err
}
chartArchiveFile, err := os.Open(chartPackagePath)
if err != nil {
return err
}
defer chartArchiveFile.Close()
fileInfo, err := chartArchiveFile.Stat()
if err != nil {
return err
}
req, err := http.NewRequest("POST", repo.URL, chartArchiveFile)
if err != nil {
return err
}
req.SetBasicAuth(repo.Username, repo.Password)
req.Header.Set("User-Agent", fmt.Sprintf("helm-push/%s", version))
req.Header.Set("Content-Length", strconv.FormatInt(fileInfo.Size(), 10))
fmt.Printf("pushing chart '%s' to repository '%s' ...\n", chart.Metadata.Name, repo.URL)
resp, err := (&http.Client{}).Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode >= 400 {
return fmt.Errorf(
"fail to push chart '%s' to repository '%s': repository returns error %s: %s",
chart.Metadata.Name,
repo.URL,
http.StatusText(resp.StatusCode),
string(body),
)
}
fmt.Printf("finished pushing chart '%s' to repository '%s'\n", chart.Metadata.Name, repo.URL)
return nil
}