This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
/
aklogin.go
220 lines (190 loc) · 4.64 KB
/
aklogin.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package aklogin
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"code.cloudfoundry.org/cli/cf/flags"
"code.cloudfoundry.org/cli/plugin"
"github.com/olebedev/config"
)
const defaultYML = "~/.cflogin.yml"
// CFPlugin is the runnable plugin for `cf`
type CFPlugin struct{}
// Profile matches a YML profile
type Profile struct {
Target, Username, Password, Org, Space string
SSO bool
}
// GetMetadata returns the plugin's version
// and the min `cf-cli` version
func (ak *CFPlugin) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "aklogin",
Version: plugin.VersionType{
Major: 1,
Minor: 3,
Build: 0,
},
MinCliVersion: plugin.VersionType{
Major: 6,
Minor: 7,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "aklogin",
HelpText: "CF login via profiles",
UsageDetails: plugin.Usage{
Usage: "cf aklogin [options] <profile>",
Options: map[string]string{
"-filename": "YML config file path",
"-list": "List available profiles",
"-version": "Print version",
},
},
},
},
}
}
// Run is the function invoked by the `cf-cli`
func (ak *CFPlugin) Run(cliConnection plugin.CliConnection, args []string) {
switch args[0] {
case "aklogin":
fc, err := parseArguments(args)
if check(err) {
return
}
if fc.IsSet("version") {
fmt.Printf("%d.%d.%d\n",
ak.GetMetadata().Version.Major,
ak.GetMetadata().Version.Minor,
ak.GetMetadata().Version.Build)
return
}
yml, err := globalYML(fc.String("filename"))
if check(err) {
return
}
var profile string
if len(fc.Args()) > 1 {
profile = fc.Args()[1]
}
if fc.IsSet("list") {
profilesMap, _ := yml.Map("")
delete(profilesMap, "include")
var inputProfile int
profiles := make([]string, inputProfile, len(profilesMap))
for p := range profilesMap {
profiles = append(profiles, p)
}
sort.Strings(profiles)
fmt.Println("Available profiles:")
for i, p := range profiles {
fmt.Printf("%d. %s\n", i, p)
}
fmt.Printf("Select profile: ")
n, _ := fmt.Scanf("%d", &inputProfile)
if n == 0 { // hack for testing user input
inputProfile = n // auto-select 1st profile
}
if inputProfile >= len(profiles) {
fmt.Println("Invalid profile.")
return
}
profile = profiles[inputProfile]
}
if profile == "" {
fmt.Println("Please specify a profile.")
return
}
fmt.Printf("Using profile: '%s'\n", profile)
activeProfile, err := yml.Get(profile)
if err != nil {
fmt.Println("Profile not found.")
return
}
target, err := activeProfile.String("target")
if check(err) {
return
}
sso, _ := activeProfile.Bool("sso")
var username, password string
if !sso {
username, err = activeProfile.String("username")
if check(err) {
return
}
// optional
password, _ = activeProfile.String("password")
}
org, _ := activeProfile.String("org")
space, _ := activeProfile.String("space")
p := &Profile{Target: target, Username: username, Password: password, Org: org, Space: space, SSO: sso}
err = login(cliConnection, p)
check(err)
case "CLI-MESSAGE-UNINSTALL":
fmt.Println("Thanks for using the aklogin plugin.")
}
}
func globalYML(filename string) (*config.Config, error) {
yml, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
cfg, err := config.ParseYamlBytes(yml)
if err != nil {
return nil, err
}
include, err := cfg.Get("include")
if err == nil {
includes, _ := include.List("")
for _, path := range includes {
iyml, err := ioutil.ReadFile(expandTilde(path.(string)))
if err != nil {
return nil, err
}
yml = append(append(yml, 0x0a), iyml...) // 0x0a == "\n"
}
}
return config.ParseYamlBytes(yml)
}
func login(cliConn plugin.CliConnection, p *Profile) error {
var err error
if p.SSO {
_, err = cliConn.CliCommand("login",
"-a", p.Target,
"--sso",
"-o", p.Org,
"-s", p.Space)
} else {
_, err = cliConn.CliCommand("login",
"-a", p.Target,
"-u", p.Username,
"-p", p.Password,
"-o", p.Org,
"-s", p.Space)
}
return err
}
func parseArguments(args []string) (flags.FlagContext, error) {
fc := flags.New()
fc.NewStringFlagWithDefault("filename", "f", "YML config file path", expandTilde(defaultYML))
fc.NewBoolFlag("list", "l", "List available profiles")
fc.NewBoolFlag("version", "v", "Print version")
return fc, fc.Parse(args...)
}
func expandTilde(filename string) string {
if filename[:2] == "~/" {
filename = filepath.Join(os.Getenv("HOME"), filename[2:])
}
return filename
}
func check(err error) (ok bool) {
if err != nil {
fmt.Println(err)
return true
}
return
}