This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
/
adm.go
141 lines (121 loc) · 3.45 KB
/
adm.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
package cmd
import (
"fmt"
"os"
"github.com/skatteetaten/ao/pkg/config"
"github.com/spf13/cobra"
)
var flagShowAll bool
var admCmd = &cobra.Command{
Use: "adm",
Short: "Perform administrative commands on AO configuration",
}
var getClusterCmd = &cobra.Command{
Use: "clusters",
Short: "List configured clusters",
Aliases: []string{"cluster"},
Run: PrintClusters,
}
var getAffiliationCmd = &cobra.Command{
Use: "affiliations",
Short: "List defined affiliations",
Aliases: []string{"affiliation"},
Run: PrintAffiliations,
}
var updateClustersCmd = &cobra.Command{
Use: "update-clusters",
Short: "Will recreate clusters in config file",
RunE: UpdateClusters,
}
var recreateConfigCmd = &cobra.Command{
Use: "recreate-config",
Short: `The command will recreate the .ao.json file.`,
RunE: RecreateConfig,
}
var completionCmd = &cobra.Command{
Use: "completion",
Short: "Generates bash completion file",
Long: `This command generates a bash script file that provides bash completion.
Bash completion allows you to press the Tab key to complete keywords.
After running this command, a file called ao.sh will exist in your home directory.
By typing
source ./ao.sh
you will have bash completion in ao
To persist this across login sessions, please update your .bashrc file.`,
RunE: BashCompletion,
}
func init() {
RootCmd.AddCommand(admCmd)
admCmd.AddCommand(getClusterCmd)
admCmd.AddCommand(getAffiliationCmd)
admCmd.AddCommand(completionCmd)
admCmd.AddCommand(recreateConfigCmd)
admCmd.AddCommand(updateClustersCmd)
getClusterCmd.Flags().BoolVarP(&flagShowAll, "all", "a", false, "Show all clusters, not just the reachable ones")
recreateConfigCmd.Flags().StringVarP(&flagCluster, "cluster", "c", "", "Recreate config with one cluster")
}
func PrintClusters(cmd *cobra.Command, args []string) {
var rows []string
for _, name := range AO.AvailableClusters {
cluster := AO.Clusters[name]
if !(cluster.Reachable || flagShowAll) {
continue
}
reachable := ""
if cluster.Reachable {
reachable = "Yes"
}
loggedIn := ""
if cluster.HasValidToken() {
loggedIn = "Yes"
}
api := ""
if name == AO.APICluster {
api = "Yes"
}
line := fmt.Sprintf("\t%s\t%s\t%s\t%s\t%s", name, reachable, loggedIn, api, cluster.Url)
rows = append(rows, line)
}
header := "\tCLUSTER NAME\tREACHABLE\tLOGGED IN\tAPI\tURL"
DefaultTablePrinter(header, rows, cmd.OutOrStdout())
}
func PrintAffiliations(cmd *cobra.Command, args []string) {
acn, err := DefaultApiClient.GetAuroraConfigNames()
if err != nil {
return
}
var mark string
for _, affiliation := range *acn {
if affiliation == AO.Affiliation {
mark = "*"
} else {
mark = " "
}
line := fmt.Sprintf(" %s %s", mark, affiliation)
cmd.Println(line)
}
}
func UpdateClusters(cmd *cobra.Command, args []string) error {
AO.InitClusters()
AO.SelectApiCluster()
return config.WriteConfig(*AO, ConfigLocation)
}
func RecreateConfig(cmd *cobra.Command, args []string) error {
conf := &config.DefaultAOConfig
if flagCluster != "" {
conf.AvailableClusters = []string{flagCluster}
conf.PreferredAPIClusters = []string{flagCluster}
}
conf.InitClusters()
conf.SelectApiCluster()
return config.WriteConfig(*conf, ConfigLocation)
}
func BashCompletion(cmd *cobra.Command, args []string) error {
err := RootCmd.GenBashCompletionFile("ao.sh")
if err != nil {
return err
}
wd, _ := os.Getwd()
fmt.Println("Bash completion file created at", wd+"/ao.sh")
return nil
}