Skip to content

Commit

Permalink
refactor flags, config file, and command-line opts to a new pkg: comm…
Browse files Browse the repository at this point in the history
…andoptions
  • Loading branch information
jrperritt committed Jul 23, 2015
1 parent 28e02df commit 63b9785
Show file tree
Hide file tree
Showing 71 changed files with 266 additions and 174 deletions.
26 changes: 11 additions & 15 deletions auth/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"strings"

"github.com/jrperritt/rack/commandoptions"
"github.com/jrperritt/rack/internal/github.com/Sirupsen/logrus"
"github.com/jrperritt/rack/internal/github.com/codegangsta/cli"
"github.com/jrperritt/rack/internal/github.com/rackspace/gophercloud"
Expand Down Expand Up @@ -104,19 +105,14 @@ func authFromScratch(ao gophercloud.AuthOptions, region, serviceType string, log
return sc, nil
}

type authCred struct {
value string
from string
}

// Credentials determines the appropriate authentication method for the user.
// It returns a gophercloud.AuthOptions object and a region.
//
// It will use command-line authentication parameters if available, then it will
// look for any unset parameters in the config file, and then finally in
// environment variables.
func Credentials(c *cli.Context, logger *logrus.Logger) (*gophercloud.AuthOptions, string, error) {
have := make(map[string]authCred)
have := make(map[string]commandoptions.Cred)
need := map[string]string{
"username": "",
"apikey": "",
Expand All @@ -125,11 +121,11 @@ func Credentials(c *cli.Context, logger *logrus.Logger) (*gophercloud.AuthOption
}

// use command-line options if available
cliopts(c, have, need)
commandoptions.CLIopts(c, have, need)
// are there any unset auth variables?
if len(need) != 0 {
// if so, look in config file
err := configfile(c, have, need)
err := commandoptions.ConfigFile(c, have, need)
if err != nil {
return nil, "", err
}
Expand All @@ -141,14 +137,14 @@ func Credentials(c *cli.Context, logger *logrus.Logger) (*gophercloud.AuthOption
}

// if the user didn't provide an auth URL, default to the Rackspace US endpoint
if _, ok := have["authurl"]; !ok || have["authurl"].value == "" {
have["authurl"] = authCred{value: rackspace.RackspaceUSIdentity, from: "default value"}
if _, ok := have["authurl"]; !ok || have["authurl"].Value == "" {
have["authurl"] = commandoptions.Cred{Value: rackspace.RackspaceUSIdentity, From: "default value"}
delete(need, "authurl")
}

haveString := ""
for k, v := range have {
haveString += fmt.Sprintf("%s: %s (from %s)\n", k, v.value, v.from)
haveString += fmt.Sprintf("%s: %s (from %s)\n", k, v.Value, v.From)
}

if len(need) > 0 {
Expand Down Expand Up @@ -178,13 +174,13 @@ func Credentials(c *cli.Context, logger *logrus.Logger) (*gophercloud.AuthOption
}

ao := &gophercloud.AuthOptions{
Username: have["username"].value,
APIKey: have["apikey"].value,
IdentityEndpoint: have["authurl"].value,
Username: have["username"].Value,
APIKey: have["apikey"].Value,
IdentityEndpoint: have["authurl"].Value,
}

// upper-case the region
region := strings.ToUpper(have["region"].value)
region := strings.ToUpper(have["region"].Value)
// allow Gophercloud to re-authenticate
ao.AllowReauth = true

Expand Down
15 changes: 0 additions & 15 deletions auth/cliopts.go

This file was deleted.

6 changes: 4 additions & 2 deletions auth/envvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package auth
import (
"os"
"strings"

"github.com/jrperritt/rack/commandoptions"
)

func envvars(have map[string]authCred, need map[string]string) {
func envvars(have map[string]commandoptions.Cred, need map[string]string) {
vars := map[string]string{
"username": "RS_USERNAME",
"apikey": "RS_API_KEY",
Expand All @@ -14,7 +16,7 @@ func envvars(have map[string]authCred, need map[string]string) {
}
for opt := range need {
if v := os.Getenv(strings.ToUpper(vars[opt])); v != "" {
have[opt] = authCred{value: v, from: "environment variable"}
have[opt] = commandoptions.Cred{Value: v, From: "environment variable"}
delete(need, opt)
}
}
Expand Down
3 changes: 0 additions & 3 deletions auth/keyring.go

This file was deleted.

20 changes: 20 additions & 0 deletions commandoptions/cliopts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package commandoptions

import "github.com/jrperritt/rack/internal/github.com/codegangsta/cli"

type Cred struct {
Value string
From string
}

func CLIopts(c *cli.Context, have map[string]Cred, need map[string]string) {
for opt := range need {
if c.GlobalIsSet(opt) {
have[opt] = Cred{Value: c.GlobalString(opt), From: "command-line"}
delete(need, opt)
} else if c.IsSet(opt) {
have[opt] = Cred{Value: c.String(opt), From: "command-line"}
delete(need, opt)
}
}
}
2 changes: 1 addition & 1 deletion util/commandflags.go → commandoptions/commandflags.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package commandoptions

import (
"fmt"
Expand Down
14 changes: 7 additions & 7 deletions auth/configfile.go → commandoptions/configfile.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package auth
package commandoptions

import (
"fmt"
Expand All @@ -9,35 +9,35 @@ import (
"github.com/jrperritt/rack/util"
)

func configfile(c *cli.Context, have map[string]authCred, need map[string]string) error {
func ConfigFile(c *cli.Context, have map[string]Cred, need map[string]string) error {
var profile string
if c.GlobalIsSet("profile") {
profile = c.GlobalString("profile")
} else if c.IsSet("profile") {
profile = c.String("profile")
}

section, err := Section(profile)
section, err := ProfileSection(profile)
if err != nil {
return err
}

for opt := range need {
if val := section.Key(opt).String(); val != "" {
have[opt] = authCred{value: val, from: fmt.Sprintf("config file (profile: %s)", section.Name())}
have[opt] = Cred{Value: val, From: fmt.Sprintf("config file (profile: %s)", section.Name())}
delete(need, opt)
}
}

if profile != "" {
section, err := Section("")
section, err := ProfileSection("")
if err != nil {
return err
}

for opt := range need {
if val := section.Key(opt).String(); val != "" {
have[opt] = authCred{value: val, from: fmt.Sprintf("config file (profile: default)")}
have[opt] = Cred{Value: val, From: fmt.Sprintf("config file (profile: default)")}
delete(need, opt)
}
}
Expand All @@ -46,7 +46,7 @@ func configfile(c *cli.Context, have map[string]authCred, need map[string]string
return nil
}

func Section(profile string) (*ini.Section, error) {
func ProfileSection(profile string) (*ini.Section, error) {
dir, err := util.RackDir()
if err != nil {
// return fmt.Errorf("Error retrieving rack directory: %s\n", err)
Expand Down
2 changes: 1 addition & 1 deletion util/globalflags.go → commandoptions/globalflags.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package commandoptions

import "github.com/jrperritt/rack/internal/github.com/codegangsta/cli"

Expand Down
5 changes: 3 additions & 2 deletions commands/blockstoragecommands/snapshotcommands/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package snapshotcommands

import (
"github.com/jrperritt/rack/commandoptions"
"github.com/jrperritt/rack/handler"
"github.com/jrperritt/rack/internal/github.com/codegangsta/cli"
osSnapshots "github.com/jrperritt/rack/internal/github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
Expand All @@ -12,9 +13,9 @@ var create = cli.Command{
Usage: util.Usage(commandPrefix, "create", "--volume-id <volumeID>"),
Description: "Creates a volume",
Action: actionCreate,
Flags: util.CommandFlags(flagsCreate, keysCreate),
Flags: commandoptions.CommandFlags(flagsCreate, keysCreate),
BashComplete: func(c *cli.Context) {
util.CompleteFlags(util.CommandFlags(flagsCreate, keysCreate))
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsCreate, keysCreate))
},
}

Expand Down
5 changes: 3 additions & 2 deletions commands/blockstoragecommands/snapshotcommands/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package snapshotcommands
import (
"fmt"

"github.com/jrperritt/rack/commandoptions"
"github.com/jrperritt/rack/handler"
"github.com/jrperritt/rack/internal/github.com/codegangsta/cli"
osSnapshots "github.com/jrperritt/rack/internal/github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
Expand All @@ -14,9 +15,9 @@ var remove = cli.Command{
Usage: util.Usage(commandPrefix, "delete", "[--id <snapshotID> | --name <snapshotName> | --stdin id]"),
Description: "Deletes a snapshot",
Action: actionDelete,
Flags: util.CommandFlags(flagsDelete, keysDelete),
Flags: commandoptions.CommandFlags(flagsDelete, keysDelete),
BashComplete: func(c *cli.Context) {
util.CompleteFlags(util.CommandFlags(flagsDelete, keysDelete))
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsDelete, keysDelete))
},
}

Expand Down
5 changes: 3 additions & 2 deletions commands/blockstoragecommands/snapshotcommands/get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package snapshotcommands

import (
"github.com/jrperritt/rack/commandoptions"
"github.com/jrperritt/rack/handler"
"github.com/jrperritt/rack/internal/github.com/codegangsta/cli"
osSnapshots "github.com/jrperritt/rack/internal/github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
Expand All @@ -12,9 +13,9 @@ var get = cli.Command{
Usage: util.Usage(commandPrefix, "get", "[--id <snapshotID> | --name <snapshotName> | --stdin id]"),
Description: "Gets a snapshot",
Action: actionGet,
Flags: util.CommandFlags(flagsGet, keysGet),
Flags: commandoptions.CommandFlags(flagsGet, keysGet),
BashComplete: func(c *cli.Context) {
util.CompleteFlags(util.CommandFlags(flagsGet, keysGet))
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsGet, keysGet))
},
}

Expand Down
5 changes: 3 additions & 2 deletions commands/blockstoragecommands/snapshotcommands/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package snapshotcommands

import (
"github.com/jrperritt/rack/commandoptions"
"github.com/jrperritt/rack/handler"
"github.com/jrperritt/rack/internal/github.com/codegangsta/cli"
osSnapshots "github.com/jrperritt/rack/internal/github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
Expand All @@ -13,9 +14,9 @@ var list = cli.Command{
Usage: util.Usage(commandPrefix, "list", ""),
Description: "Lists existing snapshots",
Action: actionList,
Flags: util.CommandFlags(flagsList, keysList),
Flags: commandoptions.CommandFlags(flagsList, keysList),
BashComplete: func(c *cli.Context) {
util.CompleteFlags(util.CommandFlags(flagsList, keysList))
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsList, keysList))
},
}

Expand Down
5 changes: 3 additions & 2 deletions commands/blockstoragecommands/volumecommands/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package volumecommands

import (
"github.com/jrperritt/rack/commandoptions"
"github.com/jrperritt/rack/handler"
"github.com/jrperritt/rack/internal/github.com/codegangsta/cli"
osVolumes "github.com/jrperritt/rack/internal/github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
Expand All @@ -12,9 +13,9 @@ var create = cli.Command{
Usage: util.Usage(commandPrefix, "create", "--size <size>"),
Description: "Creates a volume",
Action: actionCreate,
Flags: util.CommandFlags(flagsCreate, keysCreate),
Flags: commandoptions.CommandFlags(flagsCreate, keysCreate),
BashComplete: func(c *cli.Context) {
util.CompleteFlags(util.CommandFlags(flagsCreate, keysCreate))
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsCreate, keysCreate))
},
}

Expand Down
5 changes: 3 additions & 2 deletions commands/blockstoragecommands/volumecommands/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package volumecommands
import (
"fmt"

"github.com/jrperritt/rack/commandoptions"
"github.com/jrperritt/rack/handler"
"github.com/jrperritt/rack/internal/github.com/codegangsta/cli"
osVolumes "github.com/jrperritt/rack/internal/github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
Expand All @@ -14,9 +15,9 @@ var remove = cli.Command{
Usage: util.Usage(commandPrefix, "delete", "[--id <volumeID> | | --name <volumeName> | --stdin id]"),
Description: "Deletes a volume",
Action: actionDelete,
Flags: util.CommandFlags(flagsDelete, keysDelete),
Flags: commandoptions.CommandFlags(flagsDelete, keysDelete),
BashComplete: func(c *cli.Context) {
util.CompleteFlags(util.CommandFlags(flagsDelete, keysDelete))
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsDelete, keysDelete))
},
}

Expand Down
5 changes: 3 additions & 2 deletions commands/blockstoragecommands/volumecommands/get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package volumecommands

import (
"github.com/jrperritt/rack/commandoptions"
"github.com/jrperritt/rack/handler"
"github.com/jrperritt/rack/internal/github.com/codegangsta/cli"
osVolumes "github.com/jrperritt/rack/internal/github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
Expand All @@ -12,9 +13,9 @@ var get = cli.Command{
Usage: util.Usage(commandPrefix, "get", "[--id <volumeID> | --name <volumeName> | --stdin id]"),
Description: "Gets a volume",
Action: actionGet,
Flags: util.CommandFlags(flagsGet, keysGet),
Flags: commandoptions.CommandFlags(flagsGet, keysGet),
BashComplete: func(c *cli.Context) {
util.CompleteFlags(util.CommandFlags(flagsGet, keysGet))
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsGet, keysGet))
},
}

Expand Down
5 changes: 3 additions & 2 deletions commands/blockstoragecommands/volumecommands/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package volumecommands

import (
"github.com/jrperritt/rack/commandoptions"
"github.com/jrperritt/rack/handler"
"github.com/jrperritt/rack/internal/github.com/codegangsta/cli"
osVolumes "github.com/jrperritt/rack/internal/github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
Expand All @@ -13,9 +14,9 @@ var list = cli.Command{
Usage: util.Usage(commandPrefix, "list", ""),
Description: "Lists existing volumes",
Action: actionList,
Flags: util.CommandFlags(flagsList, keysList),
Flags: commandoptions.CommandFlags(flagsList, keysList),
BashComplete: func(c *cli.Context) {
util.CompleteFlags(util.CommandFlags(flagsList, keysList))
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsList, keysList))
},
}

Expand Down
5 changes: 3 additions & 2 deletions commands/blockstoragecommands/volumecommands/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package volumecommands

import (
"github.com/jrperritt/rack/commandoptions"
"github.com/jrperritt/rack/handler"
"github.com/jrperritt/rack/internal/github.com/codegangsta/cli"
osVolumes "github.com/jrperritt/rack/internal/github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
Expand All @@ -12,9 +13,9 @@ var update = cli.Command{
Usage: util.Usage(commandPrefix, "update", "--id <volumeID>"),
Description: "Updates a volume",
Action: actionUpdate,
Flags: util.CommandFlags(flagsUpdate, keysUpdate),
Flags: commandoptions.CommandFlags(flagsUpdate, keysUpdate),
BashComplete: func(c *cli.Context) {
util.CompleteFlags(util.CommandFlags(flagsUpdate, keysUpdate))
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsUpdate, keysUpdate))
},
}

Expand Down
Loading

0 comments on commit 63b9785

Please sign in to comment.