/
role_list.go
87 lines (72 loc) · 2.34 KB
/
role_list.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
/*
Copyright © 2023 Julian Easterling <julian@julianscorner.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"os"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
var roleListCmd = &cobra.Command{
Use: "list",
Short: "Show list of Ansible roles in the development environment",
Long: "Show list of Ansible roles in the development environment",
Run: func(cmd *cobra.Command, args []string) {
required, _ := cmd.Flags().GetBool("requirements")
verbose, _ := cmd.Flags().GetBool("verbose")
if required {
ensureRequirementsFile()
list_required_roles()
} else {
list_all_roles(verbose)
}
},
PreRun: func(cmd *cobra.Command, args []string) {
ensureAnsibleDirectory()
},
PostRun: func(cmd *cobra.Command, args []string) {
ensureWorkingDirectoryAndExit()
},
}
func init() {
roleCmd.AddCommand(roleListCmd)
roleListCmd.Flags().BoolP("verbose", "v", false, "tell Ansible to print more debug messages")
roleListCmd.Flags().BoolP("requirements", "r", false, "show only roles from requirements.yml")
}
func list_all_roles(verbose bool) {
param := []string{"role", "list"}
if verbose {
param = []string{"role", "list", "-v"}
}
executeExternalProgram("ansible-galaxy", param...)
}
func list_required_roles() {
requirements, _ := readRequirementsFile()
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Source", "Version"})
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator(" ")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t")
table.SetNoWhiteSpace(true)
for _, r := range requirements.Roles {
row := []string{r.Name, r.Source, r.Version}
table.Append(row)
}
table.Render()
}