-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from helgi/registry
feat(registry): add support for setting private registry information per application
- Loading branch information
Showing
5 changed files
with
282 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/deis/pkg/prettyprint" | ||
|
||
"github.com/deis/workflow-cli/controller/api" | ||
"github.com/deis/workflow-cli/controller/models/config" | ||
) | ||
|
||
// RegistryList lists an app's registry information. | ||
func RegistryList(appID string) error { | ||
c, appID, err := load(appID) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
config, err := config.List(c, appID) | ||
|
||
fmt.Printf("=== %s Registry\n", appID) | ||
|
||
registryMap := make(map[string]string) | ||
|
||
for key, value := range config.Registry { | ||
registryMap[key] = fmt.Sprintf("%v", value) | ||
} | ||
|
||
fmt.Print(prettyprint.PrettyTabs(registryMap, 5)) | ||
|
||
return nil | ||
} | ||
|
||
// RegistrySet sets an app's registry information. | ||
func RegistrySet(appID string, item []string) error { | ||
c, appID, err := load(appID) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
registryMap := parseInfos(item) | ||
|
||
fmt.Print("Applying registry information... ") | ||
|
||
quit := progress() | ||
configObj := api.Config{} | ||
configObj.Registry = registryMap | ||
|
||
_, err = config.Set(c, appID, configObj) | ||
|
||
quit <- true | ||
<-quit | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Print("done\n\n") | ||
|
||
return RegistryList(appID) | ||
} | ||
|
||
// RegistryUnset removes an app's registry information. | ||
func RegistryUnset(appID string, items []string) error { | ||
c, appID, err := load(appID) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Print("Applying registry information... ") | ||
|
||
quit := progress() | ||
|
||
configObj := api.Config{} | ||
|
||
registryMap := make(map[string]interface{}) | ||
|
||
for _, key := range items { | ||
registryMap[key] = nil | ||
} | ||
|
||
configObj.Registry = registryMap | ||
|
||
_, err = config.Set(c, appID, configObj) | ||
|
||
quit <- true | ||
<-quit | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Print("done\n\n") | ||
|
||
return RegistryList(appID) | ||
} | ||
|
||
func parseInfos(items []string) map[string]interface{} { | ||
registryMap := make(map[string]interface{}) | ||
|
||
for _, item := range items { | ||
key, value, err := parseInfo(item) | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
continue | ||
} | ||
|
||
registryMap[key] = value | ||
} | ||
|
||
return registryMap | ||
} | ||
|
||
func parseInfo(item string) (string, string, error) { | ||
parts := strings.Split(item, "=") | ||
|
||
if len(parts) != 2 { | ||
return "", "", fmt.Errorf(`%s is invalid, Must be in format key=value | ||
Examples: username=bob password=s3cur3pw1`, item) | ||
} | ||
|
||
return parts[0], parts[1], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package parser | ||
|
||
import ( | ||
"github.com/deis/workflow-cli/cmd" | ||
docopt "github.com/docopt/docopt-go" | ||
) | ||
|
||
// Registry routes registry commands to their specific function | ||
func Registry(argv []string) error { | ||
usage := ` | ||
Valid commands for registry: | ||
registry:list list registry info for an app | ||
registry:set set registry info for an app | ||
registry:unset unset registry info for an app | ||
Use 'deis help [command]' to learn more. | ||
` | ||
|
||
switch argv[0] { | ||
case "registry:list": | ||
return registryList(argv) | ||
case "registry:set": | ||
return registrySet(argv) | ||
case "registry:unset": | ||
return registryUnset(argv) | ||
default: | ||
if printHelp(argv, usage) { | ||
return nil | ||
} | ||
|
||
if argv[0] == "registry" { | ||
argv[0] = "registry:list" | ||
return registryList(argv) | ||
} | ||
|
||
PrintUsage() | ||
return nil | ||
} | ||
} | ||
|
||
func registryList(argv []string) error { | ||
usage := ` | ||
Lists registry information for an application. | ||
Usage: deis registry:list [options] | ||
Options: | ||
-a --app=<app> | ||
the uniquely identifiable name of the application. | ||
` | ||
|
||
args, err := docopt.Parse(usage, argv, true, "", false, true) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return cmd.RegistryList(safeGetValue(args, "--app")) | ||
} | ||
|
||
func registrySet(argv []string) error { | ||
usage := ` | ||
Sets registry information for an application. | ||
key/value pairs used to configure / authenticate against external registries | ||
Usage: deis registry:set [options] <key>=<value>... | ||
Arguments: | ||
<key> the registry key, for example: "username" or "password" | ||
<value> the registry value, for example: "bob" or "s3cur3pw1" | ||
Options: | ||
-a --app=<app> | ||
the uniquely identifiable name for the application. | ||
` | ||
|
||
args, err := docopt.Parse(usage, argv, true, "", false, true) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
app := safeGetValue(args, "--app") | ||
info := args["<key>=<value>"].([]string) | ||
|
||
return cmd.RegistrySet(app, info) | ||
} | ||
|
||
func registryUnset(argv []string) error { | ||
usage := ` | ||
Unsets registry information for an application. | ||
Usage: deis registry:unset [options] <key>... | ||
Arguments: | ||
<key> the registry key to unset, for example: "username" or "password" | ||
Options: | ||
-a --app=<app> | ||
the uniquely identifiable name for the application. | ||
` | ||
|
||
args, err := docopt.Parse(usage, argv, true, "", false, true) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
app := safeGetValue(args, "--app") | ||
key := args["<key>"].([]string) | ||
|
||
return cmd.RegistryUnset(app, key) | ||
} |