Skip to content

Commit fdd3185

Browse files
authored
Merge pull request #1 from ardikabs/use-envdecode-pkg
Switch from envconfig to envdecode pkg
2 parents 33961b2 + c026694 commit fdd3185

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

config/config.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@ package config
33
import (
44
"log"
55

6-
"github.com/kelseyhightower/envconfig"
6+
"github.com/joeshaw/envdecode"
77
)
88

99
// Config represent the application configuration
1010
type Config struct {
11-
Debug bool `envconfig:"debug" default:"0"`
12-
Tags string `envconfig:"tags" default:"Name=*"`
13-
SSHUsername string `envconfig:"ssh_username" default:"ec2-user"`
14-
SSHPort string `envconfig:"ssh_port" default:"22"`
15-
SSHOpts string `envconfig:"ssh_opts" default:"-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=5"`
11+
Debug bool `env:"AWSSH_DEBUG,default=0"`
12+
Tags string `env:"AWSS_TAGS,default=Name=*"`
13+
SSHUsername string `env:"AWSSH_SSH_USERNAME,default=ec2-user"`
14+
SSHPort string `env:"AWSSH_SSH_PORT,default=22"`
15+
SSHOpts string `env:"AWSSH_SSH_OPTS,default=-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=5"`
16+
Region string `env:"AWS_DEFAULT_REGION"`
1617
}
1718

1819
var appConfig Config
1920

2021
// Load used to load the application configuration
2122
func Load() {
22-
if err := envconfig.Process("awssh", &appConfig); err != nil {
23+
24+
if err := envdecode.Decode(&appConfig); err != nil {
2325
log.Fatal("Can't load config: ", err)
2426
}
2527
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.13
44

55
require (
66
github.com/aws/aws-sdk-go v1.33.19
7+
github.com/joeshaw/envdecode v0.0.0-20200121155833-099f1fc765bd
78
github.com/kelseyhightower/envconfig v1.4.0
89
github.com/manifoldco/promptui v0.7.0
910
github.com/morikuni/aec v1.0.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
5353
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
5454
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
5555
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
56+
github.com/joeshaw/envdecode v0.0.0-20200121155833-099f1fc765bd h1:nIzoSW6OhhppWLm4yqBwZsKJlAayUu5FGozhrF3ETSM=
57+
github.com/joeshaw/envdecode v0.0.0-20200121155833-099f1fc765bd/go.mod h1:MEQrHur0g8VplbLOv5vXmDzacSaH9Z7XhcgsSh1xciU=
5658
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
5759
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU=
5860
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU=

internal/cli/root.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
var (
1717
usePublicIP bool
18-
region string
1918
appConfig *config.Config
2019
)
2120

@@ -50,12 +49,12 @@ func MakeRoot() *cobra.Command {
5049
command.Run = runSSHAccess
5150

5251
command.Flags().BoolVarP(&appConfig.Debug, "debug", "d", appConfig.Debug, "Enabled debug mode")
52+
command.Flags().StringVar(&appConfig.Region, "region", appConfig.Region, "Default AWS region to be used. Either set AWS_REGION or AWS_DEFAULT_REGION")
5353
command.Flags().StringVarP(&appConfig.Tags, "tags", "t", appConfig.Tags, "EC2 tags key-value pair")
5454
command.Flags().StringVarP(&appConfig.SSHUsername, "ssh-username", "u", appConfig.SSHUsername, "EC2 SSH username")
5555
command.Flags().StringVarP(&appConfig.SSHPort, "ssh-port", "p", appConfig.SSHPort, "An EC2 instance ssh port")
5656
command.Flags().StringVarP(&appConfig.SSHOpts, "ssh-opts", "o", appConfig.SSHOpts, "An additional ssh options")
5757
command.Flags().BoolVarP(&usePublicIP, "use-public-ip", "", false, "Use public IP to access the EC2 instance")
58-
command.Flags().StringVarP(&region, "region", "", "", "Default AWS region to be used. Either set AWS_REGION or AWS_DEFAULT_REGION")
5958

6059
return command
6160
}
@@ -81,7 +80,7 @@ func runSSHAccess(cmd *cobra.Command, args []string) {
8180

8281
var target *aws.EC2Instance
8382

84-
session := aws.NewSession(region)
83+
session := aws.NewSession(appConfig.Region)
8584

8685
if len(args) > 0 {
8786
instances, err := aws.GetInstanceWithID(session, args[0])

0 commit comments

Comments
 (0)