-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (54 loc) · 1.19 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
package main
import (
"flag"
"bufio"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"strings"
"regexp"
)
var shortenKind *bool
var findLowerCase = regexp.MustCompile("[a-z]*")
func init() {
shortenKind = flag.Bool("shorten", false, "Shorten the Kind used in the filename")
flag.Parse()
}
func main() {
reader := bufio.NewScanner(os.Stdin)
reader.Split(splitOnDashes)
for reader.Scan() {
manifest := reader.Text()
filename, _ := buildFilename(manifest)
ioutil.WriteFile(filename, []byte(manifest), 0644)
}
}
func buildFilename(manifest string) (string, error) {
obj := manifestStub{}
yaml.Unmarshal([]byte(manifest), &obj)
kind := obj.Kind
if *shortenKind {
kind = findLowerCase.ReplaceAllString(kind, "")
}
return strings.ToLower(fmt.Sprintf("%s-%s.yaml", kind, obj.Metadata.Name)), nil
}
func splitOnDashes(data []byte, atEOF bool) (int, []byte, error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := strings.Index(string(data), "---"); i >= 0 {
return i + 3, data[0:i], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
}
type manifestStub struct {
Kind string
Metadata manifestMetadata
}
type manifestMetadata struct {
Name string
}