-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
101 lines (76 loc) · 2.52 KB
/
config.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
package main
import (
"context"
"errors"
"path/filepath"
"github.com/mitchellh/go-homedir"
logger "github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
var aslPath string
// ConfigOptions defines the ASL options
type ConfigOptions struct {
AccountID string `json:"accountId"`
RoleName string `json:"roleName"`
StartURL string `json:"startUrl"`
Region string `json:"region"`
BackupFile bool `json:"-"`
ForceSSOLogin bool `json:"-"`
}
func configureCmd(ctx context.Context) *cobra.Command {
o := &ConfigOptions{}
cmd := &cobra.Command{
Use: "configure",
Short: "Store the parameters used to log in to AWS SSO",
RunE: func(cmd *cobra.Command, args []string) error {
logger.Debug().Str("aslPath", aslPath).Interface("options", o).Msg("configuring...")
if err := Configure(o); err != nil {
return err
}
logger.Info().Msg("it worked! please run: asl")
return nil
},
}
cmd.Flags().StringVarP(&o.AccountID, "account-id", "a", "", "the AWS account that is assigned to the user")
cmd.Flags().StringVarP(&o.RoleName, "role-name", "R", "", "the role name that is assigned to the user")
cmd.Flags().StringVarP(&o.StartURL, "start-url", "u", "", "the URL that points to the organization's AWS Single Sign-On (AWS SSO) user portal")
cmd.Flags().StringVarP(&o.Region, "region", "r", "", "the region to use")
_ = cmd.MarkFlagRequired("account-id")
_ = cmd.MarkFlagRequired("role-name")
_ = cmd.MarkFlagRequired("start-url")
_ = cmd.MarkFlagRequired("region")
return cmd
}
// Configure writes the ASL parameters to use when needed
func Configure(o *ConfigOptions) error {
config := NewFile(aslPath)
if err := config.WriteJSON(o); err != nil {
return err
}
logger.Debug().Str("path", config.FullName).Msg("the asl config file has been successfully stored")
return nil
}
// LoadConfig reads the ASL parameters
func LoadConfig(opts *Options) (*ConfigOptions, error) {
config := NewFile(aslPath)
if !config.Exists() {
return nil, errors.New("asl config file not found. please run: asl configure")
}
logger.Info().Str("path", config.FullName).Msg("loading the asl config file")
data := &ConfigOptions{}
err := config.ReadJSON(data)
if err != nil {
return nil, err
}
data.BackupFile = opts.Backup
data.ForceSSOLogin = opts.ForceSSOLogin
logger.Debug().Interface("data", data).Msg("the asl config file has been successfully read")
return data, nil
}
func init() {
home, err := homedir.Dir()
if err != nil {
logger.Fatal().Err(err)
}
aslPath = filepath.Join(home, ".asl")
}