Skip to content

Commit

Permalink
Add "-e" option
Browse files Browse the repository at this point in the history
  • Loading branch information
winebarrel committed Dec 4, 2024
1 parent d6e62c3 commit 82b9cea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,23 @@ https://github.com/winebarrel/rdsauth/releases/latest

## Usage

```
Usage: rdsauth <url> [flags]
Arguments:
<url> 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=>
Expand All @@ -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>
Expand All @@ -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://[email protected])
$ $(rdsauth -e postgres://[email protected])
$ psql -h my-db.example.com -U scott
...
postgres=>
Expand Down
33 changes: 25 additions & 8 deletions cmd/rdsauth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 82b9cea

Please sign in to comment.