/
main.go
165 lines (143 loc) · 4.5 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package cli_utils
import (
"fmt"
"github.com/common-nighthawk/go-figure"
"github.com/enescakir/emoji"
"github.com/hashicorp/go-version"
"github.com/olekukonko/tablewriter"
"github.com/sirrend/terrap-cli/internal/commons"
"github.com/sirrend/terrap-cli/internal/workspace"
"github.com/spf13/cobra"
validate "golang.org/x/mod/semver"
"os"
"strings"
)
// SirrendLogoPrint
/*
@brief:
SirrendLogoPrint prints the Sirrend logo
*/
func SirrendLogoPrint() {
myFigure := figure.NewColorFigure("Sirrend", "", "purple", true)
myFigure.Print()
fmt.Println()
}
// flagsToAPIRepresentation
/*
@brief:
flagsToAPIRepresentation change flag names to the corresponding API json field name
@params:
flags - []string - the flags list to change
@returns:
[]string - the flags as stated under the API response json
*/
func flagsToAPIRepresentation(flags []string) []string {
for i, flag := range flags {
if flag == "data-sources" {
flags[i] = "data"
} else if flag == "resources" {
flags[i] = "resource"
}
}
return flags
}
// ChangedComponentsFlags
/*
@brief:
ChangedComponentsFlags checks which components flag were changed by the user.
The passed cmd must have the flags: provider, data-sources and resources under it
@params:
cmd - cobra.Command - the Command to look for flags under
@returns:
[]string - the resources to filter the output according to
*/
func ChangedComponentsFlags(cmd cobra.Command) []string {
flags := []string{"provider", "data-sources", "resources"}
var componentsShow []string
for _, flag := range flags {
if cmd.Flag(flag).Changed {
componentsShow = append(componentsShow, flag)
}
}
if len(componentsShow) > 0 {
return flagsToAPIRepresentation(componentsShow)
}
return flagsToAPIRepresentation(flags)
}
// GetFixedProvidersFlag
/*
@brief:
GetFixedProvidersFlag returns a parsed workspace with the providers inserted under the fixed-providers flag
@params:
cmd - cobra.Command - the Command to look for flags under
@returns:
workspace.Workspace - the workspace which holds all the providers found
*/
func GetFixedProvidersFlag(cmd cobra.Command) workspace.Workspace {
var ws workspace.Workspace
ws.Providers = make(map[string]*version.Version)
fp, err := cmd.Flags().GetStringSlice("fixed-providers")
if err != nil {
_, _ = commons.RED.Println(emoji.CrossMark, " Couldn't parse the received list of providers")
os.Exit(1)
}
for _, p := range fp {
if strings.Contains(p, ":") {
providerValue := strings.Split(p, ":")
if len(providerValue) > 1 {
if validate.IsValid("v" + providerValue[1]) {
v, err := version.NewVersion(providerValue[1])
if err != nil {
if providerValue[1] != "" {
_, _ = commons.RED.Println(emoji.CrossMark, " The received "+providerValue[0]+" version is malformed: "+providerValue[1])
} else {
_, _ = commons.RED.Println(emoji.CrossMark, " Didn't receive the provider version of "+providerValue[0])
}
os.Exit(1)
}
ws.Providers[providerValue[0]] = v
} else {
_, _ = commons.RED.Println(emoji.CrossMark, " The received "+providerValue[0]+" provider version is malformed: "+providerValue[1])
os.Exit(1)
}
} else {
_, _ = commons.RED.Println(emoji.CrossMark, " Provider:Version format received is malformed: "+p)
os.Exit(1)
}
} else {
_, _ = commons.RED.Println(emoji.CrossMark, " Provider format received is malformed: "+p)
os.Exit(1)
}
}
return ws
}
// GetTable
/*
@brief:
GetTable returns a new initialized table
@params:
headers - []string - the list of headers to append to the new table
@returns:
*tablewriter.Table - the new table
*/
func GetTable(headers []string) *tablewriter.Table {
var headerColors []tablewriter.Colors
columnAlignment := []int{0}
columnColors := []tablewriter.Colors{{tablewriter.Bold, tablewriter.FgGreenColor}}
table := tablewriter.NewWriter(os.Stdout) // init table
table.SetHeader(headers) // add headers
// set colors
for i := 0; i < len(headers); i++ {
headerColors = append(headerColors, tablewriter.Colors{tablewriter.Bold, tablewriter.BgMagentaColor})
columnColors = append(columnColors, tablewriter.Colors{tablewriter.Bold, tablewriter.FgYellowColor})
}
columnColors = columnColors[:len(columnColors)-1] // trim last element as the first one is set before the iteration
table.SetHeaderColor(headerColors...)
table.SetColumnColor(columnColors...)
// set alignment
for i := 0; i < len(headers)-1; i++ {
columnAlignment = append(columnAlignment, 1)
}
table.SetColumnAlignment(columnAlignment)
return table
}