/
switch.go
47 lines (43 loc) · 1.17 KB
/
switch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package org
import (
"errors"
"github.com/appcelerator/amp/api/rpc/account"
"github.com/appcelerator/amp/cli"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
// NewSwitchCommand returns a new instance of the switch command.
func NewSwitchCommand(c cli.Interface) *cobra.Command {
return &cobra.Command{
Use: "switch ACCOUNT",
Short: "Switch account",
PreRunE: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return switch_(c, args)
},
}
}
func switch_(c cli.Interface, args []string) error {
conn := c.ClientConn()
client := account.NewAccountClient(conn)
request := &account.SwitchRequest{
Account: args[0],
}
headers := metadata.MD{}
_, err := client.Switch(context.Background(), request, grpc.Header(&headers))
if err != nil {
if s, ok := status.FromError(err); ok {
return errors.New(s.Message())
}
}
if err := cli.SaveToken(headers, c.Server()); err != nil {
if s, ok := status.FromError(err); ok {
return errors.New(s.Message())
}
}
c.Console().Printf("You are now logged in as: %s\n", args[0])
return nil
}