This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (72 loc) · 1.49 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
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"time"
yaml "gopkg.in/yaml.v2"
)
type configmap struct {
Kind string `yaml:"kind"`
APIVersion string `yaml:"apiVersion"`
Metadata metadata `yaml:"metadata"`
BinaryData map[string]string `yaml:"binaryData"`
}
type metadata struct {
CreationTimestamp time.Time `yaml:"creationTimestamp"`
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
}
var args struct {
Name string
Namespace string
Files []string
}
func parseArgs() {
for _, arg := range os.Args[1:] {
switch true {
case args.Name == "":
args.Name = arg
case args.Namespace == "":
args.Namespace = arg
default:
if _, err := os.Lstat(arg); err == nil {
args.Files = append(args.Files, arg)
}
}
}
}
func main() {
parseArgs()
cm := configmap{
Kind: "ConfigMap",
APIVersion: "v1",
Metadata: metadata{
CreationTimestamp: time.Now().UTC(),
Name: args.Name,
Namespace: args.Namespace,
},
BinaryData: make(map[string]string),
}
b64 := base64.StdEncoding
for _, f := range args.Files {
buf, err := ioutil.ReadFile(f)
if err != nil {
panic(err)
}
_, name := path.Split(f)
cm.BinaryData[name] = b64.EncodeToString(buf)
}
if len(cm.BinaryData) == 0 {
log.Println("Nothing to save.")
os.Exit(1)
}
buf, err := yaml.Marshal(&cm)
if err != nil {
panic(err)
}
fmt.Println(string(buf))
}