This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
forked from sky-uk/osprey
/
targets.go
121 lines (107 loc) · 3.09 KB
/
targets.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
package cmd
import (
"github.com/sky-uk/osprey/client"
"github.com/spf13/cobra"
"fmt"
"os"
"strings"
log "github.com/sirupsen/logrus"
)
var targetsCommand = &cobra.Command{
Use: "targets",
Short: "ConfigSnapshot commands for osprey.",
Long: "Returns the list of targets sorted alphabetically.",
PersistentPreRun: checkClientParams,
Run: targets,
}
var (
byGroups bool
listGroups bool
)
func init() {
configCmd.AddCommand(targetsCommand)
flags := targetsCommand.Flags()
flags.BoolVarP(&byGroups, "by-groups", "b", false, "list targets by group")
flags.BoolVarP(&listGroups, "list-groups", "l", false, "list groups only")
}
func targets(_ *cobra.Command, _ []string) {
ospreyconfig, err := client.LoadConfig(ospreyconfigFile)
if err != nil {
log.Fatalf("Failed to load ospreyconfig file %s: %v", ospreyconfigFile, err)
}
snapshot := client.GetSnapshot(ospreyconfig)
var outputLines []string
if listGroups {
outputLines = append(outputLines, "Osprey groups:")
outputLines = append(outputLines, displayGroups(snapshot, false)...)
} else {
outputLines = append(outputLines, "Osprey targets:")
if byGroups {
outputLines = append(outputLines, displayGroups(snapshot, true)...)
} else {
outputLines = append(outputLines, displayTargets(snapshot)...)
}
}
fmt.Println(strings.Join(outputLines, "\n"))
}
func displayGroups(snapshot client.ConfigSnapshot, listTargets bool) []string {
var outputLines []string
var groups []client.Group
if targetGroup == "" {
if ungrouped, ok := snapshot.GetGroup(targetGroup); ok {
outputLines = displayGroup(ungrouped, listTargets)
}
groups = snapshot.Groups()
} else {
group, ok := snapshot.GetGroup(targetGroup)
if !ok {
log.Errorf("Group not found: %q", targetGroup)
os.Exit(1)
}
log.Infof("Active group: %s", group.Name())
groups = []client.Group{group}
}
for _, group := range groups {
outputLines = append(outputLines, displayGroup(group, listTargets)...)
}
return outputLines
}
func displayGroup(group client.Group, listTargets bool) []string {
var outputLines []string
highlight := " "
if group.IsDefault() {
highlight = "*"
}
name := group.Name()
if name == "" {
name = "<ungrouped>"
}
outputLines = append(outputLines, fmt.Sprintf("%s %s", highlight, name))
if listTargets {
for _, target := range group.Targets() {
aliases := ""
if target.HasAliases() {
aliases = fmt.Sprintf(" | %s", strings.Join(target.Aliases(), " | "))
}
outputLines = append(outputLines, fmt.Sprintf(" %s%s", target.Name(), aliases))
}
}
return outputLines
}
func displayTargets(snapshot client.ConfigSnapshot) []string {
allTargets := snapshot.Targets()
defaultGroup := snapshot.DefaultGroup()
var outputLines []string
for _, target := range allTargets {
highlight := " "
if defaultGroup.Contains(target) {
highlight = "*"
}
aliases := ""
if target.HasAliases() {
aliases = fmt.Sprintf(" | %s", strings.Join(target.Aliases(), " | "))
}
outputLines = append(outputLines, fmt.Sprintf("%s %s%s", highlight, target.Name(), aliases))
}
return outputLines
}