forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh_code.go
81 lines (65 loc) · 2.25 KB
/
ssh_code.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package commands
import (
"errors"
"github.com/cloudfoundry/cli/cf/api"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/flags"
"github.com/cloudfoundry/cli/cf/api/authentication"
"github.com/cloudfoundry/cli/cf/command_registry"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
)
//go:generate counterfeiter -o fakes/fake_ssh_code_getter.go . SSHCodeGetter
type SSHCodeGetter interface {
command_registry.Command
Get() (string, error)
}
type OneTimeSSHCode struct {
ui terminal.UI
config core_config.ReadWriter
authRepo authentication.AuthenticationRepository
endpointRepo api.EndpointRepository
}
func init() {
command_registry.Register(&OneTimeSSHCode{})
}
func (cmd *OneTimeSSHCode) MetaData() command_registry.CommandMetadata {
return command_registry.CommandMetadata{
Name: "ssh-code",
Description: T("Get a one time password for ssh clients"),
Usage: T("CF_NAME ssh-code"),
}
}
func (cmd *OneTimeSSHCode) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
if len(fc.Args()) != 0 {
cmd.ui.Failed(T("Incorrect Usage. No argument required\n\n") + command_registry.Commands.CommandUsage("ssh-code"))
}
reqs := append([]requirements.Requirement{}, requirementsFactory.NewApiEndpointRequirement())
return reqs, nil
}
func (cmd *OneTimeSSHCode) SetDependency(deps command_registry.Dependency, _ bool) command_registry.Command {
cmd.ui = deps.Ui
cmd.config = deps.Config
cmd.authRepo = deps.RepoLocator.GetAuthenticationRepository()
cmd.endpointRepo = deps.RepoLocator.GetEndpointRepository()
return cmd
}
func (cmd *OneTimeSSHCode) Execute(c flags.FlagContext) {
code, err := cmd.Get()
if err != nil {
cmd.ui.Failed(err.Error())
}
cmd.ui.Say(code)
}
func (cmd *OneTimeSSHCode) Get() (string, error) {
_, err := cmd.endpointRepo.UpdateEndpoint(cmd.config.ApiEndpoint())
if err != nil {
return "", errors.New(T("Error getting info from v2/info: ") + err.Error())
}
token, err := cmd.authRepo.RefreshAuthToken()
if err != nil {
return "", errors.New(T("Error refreshing oauth token: ") + err.Error())
}
return token, nil
}