/
root.go
51 lines (43 loc) · 1.18 KB
/
root.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
package cmd
import (
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/spf13/cobra"
)
var sess *session.Session
var rootCmd = &cobra.Command{
Use: "awsugar",
Short: "AWS Working Sugar",
Long: `AWS Working Sugar provides a set of useful tools for
your day to day AWS duties.`,
Version: "0.0.1",
}
var rootFlags struct {
DryRun bool
Region string
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func initSession() {
sess = session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Config: aws.Config{
Region: aws.String(rootFlags.Region),
},
}))
}
func init() {
cobra.OnInitialize(initSession)
rootCmd.PersistentFlags().BoolVarP(&rootFlags.DryRun, "dry-run", "d", false,
"Toggle a list-only mode without executing any action.")
rootCmd.PersistentFlags().StringVarP(&rootFlags.Region, "region", "r", "us-west-2",
"Choose the region to execute the actions in")
}