-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
118 lines (97 loc) · 2.39 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
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/blendle/kubecrt/chartsconfig"
"github.com/blendle/kubecrt/config"
"github.com/blendle/kubecrt/helm"
"github.com/ghodss/yaml"
)
func main() {
cli := config.CLI()
opts, err := config.NewCLIOptions(cli)
if err != nil {
fmt.Fprintf(os.Stderr, "kubecrt arguments error: \n\n%s\n", err)
os.Exit(1)
}
if err = helm.Init(); err != nil {
fmt.Fprintf(os.Stderr, "error initialising helm: \n\n%s\n", err)
os.Exit(1)
}
if cli["--repo"] != nil {
for _, r := range strings.Split(cli["--repo"].(string), ",") {
p := strings.SplitN(r, "=", 2)
repo := strings.TrimSpace(string(p[0]))
url := strings.TrimSpace(string(p[1]))
if err = helm.AddRepository(repo, url); err != nil {
fmt.Fprintf(os.Stderr, "error adding repository: \n\n%s\n", err)
os.Exit(1)
}
}
}
cfg, err := readInput(opts.ChartsConfigurationPath)
if err != nil {
fmt.Fprintf(os.Stderr, "charts config IO error: \n\n%s\n", err)
os.Exit(1)
}
cc, err := chartsconfig.NewChartsConfiguration(cfg, opts.PartialTemplatesPath)
if err != nil {
fmt.Fprintf(os.Stderr, "charts config parsing error: \n\n%s\n", err)
os.Exit(1)
}
name := opts.ChartsConfigurationOptions.Name
if name != "" {
cc.Name = name
}
namespace := opts.ChartsConfigurationOptions.Namespace
if namespace != "" {
cc.Namespace = namespace
}
if err = cc.Validate(); err != nil {
fmt.Fprintf(os.Stderr, "charts validation error: \n\n%s\n", err)
os.Exit(1)
}
out, err := cc.ParseCharts()
if err != nil {
fmt.Fprintf(os.Stderr, "chart parsing error: %s\n", err)
os.Exit(1)
}
if opts.OutputJSON {
out, err = toJSON(out)
if err != nil {
fmt.Printf("error converting chart to JSON format: %s\n", err)
os.Exit(1)
}
}
if cli["--output"] == nil {
fmt.Print(string(out))
return
}
if err = ioutil.WriteFile(cli["--output"].(string), out, 0644); err != nil {
fmt.Fprintf(os.Stderr, "output IO error: %s\n", err)
os.Exit(1)
}
}
func readInput(input string) ([]byte, error) {
if input == "-" {
return ioutil.ReadAll(os.Stdin)
}
return ioutil.ReadFile(input)
}
func toJSON(input []byte) ([]byte, error) {
var err error
bs := bytes.Split(input, []byte("---"))
for i := range bs {
if len(bs[i]) == 0 {
continue
}
bs[i], err = yaml.YAMLToJSON(bs[i])
if err != nil {
return nil, err
}
}
return bytes.Join(bs, []byte("\n")), nil
}