Skip to content

Commit

Permalink
Add configure command
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Jan 3, 2022
1 parent 5cd117b commit 1bf4a4d
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
73 changes: 73 additions & 0 deletions awscmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package awscmd

import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/mitchellh/go-homedir"
"gopkg.in/ini.v1"
)

type InputConfigure struct {
Region string
Profile string
AccessKey string
SecretKey string
}

type OutputConfigure struct{}

func Configure(ctx context.Context, input *InputConfigure) (*OutputConfigure, error) {
path, err := homedir.Expand("~/.aws")
if err != nil {
return nil, fmt.Errorf("Failed to expand path: %v", err)
}
err = os.Mkdir(path, 0755)
if err != nil && !os.IsExist(err) {
return nil, fmt.Errorf("Failed to create .aws directory: %v", err)
}

configPath := filepath.Join(path, "config")
credentialsPath := filepath.Join(path, "credentials")

config, err := iniLoadOrCreate(configPath)
if err != nil {
return nil, fmt.Errorf("Fail to read config file: %v", err)
}
config.Section(fmt.Sprint("profile", " ", input.Profile)).NewKey("region", input.Region)
err = config.SaveTo(configPath)
if err != nil {
return nil, fmt.Errorf("Fail to save config file: %v", err)
}

credentials, err := iniLoadOrCreate(credentialsPath)
if err != nil {
return nil, fmt.Errorf("Fail to read credentials file: %v", err)
}
sec := credentials.Section(input.Profile)
sec.NewKey("aws_access_key_id", input.AccessKey)
sec.NewKey("aws_secret_access_key", input.SecretKey)

err = credentials.SaveTo(credentialsPath)
if err != nil {
return nil, fmt.Errorf("Fail to save config file: %v", err)
}

return &OutputConfigure{}, nil
}

func iniLoadOrCreate(path string) (*ini.File, error) {
_, err := os.Stat(path)
if err == nil {
return ini.Load(path)
}

_, err = os.Create(path)
if err != nil {
return nil, fmt.Errorf("Failed to create file %s", path)
}

return ini.Load(path)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.13

require (
github.com/aws/aws-sdk-go v1.42.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/urfave/cli/v2 v2.3.0
github.com/wzshiming/ctc v1.2.3 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -30,5 +32,7 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI=
gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ func main() {
Name: "aws",
Usage: "AWS cloud commands",
Subcommands: []*cli.Command{
{
Name: "configure",
Usage: "Configure AWS profile",
Flags: []cli.Flag{
&cli.StringFlag{Name: "region", Usage: "AWS region", Required: true},
&cli.StringFlag{Name: "profile", Usage: "AWS profile", Required: true},
&cli.StringFlag{Name: "key", Usage: "Access key ID", Required: true},
&cli.StringFlag{Name: "secret", Usage: "Secret access key", Required: true},
},
Action: func(c *cli.Context) error {
input := &awscmd.InputConfigure{
Region: c.String("region"),
Profile: c.String("profile"),
AccessKey: c.String("key"),
SecretKey: c.String("secret"),
}
_, err := awscmd.Configure(context.TODO(), input)
if err != nil {
return err
}

return nil
},
},
{
Name: "secrets",
Usage: "Secrets manager",
Expand Down

0 comments on commit 1bf4a4d

Please sign in to comment.