...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package main
18
19 import (
20 "io/ioutil"
21 "log"
22 "os"
23 "path"
24
25 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/snippet/snippetgeneration"
26 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/fileutil"
27 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/repo"
28 "gopkg.in/yaml.v2"
29 )
30
31 const outputFileMode = 0600
32 const dirMode = 0700
33
34 func main() {
35
36 snippetPath := repo.GetResourcesSnippetsPath()
37 if err := os.RemoveAll(snippetPath); err != nil {
38 log.Fatalf("error deleting dir %v: %v", snippetPath, err)
39 }
40 if err := os.Mkdir(snippetPath, dirMode); err != nil {
41 log.Fatalf("error recreating dir %v: %v", snippetPath, err)
42 }
43
44 samplesPath := repo.GetResourcesSamplesPath()
45 resources, err := fileutil.SubdirsIn(samplesPath)
46 if err != nil {
47 log.Fatal(err)
48 }
49
50 for _, resource := range resources {
51 sampleFilePath, err := snippetgeneration.PathToSampleFileUsedForSnippets(resource)
52 if err != nil {
53 log.Fatal(err)
54 }
55
56 content, err := ioutil.ReadFile(sampleFilePath)
57 if err != nil {
58 log.Fatalf("error reading file: %v", err)
59 }
60
61 snippet, err := snippetgeneration.SnippifyResourceConfig(content)
62 if err != nil {
63 log.Fatal(err)
64 }
65
66 sampleFileName := path.Base(sampleFilePath)
67 err = outputSnippetToFile(sampleFileName, snippet)
68 if err != nil {
69 log.Fatalf("error writing snippet to file: %v", err)
70 }
71 }
72 }
73
74 func outputSnippetToFile(outputFileName string, s snippetgeneration.Snippet) error {
75 outputPath := path.Join(repo.GetResourcesSnippetsPath(), outputFileName)
76 b, err := yaml.Marshal(s)
77 if err != nil {
78 return err
79 }
80 err = ioutil.WriteFile(outputPath, b, outputFileMode)
81 if err != nil {
82 return err
83 }
84 return nil
85 }
86
View as plain text