@@ -15,11 +15,12 @@ import (
1515//
1616// Usage: hyper config
1717func (cli * DockerCli ) CmdConfig (args ... string ) error {
18- cmd := Cli .Subcmd ("config" , []string {"[SERVER ]" }, Cli .DockerCommands ["config" ].Description + ".\n If no server is specified, the default is defined as " + cliconfig .DefaultHyperServer , true )
18+ cmd := Cli .Subcmd ("config" , []string {"[REGION ]" }, Cli .DockerCommands ["config" ].Description + ".\n If no region is specified, the default is defined as " + cliconfig .DefaultHyperRegion , true )
1919 cmd .Require (flag .Max , 1 )
2020
2121 flAccesskey := cmd .String ([]string {"-accesskey" }, "" , "Access Key" )
2222 flSecretkey := cmd .String ([]string {"-secretkey" }, "" , "Secret Key" )
23+ flDefaultRegion := cmd .String ([]string {"-default-region" }, "" , "Default Region Endpoint" )
2324
2425 cmd .ParseFlags (args , true )
2526
@@ -32,10 +33,10 @@ func (cli *DockerCli) CmdConfig(args ...string) error {
3233 if len (cmd .Args ()) > 0 {
3334 serverAddress = cmd .Arg (0 )
3435 } else {
35- serverAddress = cliconfig .DefaultHyperServer
36+ serverAddress = cliconfig .DefaultHyperFormat
3637 }
3738
38- _ , err := cli .configureCloud (serverAddress , * flAccesskey , * flSecretkey )
39+ _ , err := cli .configureCloud (serverAddress , * flDefaultRegion , * flAccesskey , * flSecretkey )
3940 if err != nil {
4041 return err
4142 }
@@ -48,33 +49,75 @@ func (cli *DockerCli) CmdConfig(args ...string) error {
4849 return nil
4950}
5051
51- func (cli * DockerCli ) configureCloud (serverAddress , flAccesskey , flSecretkey string ) (cliconfig.CloudConfig , error ) {
52- cloudConfig , ok := cli .configFile .CloudConfig [serverAddress ]
53- if ! ok {
54- cloudConfig = cliconfig.CloudConfig {}
52+ func (cli * DockerCli ) configureCloud (serverAddress , flRegion , flAccesskey , flSecretkey string ) (cliconfig.CloudConfig , error ) {
53+ cloudConfig := cliconfig.CloudConfig {}
54+ if serverAddress != "" {
55+ if cc , ok := cli .configFile .CloudConfig [serverAddress ]; ok {
56+ cloudConfig = cc
57+ } else {
58+ // for legacy format
59+ defaultHost := "tcp://" + cliconfig .DefaultHyperRegion + "." + cliconfig .DefaultHyperEndpoint
60+ cloudConfig , ok = cli .configFile .CloudConfig [defaultHost ]
61+ if ok {
62+ delete (cli .configFile .CloudConfig , defaultHost )
63+ }
64+ }
5565 }
5666
67+ defaultRegion := cli .getDefaultRegion ()
68+ if cloudConfig .Region != "" {
69+ defaultRegion = cloudConfig .Region
70+ }
5771 if flAccesskey = strings .TrimSpace (flAccesskey ); flAccesskey == "" {
5872 cli .promptWithDefault ("Enter Access Key" , cloudConfig .AccessKey )
5973 flAccesskey = readInput (cli .in , cli .out )
6074 flAccesskey = strings .TrimSpace (flAccesskey )
75+ if flAccesskey == "" {
76+ flAccesskey = cloudConfig .AccessKey
77+ }
6178 }
6279 if flSecretkey = strings .TrimSpace (flSecretkey ); flSecretkey == "" {
6380 cli .promptWithDefault ("Enter Secret Key" , cloudConfig .SecretKey )
6481 flSecretkey = readInput (cli .in , cli .out )
6582 flSecretkey = strings .TrimSpace (flSecretkey )
83+ if flSecretkey == "" {
84+ flSecretkey = cloudConfig .SecretKey
85+ }
86+ }
87+ if flRegion = strings .TrimSpace (flRegion ); flRegion == "" {
88+ cli .promptWithDefault ("Enter Default Region" , defaultRegion )
89+ flRegion = readInput (cli .in , cli .out )
90+ flRegion = strings .TrimSpace (flRegion )
91+ if flRegion == "" {
92+ flRegion = defaultRegion
93+ }
6694 }
6795
6896 cloudConfig .AccessKey = flAccesskey
6997 cloudConfig .SecretKey = flSecretkey
70- cli .configFile .CloudConfig [serverAddress ] = cloudConfig
98+ cloudConfig .Region = flRegion
99+ if serverAddress != "" {
100+ cli .configFile .CloudConfig [serverAddress ] = cloudConfig
101+ }
102+
71103 return cloudConfig , nil
72104}
73105
74106func (cli * DockerCli ) checkCloudConfig () error {
75107 _ , ok := cli .configFile .CloudConfig [cli .host ]
76108 if ! ok {
77- return fmt .Errorf ("Config info for the host is not found, please run 'hyper config %s' first." , cli .host )
109+ _ , ok = cli .configFile .CloudConfig [cliconfig .DefaultHyperFormat ]
110+ if ! ok {
111+ return fmt .Errorf ("Config info for the host is not found, please run 'hyper config %s' first." , cli .host )
112+ }
78113 }
79114 return nil
80115}
116+
117+ func (cli * DockerCli ) getDefaultRegion () string {
118+ cc , ok := cli .configFile .CloudConfig [cliconfig .DefaultHyperFormat ]
119+ if ok && cc .Region != "" {
120+ return cc .Region
121+ }
122+ return cliconfig .DefaultHyperRegion
123+ }
0 commit comments