Skip to content

Commit cbb5321

Browse files
committed
feat(cmd): add base comamnds
0 parents  commit cbb5321

File tree

10 files changed

+901
-0
lines changed

10 files changed

+901
-0
lines changed

LICENSE

Whitespace-only changes.

cmd/init.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright © 2022 James Ray [email protected]
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"log"
9+
"os"
10+
"path/filepath"
11+
12+
"github.com/spf13/cobra"
13+
"github.com/spf13/viper"
14+
)
15+
16+
// initCmd represents the init command
17+
var initCmd = &cobra.Command{
18+
Use: "init",
19+
Short: "Setup toolsium",
20+
Run: func(cmd *cobra.Command, args []string) {
21+
home, err := os.UserHomeDir()
22+
cobra.CheckErr(err)
23+
if err := viper.WriteConfigAs(filepath.Join(home, cfgFileName)); err != nil {
24+
log.Fatal(err)
25+
}
26+
},
27+
}
28+
29+
func init() {
30+
rootCmd.AddCommand(initCmd)
31+
32+
initCmd.Flags().StringP("mfa_serial", "m", "", "The MFA serial for your account")
33+
viper.BindPFlag("mfa_serial", initCmd.Flags().Lookup("mfa_serial"))
34+
viper.SetDefault("mfa_serial", "")
35+
36+
initCmd.Flags().StringP("department", "d", "", "Department filter")
37+
viper.BindPFlag("department", initCmd.Flags().Lookup("department"))
38+
viper.SetDefault("department", "")
39+
}

cmd/manage.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright © 2022 James Ray [email protected]
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
// manageCmd represents the manage command
14+
var manageCmd = &cobra.Command{
15+
Use: "manage",
16+
Short: "A brief description of your command",
17+
Long: `A longer description that spans multiple lines and likely contains examples
18+
and usage of using your command. For example:
19+
20+
Cobra is a CLI library for Go that empowers applications.
21+
This application is a tool to generate the needed files
22+
to quickly create a Cobra application.`,
23+
Run: func(cmd *cobra.Command, args []string) {
24+
fmt.Println("manage called")
25+
},
26+
}
27+
28+
func init() {
29+
rootCmd.AddCommand(manageCmd)
30+
31+
// Here you will define your flags and configuration settings.
32+
33+
// Cobra supports Persistent Flags which will work for this command
34+
// and all subcommands, e.g.:
35+
// manageCmd.PersistentFlags().String("foo", "", "A help for foo")
36+
37+
// Cobra supports local flags which will only run when this command
38+
// is called directly, e.g.:
39+
// manageCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
40+
}

cmd/mfa.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright © 2022 James Ray [email protected]
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"context"
9+
"errors"
10+
"log"
11+
12+
"github.com/aws/aws-sdk-go-v2/aws"
13+
"github.com/aws/aws-sdk-go-v2/config"
14+
"github.com/aws/aws-sdk-go-v2/service/sts"
15+
"github.com/spf13/cobra"
16+
"github.com/spf13/viper"
17+
)
18+
19+
// mfaCmd represents the mfa command
20+
var mfaCmd = &cobra.Command{
21+
Use: "mfa [token]",
22+
Short: "A brief description of your command",
23+
Long: `A longer description that spans multiple lines and likely contains examples
24+
and usage of using your command. For example:
25+
26+
Cobra is a CLI library for Go that empowers applications.
27+
This application is a tool to generate the needed files
28+
to quickly create a Cobra application.`,
29+
Args: func(cmd *cobra.Command, args []string) error {
30+
if len(args) < 1 {
31+
return errors.New("requires mfa token")
32+
}
33+
if len(args[0]) < 6 || len(args[0]) > 6 {
34+
return errors.New("invalid token")
35+
}
36+
return nil
37+
},
38+
Run: func(cmd *cobra.Command, args []string) {
39+
cfg, err := config.LoadDefaultConfig(context.TODO())
40+
if err != nil {
41+
log.Fatal(err)
42+
}
43+
44+
client := sts.NewFromConfig(cfg)
45+
sessionTokenInput := sts.GetSessionTokenInput{
46+
SerialNumber: aws.String(viper.GetString("mfa_serial")),
47+
TokenCode: aws.String(args[0]),
48+
}
49+
tokenOutput, err := client.GetSessionToken(context.TODO(), &sessionTokenInput)
50+
if err != nil {
51+
log.Fatal(err)
52+
}
53+
viper.Set("session", tokenOutput)
54+
if err := viper.WriteConfig(); err != nil {
55+
log.Fatal(err)
56+
}
57+
},
58+
}
59+
60+
func init() {
61+
rootCmd.AddCommand(mfaCmd)
62+
}

