-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (132 loc) · 3.61 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"flag"
"github.com/drewsonne/go-gocd/gocd"
"github.com/drewsonne/gocd-tf-generate/generator"
"context"
"fmt"
"io/ioutil"
"os"
)
func main() {
// Version for the cli tool
var Version string
var id, resource, output, profile string
var outputToFile, debug, printVersion bool
flag.StringVar(&resource, "resource", "",
"Name of the resource to generate tf config for. Use '*' for all resources. Default: * ."+
"Allowed Values: (pipeline_template, pipeline)")
flag.StringVar(&id, "id", "*",
"Specify the ID of the resource to generate a specific resource. Omit this to generate all.")
flag.StringVar(&profile, "profile", "default", "Specify the gocd profile to use.")
flag.BoolVar(&outputToFile, "to-file", false, "If supplied, output to file.")
flag.BoolVar(&debug, "debug", false, "Print debug logging")
flag.BoolVar(&printVersion, "version", false, "Print version and exit")
flag.Parse()
if len(os.Args) < 2 {
flag.Usage()
return
}
if (printVersion) {
fmt.Printf("gocd-tf-generate '%s'", Version)
return
}
cfg := gocd.Configuration{}
if err := gocd.LoadConfigByName(profile, &cfg); err != nil {
panic(err)
}
c := cfg.Client()
ctx := context.Background()
doFilter := id != "" && id != "*"
if debug {
fmt.Printf("Searching for resource: `%s`\n", resource)
}
if resource == "pipeline_template" {
if debug {
fmt.Printf("Listing pipeline templates...")
}
templates, _, err := c.PipelineTemplates.List(ctx)
if err != nil {
panic(err)
}
if doFilter {
if debug {
fmt.Printf("Searching for '%s' with id '%s'\n", resource, id)
}
for _, template := range templates {
if template.Name == id {
templates = []*gocd.PipelineTemplate{template}
break
}
}
}
for _, templateDescription := range templates {
template, _, err := c.PipelineTemplates.Get(ctx, templateDescription.Name)
if err != nil {
panic(err)
}
output, err = generator.RenderPipelineTemplate(template)
if err != nil {
panic(err)
}
writeOutput(outputToFile, template.Name, []byte(output))
}
} else if resource == "pipeline" {
if debug {
fmt.Printf("Listing pipelne groups...\n")
}
pipelinesGroups, _, err := c.PipelineGroups.List(ctx, "")
if err != nil {
panic(err)
}
for _, group := range (*pipelinesGroups) {
if debug {
fmt.Printf("Searching group '%s' for pipeline with id '%s'.\n", group.Name, id)
}
for _, pipeline := range group.Pipelines {
if doFilter {
if pipeline.Name == id {
if debug {
fmt.Printf("Getting config for pipeline '%s.%s'", group.Name, pipeline.Name)
}
pipelineCfg, _, err := c.PipelineConfigs.Get(ctx, pipeline.Name)
if err != nil {
panic(err)
}
if output, err = generator.RenderPipeline(pipelineCfg, group.Name, debug); err != nil {
panic(err)
}
if err := writeOutput(outputToFile, pipelineCfg.Name, []byte(output)); err != nil {
os.Exit(1)
}
os.Exit(0)
}
} else {
pipelineCfg, _, err := c.PipelineConfigs.Get(ctx, pipeline.Name)
if err != nil {
panic(err)
}
output, err = generator.RenderPipeline(pipelineCfg, group.Name, debug)
if err != nil {
panic(err)
}
if err := writeOutput(outputToFile, pipelineCfg.Name, []byte(output)); err != nil {
os.Exit(1)
}
}
}
}
}
}
func writeOutput(outputToFile bool, name string, output []byte) error {
if outputToFile {
if err := ioutil.WriteFile(name+".tf", output, 0644); err != nil {
return err
}
} else {
if _, err := fmt.Println(string(output)); err != nil {
return err
}
}
return nil
}