-
Notifications
You must be signed in to change notification settings - Fork 0
/
argocd-util-splitter.go
66 lines (49 loc) · 1.25 KB
/
argocd-util-splitter.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
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
var lines, kind, name []string
func main() {
var (
src = flag.String("src", "/home/backup.yaml", "path of argocd backup yaml")
dst = flag.String("dst", "/tmp/", "output directory")
)
flag.Parse()
yamlFile, err := ioutil.ReadFile(*src)
if err != nil {
fmt.Println("Unable to read source file -", err)
os.Exit(1)
}
manifests := strings.Split(string(yamlFile), "---\n")
reKind := regexp.MustCompile("kind: (.*)")
reName := regexp.MustCompile(" name: (.*)")
for _, manifest := range manifests {
scanner := bufio.NewScanner(strings.NewReader(manifest))
for scanner.Scan() {
line := scanner.Text()
if kind == nil {
kind = reKind.FindStringSubmatch(line)
}
if name == nil {
name = reName.FindStringSubmatch(line)
}
}
path := filepath.Join(*dst, fmt.Sprintf("%s-%s.yaml", name[1], kind[1]))
err := ioutil.WriteFile(path, []byte(manifest), 0644)
if err != nil {
fmt.Println("Unable to write file -", err)
os.Exit(1)
}
// reset manifest details to nil
lines, name, kind = nil, nil, nil
}
fmt.Println(fmt.Sprintf("Success - ArgoCD individual backup manifests have been written to %s", *dst))
os.Exit(0)
}