cmd/papi.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright © 2022 James Ray [email protected]
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"log"
11+
12+
"github.com/aws/aws-sdk-go-v2/aws"
13+
"github.com/aws/aws-sdk-go-v2/config"
14+
"github.com/aws/aws-sdk-go-v2/service/ec2"
15+
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
16+
"github.com/rayprogramming/toolsium/lib"
17+
"github.com/spf13/cobra"
18+
)
19+
20+
// papiCmd represents the papi command
21+
var papiCmd = &cobra.Command{
22+
Use: "papi",
23+
Short: "A brief description of your command",
24+
Long: `A longer description that spans multiple lines and likely contains examples
25+
and usage of using your command. For example:
26+
27+
Cobra is a CLI library for Go that empowers applications.
28+
This application is a tool to generate the needed files
29+
to quickly create a Cobra application.`,
30+
Run: func(cmd *cobra.Command, args []string) {
31+
cfg, err := config.LoadDefaultConfig(context.TODO())
32+
cfg.Region = "us-east-1"
33+
cfg.Credentials = aws.NewCredentialsCache(&lib.MfaProvider{})
34+
35+
if err != nil {
36+
log.Fatal(err)
37+
}
38+
39+
client := ec2.NewFromConfig(cfg)
40+
params := &ec2.DescribeInstancesInput{
41+
Filters: []types.Filter{{
42+
Name: aws.String("tag:Environment"),
43+
Values: []string{"beta"},
44+
}},
45+
}
46+
result, err := client.DescribeInstances(context.TODO(), params)
47+
48+
if err != nil {
49+
fmt.Println("Error calling ec2: ", err)
50+
return
51+
}
52+
count := len(result.Reservations)
53+
fmt.Println("Instances: ", count)
54+
for i, reservation := range result.Reservations {
55+
for k, instance := range reservation.Instances {
56+
fmt.Printf("Instance number: %v - %v Id: %v \n", i, k, aws.ToString(instance.InstanceId))
57+
}
58+
}
59+
},
60+
}
61+
62+
func init() {
63+
manageCmd.AddCommand(papiCmd)
64+
65+
// Here you will define your flags and configuration settings.
66+
67+
// Cobra supports Persistent Flags which will work for this command
68+
// and all subcommands, e.g.:
69+
// papiCmd.PersistentFlags().String("foo", "", "A help for foo")
70+
71+
// Cobra supports local flags which will only run when this command
72+
// is called directly, e.g.:
73+
// papiCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
74+
}

