Merge lp:~waigani/juju-core/switch-show-user into lp:~go-bot/juju-core/trunk

Proposed by Jesse Meek
Status: Needs review
Proposed branch: lp:~waigani/juju-core/switch-show-user
Merge into: lp:~go-bot/juju-core/trunk
Diff against target: 363 lines (+234/-33)
2 files modified
cmd/juju/switch.go (+165/-33)
cmd/juju/switch_test.go (+69/-0)
To merge this branch: bzr merge lp:~waigani/juju-core/switch-show-user
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+220868@code.launchpad.net

Description of the change

Add user infor to switch

Append current user's username to current environment
info printed out by `$ juju switch`.

https://codereview.appspot.com/92610043/

To post a comment you must log in.
Revision history for this message
Jesse Meek (waigani) wrote :
Download full text (6.9 KiB)

Reviewers: mp+220868_code.launchpad.net,

Message:
Please take a look.

Description:
Add user infor to switch

Append current user's username to current environment
info printed out by `$ juju switch`.

https://code.launchpad.net/~waigani/juju-core/switch-show-user/+merge/220868

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/92610043/

Affected files (+91, -5 lines):
   A [revision details]
   M cmd/juju/switch.go
   M cmd/juju/switch_test.go

Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: tarmac-20140523013529-f0gwneum0rsp1vfe
+New revision: <email address hidden>

Index: cmd/juju/switch.go
=== modified file 'cmd/juju/switch.go'
--- cmd/juju/switch.go 2014-05-09 13:24:50 +0000
+++ cmd/juju/switch.go 2014-05-25 01:07:06 +0000
@@ -4,16 +4,17 @@
  package main

  import (
- "errors"
   "fmt"
   "os"
   "sort"

+ "github.com/juju/errors"
   "launchpad.net/gnuflag"

   "launchpad.net/juju-core/cmd"
   "launchpad.net/juju-core/cmd/envcmd"
   "launchpad.net/juju-core/environs"
+ "launchpad.net/juju-core/environs/configstore"
  )

  type SwitchCommand struct {
@@ -62,6 +63,26 @@
   return false
  }

