diff --git a/README.md b/README.md index a981518..0cc6294 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,23 @@ https://github.com/winebarrel/rdsauth/releases/latest ## Usage +``` +Usage: rdsauth [flags] + +Arguments: + Database URL + +Flags: + -h, --help Show help. + -e, --export Output as environment variable. + --version +``` + ### PostgreSQL ```sh $ MY_DB_HOST=database-1.cluster-abcdef012345.us-east-1.rds.amazonaws.com -$ export PGPASSWORD=$(rdsauth postgres://scott@$MY_DB_HOST) +$ $(rdsauth postgres://scott@$MY_DB_HOST -e) $ psql -h $MY_DB_HOST -U scott ... postgres=> @@ -24,7 +36,7 @@ postgres=> ```sh $ MY_DB_HOST=database-1.cluster-abcdef012345.us-east-1.rds.amazonaws.com -$ export MYSQL_PWD=$(rdsauth mysql://scott@$MY_DB_HOST) +$ $(rdsauth -e mysql://scott@$MY_DB_HOST) $ psql -h $MY_DB_HOST -u scott --enable-cleartext-plugin ... mysql> @@ -36,7 +48,7 @@ mysql> $ dig +short cname my-db.example.com database-1.cluster-abcdef012345.us-east-1.rds.amazonaws.com -$ export PGPASSWORD=$(rdsauth postgres://scott@my-db.example.com) +$ $(rdsauth -e postgres://scott@my-db.example.com) $ psql -h my-db.example.com -U scott ... postgres=> diff --git a/cmd/rdsauth/main.go b/cmd/rdsauth/main.go index 407c43c..92364db 100644 --- a/cmd/rdsauth/main.go +++ b/cmd/rdsauth/main.go @@ -16,26 +16,43 @@ func init() { log.SetFlags(0) } -var cli struct { - URL *url.URL `kong:"arg='',required,help='Database URL'"` - Version kong.VersionFlag +type Options struct { + URL *url.URL `kong:"arg='',required,help='Database URL'"` + Export bool `kong:"short='e',help='Output as environment variable.'"` } -func parseArgs() *url.URL { +func parseArgs() *Options { + var cli struct { + Options + Version kong.VersionFlag + } + parser := kong.Must(&cli, kong.Vars{"version": version}) parser.Model.HelpFlag.Help = "Show help." _, err := parser.Parse(os.Args[1:]) parser.FatalIfErrorf(err) - return cli.URL + + return &cli.Options } func main() { - url := parseArgs() - token, err := rdsauth.GetToken(url) + options := parseArgs() + token, err := rdsauth.GetToken(options.URL) if err != nil { log.Fatal(err) } - fmt.Println(token) + if options.Export { + switch options.URL.Scheme { + case "mysql": + fmt.Printf("export MYSQL_PWD='%s'\n", token) + case "postgres", "postgresql": + fmt.Printf("export PGPASSWORD='%s'\n", token) + default: + log.Fatalf("unimplemented database: %s", options.URL.Scheme) + } + } else { + fmt.Println(token) + } }