cmd/root.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Copyright © 2022 James Ray [email protected]
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"fmt"
9+
"os"
10+
11+
"github.com/spf13/cobra"
12+
"github.com/spf13/viper"
13+
)
14+
15+
const (
16+
cfgFileName = ".toolsium"
17+
cfgFileType = "json"
18+
)
19+
20+
var (
21+
cfgFile string
22+
)
23+
24+
// rootCmd represents the base command when called without any subcommands
25+
var rootCmd = &cobra.Command{
26+
Use: "toolsium",
27+
Short: "A brief description of your application",
28+
Long: `A longer description that spans multiple lines and likely contains
29+
examples and usage of using your application. For example:
30+
31+
Cobra is a CLI library for Go that empowers applications.
32+
This application is a tool to generate the needed files
33+
to quickly create a Cobra application.`,
34+
// Uncomment the following line if your bare application
35+
// has an action associated with it:
36+
// Run: func(cmd *cobra.Command, args []string) {
37+
// cfg, err := config.LoadDefaultConfig(context.TODO())
38+
// cfg.Credentials = aws.NewCredentialsCache(&lib.MfaProvider{})
39+
40+
// if err != nil {
41+
// log.Fatal(err)
42+
// }
43+
44+
// client := s3.NewFromConfig(cfg)
45+
// output, err := client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
46+
// if err != nil {
47+
// log.Fatal(err)
48+
// }
49+
50+
// for _, object := range output.Buckets {
51+
// log.Printf("Bucket=%s", aws.ToString(object.Name))
52+
// }
53+
// },
54+
}
55+
56+
// Execute adds all child commands to the root command and sets flags appropriately.
57+
// This is called by main.main(). It only needs to happen once to the rootCmd.
58+
func Execute() {
59+
err := rootCmd.Execute()
60+
if err != nil {
61+
os.Exit(1)
62+
}
63+
}
64+
65+
func init() {
66+
cobra.OnInitialize(initConfig)
67+
68+
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", fmt.Sprintf("config file (default is $HOME/%v.%v)", cfgFileName, cfgFileType))
69+
// Instead of profiles for now, I recommend just passing in different config files.
70+
}
71+
72+
// initConfig reads in config file and ENV variables if set.
73+
func initConfig() {
74+
if cfgFile != "" {
75+
// Use config file from the flag.
76+
viper.SetConfigFile(cfgFile)
77+
} else {
78+
// Find home directory.
79+
home, err := os.UserHomeDir()
80+
cobra.CheckErr(err)
81+
82+
// Search config in home directory with name ".toolsium" (without extension).
83+
viper.AddConfigPath(home)
84+
viper.SetConfigType(cfgFileType)
85+
viper.SetConfigName(cfgFileName)
86+
}
87+
88+
viper.AutomaticEnv() // read in environment variables that match
89+
90+
// If a config file is found, read it in.
91+
if err := viper.ReadInConfig(); err == nil {
92+
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
93+
}
94+
}

go.mod

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module github.com/rayprogramming/toolsium
2+
3+
go 1.18
4+
5+
require (
6+
github.com/aws/aws-sdk-go-v2 v1.16.11
7+
github.com/aws/aws-sdk-go-v2/config v1.17.1
8+
github.com/aws/aws-sdk-go-v2/service/sts v1.16.13
9+
github.com/spf13/cobra v1.5.0
10+
github.com/spf13/viper v1.12.0
11+
)
12+
13+
require github.com/jmespath/go-jmespath v0.4.0 // indirect
14+
15+
require (
16+
github.com/aws/aws-sdk-go-v2/credentials v1.12.14 // indirect
17+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.12 // indirect
18+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.18 // indirect
19+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.12 // indirect
20+
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.19 // indirect
21+
github.com/aws/aws-sdk-go-v2/service/ec2 v1.54.0
22+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.12 // indirect
23+
github.com/aws/aws-sdk-go-v2/service/sso v1.11.17 // indirect
24+
github.com/aws/smithy-go v1.12.1 // indirect
25+
github.com/fsnotify/fsnotify v1.5.4 // indirect
26+
github.com/hashicorp/hcl v1.0.0 // indirect
27+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
28+
github.com/magiconair/properties v1.8.6 // indirect
29+
github.com/mitchellh/mapstructure v1.5.0 // indirect
30+
github.com/pelletier/go-toml v1.9.5 // indirect
31+
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
32+
github.com/spf13/afero v1.8.2 // indirect
33+
github.com/spf13/cast v1.5.0 // indirect
34+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
35+
github.com/spf13/pflag v1.0.5 // indirect
36+
github.com/subosito/gotenv v1.3.0 // indirect
37+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
38+
golang.org/x/text v0.3.7 // indirect
39+
gopkg.in/ini.v1 v1.66.4 // indirect
40+
gopkg.in/yaml.v2 v2.4.0 // indirect
41+
gopkg.in/yaml.v3 v3.0.0 // indirect
42+
)

0 commit comments

Comments
 (0)