This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
forked from sky-uk/osprey
/
user.go
74 lines (61 loc) · 1.96 KB
/
user.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
package cmd
import (
"github.com/sky-uk/osprey/client"
"github.com/sky-uk/osprey/client/kubeconfig"
"github.com/spf13/cobra"
"os"
log "github.com/sirupsen/logrus"
)
var userCmd = &cobra.Command{
Use: "user",
Short: "User commands for osprey.",
Long: "Returns the details of the current user for each of the configured targets.",
PersistentPreRun: checkClientParams,
Run: user,
}
var (
ospreyconfigFile string
targetGroup string
)
func init() {
RootCmd.AddCommand(userCmd)
persistentFlags := userCmd.PersistentFlags()
persistentFlags.StringVarP(&ospreyconfigFile, "ospreyconfig", "o", "", "osprey targets configuration. Defaults to $HOME/.osprey/config")
persistentFlags.StringVarP(&targetGroup, "group", "g", "", "name of the group to log in to.")
}
func user(_ *cobra.Command, _ []string) {
ospreyconfig, err := client.LoadConfig(ospreyconfigFile)
if err != nil {
log.Fatalf("Failed to load ospreyconfig file %s: %v", ospreyconfigFile, err)
}
err = kubeconfig.LoadConfig(ospreyconfig.Kubeconfig)
if err != nil {
log.Fatalf("Failed to initialise kubeconfig: %v", err)
}
groupName := ospreyconfig.GroupOrDefault(targetGroup)
snapshot := client.GetSnapshot(ospreyconfig)
group, ok := snapshot.GetGroup(groupName)
if !ok {
log.Errorf("Group not found: %q", groupName)
os.Exit(1)
}
displayActiveGroup(targetGroup, ospreyconfig.DefaultGroup)
success := true
for _, target := range group.Targets() {
userData, err := kubeconfig.GetUser(target.Name())
if err != nil {
log.Errorf("Failed to retrieve user for %s from kubeconfig: %v", target.Name(), err)
success = false
}
log.Infof("%s: %s", target.Name(), userData)
}
if !success {
log.Fatal("Failed to update credentials for some targets.")
}
}
func checkClientParams(_ *cobra.Command, _ []string) {
if ospreyconfigFile == "" {
ospreyconfigFile = client.RecommendedOspreyConfigFile
}
checkFile(ospreyconfigFile, "ospreyconfig")
}