-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdir2cm.go
119 lines (100 loc) · 2.05 KB
/
dir2cm.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
119
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path"
//"path/filepath"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type MetaData struct {
Name string `yaml: name`
Labels map[string]string `yaml: labels`
}
type ConfigMap struct {
ApiVersion string `yaml: apiVersion`
Kind string `yaml: kind`
Metadata MetaData `yaml: metadata`
Data map[string]string `yaml: data`
}
func EmptyConfigMap(name string) *ConfigMap {
cm := &ConfigMap{
ApiVersion: "v1",
Kind: "ConfigMap",
Metadata: MetaData{
Name: name,
},
Data: map[string]string{},
}
return cm
}
// Adds a file
func (c *ConfigMap) AddFile(f *ConfigMapFile) error {
c.Data[f.Name] = string(f.Contents)
return nil
}
func (c *ConfigMap) DumpYaml() {
yml, err := yaml.Marshal(*c)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(yml))
}
/**
* ConfigMapFile
*
* Will be come a key in the ConfigMap
*/
type ConfigMapFile struct {
// Actual FS path
Path string
// Name/key for configmap (basename(Path))
Name string `yaml: name`
// Contents (as bytes)
Contents []byte
}
func NewConfigMapFile(fpath string) (*ConfigMapFile, error) {
contents, err := ioutil.ReadFile(fpath)
if err != nil {
return nil, err
}
cm := &ConfigMapFile{
Path: fpath,
Name: path.Base(fpath),
Contents: contents,
}
return cm, nil
}
func main() {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
name := flag.String("name", "my-config", "The ConfigMap Metadata.Name")
dir := flag.String("dir", cwd, "The input directory")
flag.Parse()
//var files []string
files, err := ioutil.ReadDir(*dir)
if err != nil {
panic(err)
}
cm := EmptyConfigMap(*name)
for _, file := range files {
if file.IsDir() {
continue
}
fullpath := path.Join(*dir, file.Name())
cmf, err := NewConfigMapFile(fullpath)
if err != nil {
log.Warnf("Problem with file: %s", err)
continue
}
err = cm.AddFile(cmf)
if err != nil {
log.Warnf("Couldn't add file %s (%s)", fullpath, err)
}
}
cm.DumpYaml()
}