+// printEnvInfo appends the current user to the current environment and
prints
+// both to Stdout when `$ juju switch` is called
+func printEnvInfo(ctx *cmd.Context, msg string, envName string) (err
error) {
+ userInfo := ""
+ store, err := configstore.Default()
+ if err != nil {
+ return err
+ }
+ info, err := store.ReadInfo(envName)
+ if err != nil {
+ if !errors.IsNotFound(err) {
+ return err
+ }
+ } else {
+ userInfo = fmt.Sprintf("user: %s\n", info.APICredentials().User)
+ }
+ fmt.Fprint(ctx.Stdout, msg+userInfo)
+ return nil
+}
+
  func (c *SwitchCommand) Run(ctx *cmd.Context) error {
   // Switch is an alternative way of dealing with environments than using
   // the JUJU_ENV environment setting, and as such, doesn't play too well.
@@ -90,7 +111,10 @@
   jujuEnv := os.Getenv("JUJU_ENV")
   if jujuEnv != "" {
    if c.EnvName == "" {
- fmt.Fprintf(ctx.Stdout, "%s\n", jujuEnv)
+ msg := fmt.Sprintf("%s\n", jujuEnv)
+ if err := printEnvInfo(ctx, msg, jujuEnv); err != nil {
+ return err
+ }
     return nil
    } else {
     return fmt.Errorf("cannot switch when JUJU_ENV is overriding the
environment (set to %q)", jujuEnv)
@@ -109,7 +133,10 @@
    return errors.New("no currently specified environment")
   case c.EnvName == "":
    // Simply print the current environment.
- fmt.Fprintf(ctx.Stdout, "%s\n", currentEnv)
+ msg := fmt.Sprintf("%s\n", currentEnv)
+ if err := printEnvInfo(ctx, msg, currentEnv); err != nil {
+ return err
+ }
   default:
    // Switch the environment.
    if !validEnvironmentName(c.EnvName, names) {
@@ -119,9 +146,15 @@
     return err
    }
    if currentEnv == "" {
- fmt.Fprintf(ctx.Stdout, "-> %s\n", c.EnvName)
+ msg := fmt.Sprintf("-> %s\n", c.EnvName)
+ if err := printEnvInfo(ctx, msg, c.EnvName); err != nil {
+ return err
+ }
    } else {
- fmt.Fprin...

Read more...

Revision history for this message
Menno Finlay-Smits (menno.smits) wrote :

LGTM. Nitpicks only.

https://codereview.appspot.com/92610043/diff/1/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/1/cmd/juju/switch.go#newcode84
cmd/juju/switch.go:84: }
This might be cleaner if you extracted the code to retrieve the current
user to it's own function that printEnvInfo calls.

Personally, I'd also move printEnvInfo underneath Run. It's considered
good practice to have higher level functions towards the top of a source
file and to have the lower level functions they use underneath. It makes
the code easier for other developers to read because the lower level
functionality can be easily ignored - or at least doesn't have to be
digested up front - in order to get an understanding of what the code is
doing and how it hangs together.

https://codereview.appspot.com/92610043/diff/1/cmd/juju/switch.go#newcode153
cmd/juju/switch.go:153: } else {
The 2 printEnvInfo calls here could be reduced to one by calling it
after the "if currentEnv" block.

https://codereview.appspot.com/92610043/

Revision history for this message
Roger Peppe (rogpeppe) wrote :
Download full text (9.0 KiB)

Implementation looks fine in general (+1 to Menno's remarks) but...

Having juju switch print this seems a bit odd as it doesn't have anything
else to do with users, does it? Is there a "juju user" command? If not we
could make one. BTW this change may potentially break backwards
compatibility with sh scripts I think.
On 25 May 2014 02:15, "Jesse Meek" <email address hidden> wrote:

> Reviewers: mp+220868_code.launchpad.net,
>
> Message:
> Please take a look.
>
> Description:
> Add user infor to switch
>
> Append current user's username to current environment
> info printed out by `$ juju switch`.
>
>
> https://code.launchpad.net/~waigani/juju-core/switch-show-user/+merge/220868
>
> (do not edit description out of merge proposal)
>
>
> Please review this at https://codereview.appspot.com/92610043/
>
> Affected files (+91, -5 lines):
> A [revision details]
> M cmd/juju/switch.go
> M cmd/juju/switch_test.go
>
>
> Index: [revision details]
> === added file '[revision details]'
> --- [revision details] 2012-01-01 00:00:00 +0000
> +++ [revision details] 2012-01-01 00:00:00 +0000
> @@ -0,0 +1,2 @@
> +Old revision: tarmac-20140523013529-f0gwneum0rsp1vfe
> +New revision: <email address hidden>
>
> Index: cmd/juju/switch.go
> === modified file 'cmd/juju/switch.go'
> --- cmd/juju/switch.go 2014-05-09 13:24:50 +0000
> +++ cmd/juju/switch.go 2014-05-25 01:07:06 +0000
> @@ -4,16 +4,17 @@
> package main
>
> import (
> - "errors"
> "fmt"
> "os"
> "sort"
>
> + "github.com/juju/errors"
> "launchpad.net/gnuflag"
>
> "launchpad.net/juju-core/cmd"
> "launchpad.net/juju-core/cmd/envcmd"
> "launchpad.net/juju-core/environs"
> + "launchpad.net/juju-core/environs/configstore"
> )
>
> type SwitchCommand struct {
> @@ -62,6 +63,26 @@
> return false
> }
>
> +// printEnvInfo appends the current user to the current environment and
> prints
> +// both to Stdout when `$ juju switch` is called
> +func printEnvInfo(ctx *cmd.Context, msg string, envName string) (err
> error) {
> + userInfo := ""
> + store, err := configstore.Default()
> + if err != nil {
> + return err
> + }
> + info, err := store.ReadInfo(envName)
> + if err != nil {
> + if !errors.IsNotFound(err) {
> + return err
> + }
> + } else {
> + userInfo = fmt.Sprintf("user: %s\n",
> info.APICredentials().User)
> + }
> + fmt.Fprint(ctx.Stdout, msg+userInfo)
> + return nil
> +}
> +
> func (c *SwitchCommand) Run(ctx *cmd.Context) error {
> // Switch is an alternative way of dealing with environments than
> using
> // the JUJU_ENV environment setting, and as such, doesn't play too
> well.
> @@ -90,7 +111,10 @@
> jujuEnv := os.Getenv("JUJU_ENV")
> if jujuEnv != "" {
> if c.EnvName == "" {
> - fmt.Fprintf(ctx.Stdout, "%s\n", jujuEnv)
> + msg := fmt.Sprintf("%s\n", jujuEnv)
> + if err := printEnvInfo(ctx, msg, jujuEnv); err !=
> nil ...

Read more...

Revision history for this message
Jesse Meek (waigani) wrote :
Download full text (9.6 KiB)

Hi Rog,

Yeah, I also think it would make more sense for a subcommand of juju
user to show the user info. I will bring that up in stand up today.

BTW, I want your phone! Where can I find one?

Cheers,
Jess

On 26/05/14 10:21, Roger Peppe wrote:
> Implementation looks fine in general (+1 to Menno's remarks) but.
> Having juju switch print this seems a bit odd as it doesn't have anything
> else to do with users, does it? Is there a "juju user" command? If not we
> could make one. BTW this change may potentially break backwards
> compatibility with sh scripts I think.
> On 25 May 2014 02:15, "Jesse Meek" <email address hidden> wrote:
>
>> Reviewers: mp+220868_code.launchpad.net,
>>
>> Message:
>> Please take a look.
>>
>> Description:
>> Add user infor to switch
>>
>> Append current user's username to current environment
>> info printed out by `$ juju switch`.
>>
>>
>> https://code.launchpad.net/~waigani/juju-core/switch-show-user/+merge/220868
>>
>> (do not edit description out of merge proposal)
>>
>>
>> Please review this at https://codereview.appspot.com/92610043/
>>
>> Affected files (+91, -5 lines):
>> A [revision details]
>> M cmd/juju/switch.go
>> M cmd/juju/switch_test.go
>>
>>
>> Index: [revision details]
>> === added file '[revision details]'
>> --- [revision details] 2012-01-01 00:00:00 +0000
>> +++ [revision details] 2012-01-01 00:00:00 +0000
>> @@ -0,0 +1,2 @@
>> +Old revision: tarmac-20140523013529-f0gwneum0rsp1vfe
>> +New revision: <email address hidden>
>>
>> Index: cmd/juju/switch.go
>> === modified file 'cmd/juju/switch.go'
>> --- cmd/juju/switch.go 2014-05-09 13:24:50 +0000
>> +++ cmd/juju/switch.go 2014-05-25 01:07:06 +0000
>> @@ -4,16 +4,17 @@
>> package main
>>
>> import (
>> - "errors"
>> "fmt"
>> "os"
>> "sort"
>>
>> + "github.com/juju/errors"
>> "launchpad.net/gnuflag"
>>
>> "launchpad.net/juju-core/cmd"
>> "launchpad.net/juju-core/cmd/envcmd"
>> "launchpad.net/juju-core/environs"
>> + "launchpad.net/juju-core/environs/configstore"
>> )
>>
>> type SwitchCommand struct {
>> @@ -62,6 +63,26 @@
>> return false
>> }
>>
>> +// printEnvInfo appends the current user to the current environment and
>> prints
>> +// both to Stdout when `$ juju switch` is called
>> +func printEnvInfo(ctx *cmd.Context, msg string, envName string) (err
>> error) {
>> + userInfo := ""
>> + store, err := configstore.Default()
>> + if err != nil {
>> + return err
>> + }
>> + info, err := store.ReadInfo(envName)
>> + if err != nil {
>> + if !errors.IsNotFound(err) {
>> + return err
>> + }
>> + } else {
>> + userInfo = fmt.Sprintf("user: %s\n",
>> info.APICredentials().User)
>> + }
>> + fmt.Fprint(ctx.Stdout, msg+userInfo)
>> + return nil
>> +}
>> +
>> func (c *SwitchCommand) Run(ctx *cmd.Context) error {
>> // Switch is an alternative way of dealing with environments than
>> using
>> // the JUJU_ENV environment setting, and as such, doesn'...

Read more...

Revision history for this message
Jesse Meek (waigani) wrote :
Download full text (9.5 KiB)

oops, ignore the phone question. I thought I was replying to Rog
directly, facepalm.

On 26/05/14 10:21, Roger Peppe wrote:
> Implementation looks fine in general (+1 to Menno's remarks) but...
>
> Having juju switch print this seems a bit odd as it doesn't have anything
> else to do with users, does it? Is there a "juju user" command? If not we
> could make one. BTW this change may potentially break backwards
> compatibility with sh scripts I think.
> On 25 May 2014 02:15, "Jesse Meek" <email address hidden> wrote:
>
>> Reviewers: mp+220868_code.launchpad.net,
>>
>> Message:
>> Please take a look.
>>
>> Description:
>> Add user infor to switch
>>
>> Append current user's username to current environment
>> info printed out by `$ juju switch`.
>>
>>
>> https://code.launchpad.net/~waigani/juju-core/switch-show-user/+merge/220868
>>
>> (do not edit description out of merge proposal)
>>
>>
>> Please review this at https://codereview.appspot.com/92610043/
>>
>> Affected files (+91, -5 lines):
>> A [revision details]
>> M cmd/juju/switch.go
>> M cmd/juju/switch_test.go
>>
>>
>> Index: [revision details]
>> === added file '[revision details]'
>> --- [revision details] 2012-01-01 00:00:00 +0000
>> +++ [revision details] 2012-01-01 00:00:00 +0000
>> @@ -0,0 +1,2 @@
>> +Old revision: tarmac-20140523013529-f0gwneum0rsp1vfe
>> +New revision: <email address hidden>
>>
>> Index: cmd/juju/switch.go
>> === modified file 'cmd/juju/switch.go'
>> --- cmd/juju/switch.go 2014-05-09 13:24:50 +0000
>> +++ cmd/juju/switch.go 2014-05-25 01:07:06 +0000
>> @@ -4,16 +4,17 @@
>> package main
>>
>> import (
>> - "errors"
>> "fmt"
>> "os"
>> "sort"
>>
>> + "github.com/juju/errors"
>> "launchpad.net/gnuflag"
>>
>> "launchpad.net/juju-core/cmd"
>> "launchpad.net/juju-core/cmd/envcmd"
>> "launchpad.net/juju-core/environs"
>> + "launchpad.net/juju-core/environs/configstore"
>> )
>>
>> type SwitchCommand struct {
>> @@ -62,6 +63,26 @@
>> return false
>> }
>>
>> +// printEnvInfo appends the current user to the current environment and
>> prints
>> +// both to Stdout when `$ juju switch` is called
>> +func printEnvInfo(ctx *cmd.Context, msg string, envName string) (err
>> error) {
>> + userInfo := ""
>> + store, err := configstore.Default()
>> + if err != nil {
>> + return err
>> + }
>> + info, err := store.ReadInfo(envName)
>> + if err != nil {
>> + if !errors.IsNotFound(err) {
>> + return err
>> + }
>> + } else {
>> + userInfo = fmt.Sprintf("user: %s\n",
>> info.APICredentials().User)
>> + }
>> + fmt.Fprint(ctx.Stdout, msg+userInfo)
>> + return nil
>> +}
>> +
>> func (c *SwitchCommand) Run(ctx *cmd.Context) error {
>> // Switch is an alternative way of dealing with environments than
>> using
>> // the JUJU_ENV environment setting, and as such, doesn't play too
>> well.
>> @@ -90,7 +111,10 @@
>> jujuEnv := os.Getenv("JUJU_ENV")
>> if jujuEnv != "" {...

Read more...

Revision history for this message
Jesse Meek (waigani) wrote :
Revision history for this message
Tim Penhey (thumper) wrote :

https://codereview.appspot.com/92610043/diff/20001/cmd/juju/switch_test.go
File cmd/juju/switch_test.go (right):

https://codereview.appspot.com/92610043/diff/20001/cmd/juju/switch_test.go#newcode24
cmd/juju/switch_test.go:24: User: "joblow",
Can we just use "joe" here? For reasons explained on IRC.

https://codereview.appspot.com/92610043/diff/20001/cmd/juju/switch_test.go#newcode123
cmd/juju/switch_test.go:123: c.Assert(testing.Stdout(context),
gc.Equals, "erewhemos -> erewhemos-2\nuser: "+testCreds.User+"\n")
I think this would be better as:

   erewhemos -> erewhemos-2 as user "joe"

Also, best to make the expected text explicit if you can.

https://codereview.appspot.com/92610043/

Revision history for this message
Jesse Meek (waigani) wrote :
Revision history for this message
Tim Penhey (thumper) wrote :

https://codereview.appspot.com/92610043/diff/40001/cmd/juju/switch_test.go
File cmd/juju/switch_test.go (right):

https://codereview.appspot.com/92610043/diff/40001/cmd/juju/switch_test.go#newcode65
cmd/juju/switch_test.go:65: c.Assert(testing.Stdout(context), gc.Equals,
"erewhemos as user "+testCreds.User+"\n")
As I mentioned before, make the string explicit:

c.Assert(testing.Stdout(context), gc.Equals, `erewhemos as user "joe"` +
"\n")

Horrible having to add the + "\n" to the end, but better than
...\"joe\"\n"

https://codereview.appspot.com/92610043/

Revision history for this message
Jesse Meek (waigani) wrote :

On 2014/05/26 01:46:37, thumper wrote:

https://codereview.appspot.com/92610043/diff/40001/cmd/juju/switch_test.go
> File cmd/juju/switch_test.go (right):

https://codereview.appspot.com/92610043/diff/40001/cmd/juju/switch_test.go#newcode65
> cmd/juju/switch_test.go:65: c.Assert(testing.Stdout(context),
gc.Equals,
> "erewhemos as user "+testCreds.User+"\n")
> As I mentioned before, make the string explicit:

> c.Assert(testing.Stdout(context), gc.Equals, `erewhemos as user "joe"`
+ "\n")

> Horrible having to add the + "\n" to the end, but better than
...\"joe\"\n"

oops, missed that. Done now.

https://codereview.appspot.com/92610043/

Revision history for this message
Jesse Meek (waigani) wrote :
Revision history for this message
Tim Penhey (thumper) wrote :

https://codereview.appspot.com/92610043/diff/60001/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/60001/cmd/juju/switch.go#newcode149
cmd/juju/switch.go:149: if username != "" {
Hmm... is this implied "admin"?

https://codereview.appspot.com/92610043/diff/60001/cmd/juju/switch_test.go
File cmd/juju/switch_test.go (right):

https://codereview.appspot.com/92610043/diff/60001/cmd/juju/switch_test.go#newcode65
cmd/juju/switch_test.go:65: c.Assert(testing.Stdout(context), gc.Equals,
`erewhemos as user "`+testCreds.User+"\"\n")
umm... your earlier comment said you fixed this. Did you commit it?

https://codereview.appspot.com/92610043/

Revision history for this message
Jesse Meek (waigani) wrote :
Revision history for this message
Jesse Meek (waigani) wrote :

https://codereview.appspot.com/92610043/diff/60001/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/60001/cmd/juju/switch.go#newcode149
cmd/juju/switch.go:149: if username != "" {
On 2014/05/26 04:09:42, thumper wrote:
> Hmm... is this implied "admin"?

If there is no .jenv file, username is not appended to output of switch.
Are you saying that instead the "admin" user should be appended?

https://codereview.appspot.com/92610043/

Revision history for this message
William Reade (fwereade) wrote :

I'm not convinced by this. It's a compatibility break (which is not in
itself necessarily bad -- we should be better when we can be) but (1)
AIUI, people actively use this in scripts and (2) I'm not convinced it
actually offers anything significantly better.

* username is quoted but envname isn't
* username sometimes shows up and sometimes doesn't
* it's going to be kinda annoying to parse

ISTM that if we're going to Do It Right we should collect a map (or even
a struct) of all the relevant information and give it a --format arg (in
which the default "smart" (lol) formatting can match the existing output
for the next release. Counterarguments?

https://codereview.appspot.com/92610043/

Revision history for this message
William Reade (fwereade) wrote :

On 2014/05/26 07:26:56, fwereade wrote:
> I'm not convinced by this. It's a compatibility break (which is not in
itself
> necessarily bad -- we should be better when we can be) but (1) AIUI,
people
> actively use this in scripts and (2) I'm not convinced it actually
offers
> anything significantly better.

> * username is quoted but envname isn't
> * username sometimes shows up and sometimes doesn't
> * it's going to be kinda annoying to parse

> ISTM that if we're going to Do It Right we should collect a map (or
even a
> struct) of all the relevant information and give it a --format arg (in
which the
> default "smart" (lol) formatting can match the existing output for the
next
> release. Counterarguments?

*also*, this is *unquestionably* user-facing. We need docs for such
features to exist at the time of writing, so that evilnick can see them
and have a chance to massage them into a happy shape before the feature
ends up in front of our users.

https://codereview.appspot.com/92610043/

Revision history for this message
Jesse Meek (waigani) wrote :
Revision history for this message
William Reade (fwereade) wrote :

Coming along nicely, thanks.

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode53
cmd/juju/switch.go:53: $ juju switch --env-info
Not *really* loving --env-info... "--explain" isn't great either, but it
feels a bit closer to what we're asking for... or what about "--long"?
we want to make it super-easy for people to use this, and get used to it
-- for that matter, we should probably have a single-char flag too)

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode55
cmd/juju/switch.go:55: apiEndpoint:
I think we need:

environ-name [0]
environ-uuid
state-servers
user-name

...but I'm not so sure ca-cert is very nice to include, because it'll be
MASSIVE and push interesting stuff offscreen.

[0] environ-name is strange and interesting, fwiw -- I think it can only
ever really *sanely* be a local alias for a user/env pair -- but we *do*
have environment names lurking in the state, meaningless though they may
be. Can you think of a better way to describe what it actually is?

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode60
cmd/juju/switch.go:60: envName: local
please use "names-with-hyphens", not namesWithCaps, for consistency.

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode72
cmd/juju/switch.go:72: oldEnvName: ec2
previous-environ-name perhaps?

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode77
cmd/juju/switch.go:77: {"apiEndpoint":
similarly, marshal to json with nice-names-like-this.

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode83
cmd/juju/switch.go:83: "username":"joe"
Thanks for the docs (for all that they're still in a state of flux) -- I
find it *much* easier to think through the consistency and consequences
and so on when the UX is laid out plainly like this.

Please make sure evilnick knows about them so he has a chance to fix up
the online docs for the next release -- we absolutely want his feedback
on what's a good and easy-to-communicate experience.

https://codereview.appspot.com/92610043/

Revision history for this message
Menno Finlay-Smits (menno.smits) wrote :

Just some little things I noticed

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode76
cmd/juju/switch.go:76: # Format environment inforamtion as json
information

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode190
cmd/juju/switch.go:190: }
Can't the whole "if currentEnv" block now be replaced with:

if err := c.printEnvInfo(ctx, c.EnvName, currentEnv); err != nil {
      return err
}

?

(The first case passes "" when currentEnv is "")

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode251
cmd/juju/switch.go:251: return username, nil
Because this function uses named return values this line can just be
"return" if you like.

https://codereview.appspot.com/92610043/

Revision history for this message
Jesse Meek (waigani) wrote :
Download full text (3.8 KiB)

Please take a look.

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode53
cmd/juju/switch.go:53: $ juju switch --env-info
On 2014/05/27 07:47:12, fwereade wrote:
> Not *really* loving --env-info... "--explain" isn't great either, but
it feels a
> bit closer to what we're asking for... or what about "--long"? we want
to make
> it super-easy for people to use this, and get used to it -- for that
matter, we
> should probably have a single-char flag too)

We already have single-char flag (-e), added example to docs. If we use
"--long" we'd conflict with single-char flag for "--list", -li and/or
-lo?

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode55
cmd/juju/switch.go:55: apiEndpoint:
On 2014/05/27 07:47:12, fwereade wrote:
> I think we need:

> environ-name [0]
> environ-uuid
> state-servers
> user-name

> ...but I'm not so sure ca-cert is very nice to include, because it'll
be MASSIVE
> and push interesting stuff offscreen.

> [0] environ-name is strange and interesting, fwiw -- I think it can
only ever
> really *sanely* be a local alias for a user/env pair -- but we *do*
have
> environment names lurking in the state, meaningless though they may
be. Can you
> think of a better way to describe what it actually is?

My understanding of environ-name is that it is simply the name of an
environment, defined in environments.yaml. An environ must always have a
user, but I don't see how that makes environ-name anything but the name
of an environment still? Am I missing something? Can you give me an
example of the problem?

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode60
cmd/juju/switch.go:60: envName: local
On 2014/05/27 07:47:12, fwereade wrote:
> please use "names-with-hyphens", not namesWithCaps, for consistency.

Done.

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode72
cmd/juju/switch.go:72: oldEnvName: ec2
On 2014/05/27 07:47:13, fwereade wrote:
> previous-environ-name perhaps?

Done.

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode76
cmd/juju/switch.go:76: # Format environment inforamtion as json
On 2014/05/27 21:16:05, menn0 wrote:
> information

Done.

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode77
cmd/juju/switch.go:77: {"apiEndpoint":
On 2014/05/27 07:47:12, fwereade wrote:
> similarly, marshal to json with nice-names-like-this.

Done.

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode83
cmd/juju/switch.go:83: "username":"joe"
On 2014/05/27 07:47:13, fwereade wrote:
> Thanks for the docs (for all that they're still in a state of flux) --
I find it
> *much* easier to think through the consistency and consequences and so
on when
> the UX is laid out plainly like this.

> Please make sure evilnick knows about them so he has a chance to fix
up the
> online docs for the next release -- we absolutely want his feedback on
what's a
> good and easy-to-communicate experience.

Done.

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/...

Read more...

Revision history for this message
Tim Penhey (thumper) wrote :

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode53
cmd/juju/switch.go:53: $ juju switch --env-info
Why is this not just:
   juju switch --format=yaml
?

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode70
cmd/juju/switch.go:70: $ juju switch local --env-info
Same here:
   juju switch local --format=yaml

I don't think we need --env-info at all.

https://codereview.appspot.com/92610043/

Revision history for this message
William Reade (fwereade) wrote :
Download full text (3.7 KiB)

quick comments while I can, I'll try to do some more before you get
online

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode55
cmd/juju/switch.go:55: apiEndpoint:
On 2014/05/28 00:30:47, waigani wrote:
> On 2014/05/27 07:47:12, fwereade wrote:
> > I think we need:
> >
> > environ-name [0]
> > environ-uuid
> > state-servers
> > user-name
> >
> > ...but I'm not so sure ca-cert is very nice to include, because
it'll be
> MASSIVE
> > and push interesting stuff offscreen.
> >
> > [0] environ-name is strange and interesting, fwiw -- I think it can
only ever
> > really *sanely* be a local alias for a user/env pair -- but we *do*
have
> > environment names lurking in the state, meaningless though they may
be. Can
> you
> > think of a better way to describe what it actually is?

> My understanding of environ-name is that it is simply the name of an
> environment, defined in environments.yaml. An environ must always have
a user,
> but I don't see how that makes environ-name anything but the name of
an
> environment still? Am I missing something? Can you give me an example
of the
> problem?

I have an environment named "prod", and so do you. I give you access to
mine. How do you disambiguate? Environment name was a really bad idea --
we made it something fundamental, instead of explicitly making it a
local alias, and we continue to pay the price; but we're making some
progress, because what you're actually choosing in the general case is
now a *jenv* name, not an environment name.

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode251
cmd/juju/switch.go:251: return username, nil
On 2014/05/28 00:30:47, waigani wrote:
> On 2014/05/27 21:16:04, menn0 wrote:
> > Because this function uses named return values this line can just be
"return"
> if
> > you like.

> This would return an 'is not found' error, when we don't want to.

And FWIW I have generally found that the benefits of empty returns are
outweighed by the costs of (frequently) having to scan back through the
function to be sure what actually happened.

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode45
cmd/juju/switch.go:45: # Show current environment name
General suggestion:

# description
$ command
output
output
output

?

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode53
cmd/juju/switch.go:53: $ juju switch --env-info
On 2014/05/28 02:26:46, thumper wrote:
> Why is this not just:
> juju switch --format=yaml
> ?

Very good point. We can define a "smart" (lol) formatter that preserves
existing behaviour.

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode197
cmd/juju/switch.go:197: servers, err := stateSevers(envName)
s/Severs/Servers/

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode205
cmd/juju/switch.go:205: "state-servers": servers,
I'd be more inclined to define this as a struct, with explicit
marshalling tags...

Read more...

Revision history for this message
Menno Finlay-Smits (menno.smits) wrote :

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/90001/cmd/juju/switch.go#newcode251
cmd/juju/switch.go:251: return username, nil
On 2014/05/28 16:01:41, fwereade wrote:
> On 2014/05/28 00:30:47, waigani wrote:
> > On 2014/05/27 21:16:04, menn0 wrote:
> > > Because this function uses named return values this line can just
be
> "return"
> > if
> > > you like.
> >
> > This would return an 'is not found' error, when we don't want to.

> And FWIW I have generally found that the benefits of empty returns are
> outweighed by the costs of (frequently) having to scan back through
the function
> to be sure what actually happened.

Yep you're right. This is probably a case of shiny-new-language-toy-itis
on my part :)

Sorry for the noise.

https://codereview.appspot.com/92610043/

Revision history for this message
Andrew Wilkins (axwalk) wrote :

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode53
cmd/juju/switch.go:53: $ juju switch --env-info
On 2014/05/28 02:26:46, thumper wrote:
> Why is this not just:
> juju switch --format=yaml
> ?

+1

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode205
cmd/juju/switch.go:205: "state-servers": servers,
On 2014/05/28 16:01:41, fwereade wrote:
> I'd be more inclined to define this as a struct, with explicit
marshalling tags.

+1
the contents of the output are important to users (e.g. scripts), so we
should create a well-defined struct to deter people from making random
changes to those contents.

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode239
cmd/juju/switch.go:239: if err != nil {
Why are we swallowing other errors?

https://codereview.appspot.com/92610043/

Revision history for this message
Jesse Meek (waigani) wrote :

Please take a look.

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode45
cmd/juju/switch.go:45: # Show current environment name
On 2014/05/28 16:01:41, fwereade wrote:
> General suggestion:

> # description
> $ command
> output
> output
> output

> ?

Done.

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode53
cmd/juju/switch.go:53: $ juju switch --env-info
On 2014/05/28 02:26:46, thumper wrote:
> Why is this not just:
> juju switch --format=yaml
> ?

Done.

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode53
cmd/juju/switch.go:53: $ juju switch --env-info
On 2014/05/28 02:26:46, thumper wrote:
> Why is this not just:
> juju switch --format=yaml
> ?

Done.

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode53
cmd/juju/switch.go:53: $ juju switch --env-info
On 2014/05/28 16:01:41, fwereade wrote:
> On 2014/05/28 02:26:46, thumper wrote:
> > Why is this not just:
> > juju switch --format=yaml
> > ?

> Very good point. We can define a "smart" (lol) formatter that
preserves existing
> behaviour.

When formatter is "smart", no extra info is added and nothing is
formatted.

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode70
cmd/juju/switch.go:70: $ juju switch local --env-info
On 2014/05/28 02:26:46, thumper wrote:
> Same here:
> juju switch local --format=yaml

> I don't think we need --env-info at all.

Done.

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode197
cmd/juju/switch.go:197: servers, err := stateSevers(envName)
On 2014/05/28 16:01:41, fwereade wrote:
> s/Severs/Servers/

Done.

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode205
cmd/juju/switch.go:205: "state-servers": servers,
On 2014/05/28 16:01:41, fwereade wrote:
> I'd be more inclined to define this as a struct, with explicit
marshalling tags.

Done.

https://codereview.appspot.com/92610043/diff/110001/cmd/juju/switch.go#newcode239
cmd/juju/switch.go:239: if err != nil {
On 2014/05/29 03:05:06, axw wrote:
> Why are we swallowing other errors?

To allow the user to switch to an environment that has not yet been
bootstrapped.

https://codereview.appspot.com/92610043/

Revision history for this message
William Reade (fwereade) wrote :

Nearly there, I think it just needs another pass to rework printEnvInfo.
I have a pretty strong feeling that that method should purely be about
*constructing* an EnvInfo -- with all necessary information,
specifically *not* tuning the information based on the formatter, which
it shouldn't make use of -- and that the result-printing can then be
handled, once, in Run (and as mentioned, the simple formatter can just
ignore the fields it doesn't use).

https://codereview.appspot.com/92610043/diff/130001/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/130001/cmd/juju/switch.go#newcode119
cmd/juju/switch.go:119: c.EnvInfo = c.out.Name() != "simple"
-1 on this. Can't we always grab the same information, and define a
formatter that outputs the subset we want in the style we want?

https://codereview.appspot.com/92610043/diff/130001/cmd/juju/switch.go#newcode266
cmd/juju/switch.go:266: return nil, nil
return an actual error here please

https://codereview.appspot.com/92610043/diff/130001/cmd/juju/switch_test.go
File cmd/juju/switch_test.go (right):

https://codereview.appspot.com/92610043/diff/130001/cmd/juju/switch_test.go#newcode142
cmd/juju/switch_test.go:142: c.Assert(testing.Stdout(context),
gc.Equals,
It's rarely a good idea to test yaml directly -- better (if more
tedious) to unmarshal whatever got marshalled and check that. (there are
multiple valid forms of the yaml outout, and who knows what might cause
a change in what actually gets written)

https://codereview.appspot.com/92610043/diff/130001/cmd/juju/switch_test.go#newcode154
cmd/juju/switch_test.go:154: c.Assert(testing.Stdout(context),
gc.Equals,
ditto

https://codereview.appspot.com/92610043/diff/130001/cmd/juju/switch_test.go#newcode166
cmd/juju/switch_test.go:166: c.Assert(testing.Stdout(context),
gc.Equals,
"{\"user-name\":\"joe\",\"environ-name\":\"erewhemos\",\"state-servers\":[\"example.com\",\"kremvax.ru\"]}\n")
ditto

https://codereview.appspot.com/92610043/diff/130001/cmd/juju/switch_test.go#newcode173
cmd/juju/switch_test.go:173: c.Assert(testing.Stdout(context),
gc.Equals,
"{\"user-name\":\"joe\",\"environ-name\":\"erewhemos-2\",\"previous-environ-name\":\"erewhemos\",\"state-servers\":[\"example.com\",\"kremvax.ru\"]}\n")
ditto

https://codereview.appspot.com/92610043/

Revision history for this message
Jesse Meek (waigani) wrote :

Please take a look.

https://codereview.appspot.com/92610043/diff/130001/cmd/juju/switch.go
File cmd/juju/switch.go (right):

https://codereview.appspot.com/92610043/diff/130001/cmd/juju/switch.go#newcode119
cmd/juju/switch.go:119: c.EnvInfo = c.out.Name() != "simple"
On 2014/06/02 08:56:03, fwereade wrote:
> -1 on this. Can't we always grab the same information, and define a
formatter
> that outputs the subset we want in the style we want?

Done. Actually, the current "simple" formatter already only prints out a
subset. So I can simply remove c.EnvInfo.

https://codereview.appspot.com/92610043/diff/130001/cmd/juju/switch.go#newcode266
cmd/juju/switch.go:266: return nil, nil
On 2014/06/02 08:56:03, fwereade wrote:
> return an actual error here please

Done.

https://codereview.appspot.com/92610043/

Unmerged revisions

2789. By Jesse Meek

Remove c.EnvInfo, unmashal yaml/json in tests

2788. By Jesse Meek

Add 'simple' formatter for default behaviour, change map to struct

2787. By Jesse Meek

merge trunk

2786. By Jesse Meek

remove --env-info flag

2785. By Jesse Meek

Polish as per review

2784. By Jesse Meek

Change everything, as per review!

2783. By Jesse Meek

explicit string, take two

2782. By Jesse Meek

make string explicit

2781. By Jesse Meek

cleanup code, as per second review

2780. By Jesse Meek

cleanup code as per review

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cmd/juju/switch.go'
2--- cmd/juju/switch.go 2014-05-09 13:24:50 +0000
3+++ cmd/juju/switch.go 2014-06-03 00:37:45 +0000
4@@ -4,20 +4,23 @@
5 package main
6
7 import (
8- "errors"
9 "fmt"
10 "os"
11 "sort"
12
13+ "github.com/juju/errors"
14 "launchpad.net/gnuflag"
15
16 "launchpad.net/juju-core/cmd"
17 "launchpad.net/juju-core/cmd/envcmd"
18 "launchpad.net/juju-core/environs"
19+ "launchpad.net/juju-core/environs/configstore"
20+ "launchpad.net/juju-core/juju"
21 )
22
23 type SwitchCommand struct {
24 cmd.CommandBase
25+ out cmd.Output
26 EnvName string
27 List bool
28 }
29@@ -31,8 +34,63 @@
30 If a command line parameter is passed in, that value will is stored in the
31 current environment file if it represents a valid environment name as
32 specified in the environments.yaml file.
33+
34+Use the --format flag to print out information about the environment. You
35+need to specify a YAML or JSON format: "--format yaml" or "--format json".
36+
37+Examples:
38+
39+# Show current environment name
40+$ juju switch
41+local
42+
43+# Switch to the ec2 environment. Show the environment you are switching
44+# from and to i.e. "oldEnv -> newEnv"
45+$ juju switch ec2
46+local -> ec2
47+
48+# Show username, API endpoints and environment name
49+$ juju switch --format yaml
50+environ-name: local
51+state-servers:
52+ - example.com
53+ - kremvax.ru
54+ cacert: cert
55+username: joe
56+
57+# Show username, API endpoints and environment name
58+$ juju switch -e # Single character option, same as above
59+environ-name: local
60+state-servers:
61+ - example.com
62+ - kremvax.ru
63+username: joe
64+
65+# Show infomation for the environment you are switching to. Include
66+# the environment you are switching from in the 'previous-environ-name' field.
67+$ juju switch local --format yaml
68+environ-name: local
69+previous-environ-name: ec2
70+state-servers:
71+ - example.com
72+ - kremvax.ru
73+ cacert: cert
74+username: joe
75+
76+# Format environment information as json
77+$ juju switch --format json
78+{ "state-servers": ["example.com","kremvax.ru"],
79+"environ-name":"local",
80+"username":"joe" }
81 `
82
83+type EnvInfo struct {
84+ Username string `yaml:"user-name" json:"user-name"`
85+ EnvironName string `yaml:"environ-name" json:"environ-name"`
86+ PreviousEnvironName string `yaml:"previous-environ-name,omitempty" json:"previous-environ-name,omitempty"`
87+ StateServers []string `json:"state-servers" yaml:"state-servers"`
88+}
89+
90 func (c *SwitchCommand) Info() *cmd.Info {
91 return &cmd.Info{
92 Name: "switch",
93@@ -46,6 +104,11 @@
94 func (c *SwitchCommand) SetFlags(f *gnuflag.FlagSet) {
95 f.BoolVar(&c.List, "l", false, "list the environment names")
96 f.BoolVar(&c.List, "list", false, "")
97+ c.out.AddFlags(f, "simple", map[string]cmd.Formatter{
98+ "simple": c.formatSimple,
99+ "yaml": cmd.FormatYaml,
100+ "json": cmd.FormatJson,
101+ })
102 }
103
104 func (c *SwitchCommand) Init(args []string) (err error) {
105@@ -62,7 +125,7 @@
106 return false
107 }
108
109-func (c *SwitchCommand) Run(ctx *cmd.Context) error {
110+func (c *SwitchCommand) Run(ctx *cmd.Context) (err error) {
111 // Switch is an alternative way of dealing with environments than using
112 // the JUJU_ENV environment setting, and as such, doesn't play too well.
113 // If JUJU_ENV is set we should report that as the current environment,
114@@ -77,7 +140,6 @@
115 sort.Strings(names)
116
117 if c.List {
118- // List all environments.
119 if c.EnvName != "" {
120 return errors.New("cannot switch and list at the same time")
121 }
122@@ -87,42 +149,112 @@
123 return nil
124 }
125
126+ var info EnvInfo
127+
128 jujuEnv := os.Getenv("JUJU_ENV")
129 if jujuEnv != "" {
130 if c.EnvName == "" {
131- fmt.Fprintf(ctx.Stdout, "%s\n", jujuEnv)
132- return nil
133+ if info, err = c.envInfo(ctx, jujuEnv, ""); err != nil {
134+ return err
135+ }
136 } else {
137 return fmt.Errorf("cannot switch when JUJU_ENV is overriding the environment (set to %q)", jujuEnv)
138 }
139- }
140-
141- currentEnv := envcmd.ReadCurrentEnvironment()
142- if currentEnv == "" {
143- currentEnv = environments.Default
144- }
145-
146- // Handle the different operation modes.
147- switch {
148- case c.EnvName == "" && currentEnv == "":
149- // Nothing specified and nothing to switch to.
150- return errors.New("no currently specified environment")
151- case c.EnvName == "":
152- // Simply print the current environment.
153- fmt.Fprintf(ctx.Stdout, "%s\n", currentEnv)
154- default:
155- // Switch the environment.
156- if !validEnvironmentName(c.EnvName, names) {
157- return fmt.Errorf("%q is not a name of an existing defined environment", c.EnvName)
158- }
159- if err := envcmd.WriteCurrentEnvironment(c.EnvName); err != nil {
160- return err
161- }
162+ } else {
163+ currentEnv := envcmd.ReadCurrentEnvironment()
164 if currentEnv == "" {
165- fmt.Fprintf(ctx.Stdout, "-> %s\n", c.EnvName)
166- } else {
167- fmt.Fprintf(ctx.Stdout, "%s -> %s\n", currentEnv, c.EnvName)
168- }
169- }
170+ currentEnv = environments.Default
171+ }
172+
173+ // Handle the different operation modes.
174+ switch {
175+ case c.EnvName == "" && currentEnv == "":
176+ // Nothing specified and nothing to switch to.
177+ return errors.New("no currently specified environment")
178+ case c.EnvName == "":
179+ // Simply print the current environment.
180+ if info, err = c.envInfo(ctx, currentEnv, ""); err != nil {
181+ return err
182+ }
183+ default:
184+ // Switch the environment.
185+ if !validEnvironmentName(c.EnvName, names) {
186+ return fmt.Errorf("%q is not a name of an existing defined environment", c.EnvName)
187+ }
188+ if err := envcmd.WriteCurrentEnvironment(c.EnvName); err != nil {
189+ return err
190+ }
191+ if info, err = c.envInfo(ctx, c.EnvName, currentEnv); err != nil {
192+ return err
193+ }
194+ }
195+ }
196+
197+ if err = c.out.Write(ctx, info); err != nil {
198+ return err
199+ }
200+
201 return nil
202 }
203+
204+// envInfo builds and returns an EnvInfo
205+func (c *SwitchCommand) envInfo(ctx *cmd.Context, envName string, oldEnvName string) (info EnvInfo, err error) {
206+ info.EnvironName = envName
207+ info.PreviousEnvironName = oldEnvName
208+ if info.Username, err = user(envName); err != nil {
209+ return EnvInfo{}, err
210+ }
211+ if info.StateServers, err = stateServers(envName); err != nil {
212+ return EnvInfo{}, err
213+ }
214+ return info, nil
215+}
216+
217+// user returns the juju user for the given environment name, envName.
218+func user(envName string) (username string, err error) {
219+ store, err := configstore.Default()
220+ if err != nil {
221+ return "", err
222+ }
223+ info, err := store.ReadInfo(envName)
224+ if err != nil {
225+ if !errors.IsNotFound(err) {
226+ return "", err
227+ }
228+ } else {
229+ username = info.APICredentials().User
230+ }
231+ return username, nil
232+}
233+
234+func stateServers(envName string) (addresses []string, err error) {
235+ apiEndpoint, err := juju.APIEndpointForEnv(envName, false)
236+ if err != nil {
237+ if !errors.IsNotFound(err) {
238+ return nil, err
239+ }
240+ } else {
241+ addresses = apiEndpoint.Addresses
242+ }
243+ return addresses, nil
244+}
245+
246+func (c *SwitchCommand) formatSimple(value interface{}) (output []byte, err error) {
247+ if info, ok := value.(EnvInfo); ok {
248+ var msg string
249+ if info.PreviousEnvironName != "" {
250+ msg = fmt.Sprintf("%s -> %s", info.PreviousEnvironName, info.EnvironName)
251+ } else {
252+ var fmtStr string
253+ if c.EnvName != "" {
254+ fmtStr = "-> %s"
255+ } else {
256+ fmtStr = "%s"
257+ }
258+ msg = fmt.Sprintf(fmtStr, info.EnvironName)
259+ }
260+ output = []byte(msg)
261+ return output, nil
262+ }
263+ return nil, err
264+}
265
266=== modified file 'cmd/juju/switch_test.go'
267--- cmd/juju/switch_test.go 2014-05-22 06:03:06 +0000
268+++ cmd/juju/switch_test.go 2014-06-03 00:37:45 +0000
269@@ -4,11 +4,14 @@
270 package main
271
272 import (
273+ "encoding/json"
274 "os"
275
276 gc "launchpad.net/gocheck"
277+ "launchpad.net/goyaml"
278
279 "launchpad.net/juju-core/cmd/envcmd"
280+ "launchpad.net/juju-core/environs/configstore"
281 _ "launchpad.net/juju-core/juju"
282 "launchpad.net/juju-core/testing"
283 )
284@@ -19,6 +22,29 @@
285
286 var _ = gc.Suite(&SwitchSimpleSuite{})
287
288+var testCreds = configstore.APICredentials{
289+ User: "joe",
290+ Password: "baloney",
291+}
292+
293+var apiEndpoint = configstore.APIEndpoint{
294+ Addresses: []string{"example.com", "kremvax.ru"},
295+ CACert: "cert",
296+}
297+
298+func patchEnvWithUser(c *gc.C, envName string) {
299+ testing.WriteEnvironments(c, testing.MultipleEnvConfig)
300+
301+ // Patch APICredentials so that <envName>.jenv file is avaliable for
302+ // switch to read the envirionment user from.
303+ store, err := configstore.Default()
304+ c.Assert(err, gc.IsNil)
305+ info, err := store.CreateInfo(envName)
306+ info.SetAPIEndpoint(apiEndpoint)
307+ info.SetAPICredentials(testCreds)
308+ info.Write()
309+}
310+
311 func (*SwitchSimpleSuite) TestNoEnvironment(c *gc.C) {
312 envPath := testing.HomePath(".juju", "environments.yaml")
313 err := os.Remove(envPath)
314@@ -111,6 +137,49 @@
315 c.Assert(err, gc.ErrorMatches, "cannot switch and list at the same time")
316 }
317
318+func (*SwitchSimpleSuite) TestListEnvironmentInfoYaml(c *gc.C) {
319+ patchEnvWithUser(c, "erewhemos")
320+ context, err := testing.RunCommand(c, &SwitchCommand{}, "--format", "yaml")
321+ c.Assert(err, gc.IsNil)
322+ output := EnvInfo{}
323+ goyaml.Unmarshal([]byte(testing.Stdout(context)), &output)
324+ expected := EnvInfo{
325+ Username: "joe",
326+ EnvironName: "erewhemos",
327+ StateServers: []string{"example.com", "kremvax.ru"},
328+ }
329+ c.Assert(output, gc.DeepEquals, expected)
330+}
331+
332+func (*SwitchSimpleSuite) TestListEnvironmentInfoJson(c *gc.C) {
333+ patchEnvWithUser(c, "erewhemos")
334+ context, err := testing.RunCommand(c, &SwitchCommand{}, "--format", "json")
335+ c.Assert(err, gc.IsNil)
336+ output := EnvInfo{}
337+ json.Unmarshal([]byte(testing.Stdout(context)), &output)
338+ expected := EnvInfo{
339+ Username: "joe",
340+ EnvironName: "erewhemos",
341+ StateServers: []string{"example.com", "kremvax.ru"},
342+ }
343+ c.Assert(output, gc.DeepEquals, expected)
344+}
345+
346+func (*SwitchSimpleSuite) TestListEnvironmentInfoShowOldEnv(c *gc.C) {
347+ patchEnvWithUser(c, "erewhemos-2")
348+ context, err := testing.RunCommand(c, &SwitchCommand{}, "erewhemos-2", "--format", "json")
349+ c.Assert(err, gc.IsNil)
350+ output := EnvInfo{}
351+ json.Unmarshal([]byte(testing.Stdout(context)), &output)
352+ expected := EnvInfo{
353+ Username: "joe",
354+ EnvironName: "erewhemos-2",
355+ PreviousEnvironName: "erewhemos",
356+ StateServers: []string{"example.com", "kremvax.ru"},
357+ }
358+ c.Assert(output, gc.DeepEquals, expected)
359+}
360+
361 func (*SwitchSimpleSuite) TestTooManyParams(c *gc.C) {
362 testing.WriteEnvironments(c, testing.MultipleEnvConfig)
363 _, err := testing.RunCommand(c, &SwitchCommand{}, "foo", "bar")

Subscribers

People subscribed via source and target branches

to status/vote changes: