Skip to content

Commit

Permalink
Adds command to get token only
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Jun 3, 2022
1 parent 995f2aa commit ea2d610
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
13 changes: 13 additions & 0 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ var getProjectKeyCmd = &cobra.Command{
},
}

var getToken = &cobra.Command{
Use: "token",
Aliases: []string{"tk"},
Short: "Gets and prints a bearer token",
Run: func(cmd *cobra.Command, args []string) {
token, err := retrieveTokenViaSsh()
handleError(err)
output.RenderInfo(string(token), outputOptions)

},
}

func init() {
getCmd.AddCommand(getAllUserKeysCmd)
getCmd.AddCommand(getDeploymentCmd)
Expand All @@ -163,6 +175,7 @@ func init() {
getCmd.AddCommand(getProjectKeyCmd)
getCmd.AddCommand(getUserKeysCmd)
getCmd.AddCommand(getTaskByID)
getCmd.AddCommand(getToken)
getTaskByID.Flags().IntP("id", "I", 0, "ID of the task")
getTaskByID.Flags().BoolP("logs", "L", false, "Show the task logs if available")
getProjectKeyCmd.Flags().BoolVarP(&revealValue, "reveal", "", false, "Reveal the variable values")
Expand Down
32 changes: 20 additions & 12 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ func publicKey(path string, skipAgent bool) (ssh.AuthMethod, func() error) {
}

func loginToken() error {
out, err := retrieveTokenViaSsh()
if err != nil {
return err
}

lc := lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current]
lc.Token = strings.TrimSpace(string(out))
lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current] = lc
if err = writeLagoonConfig(&lagoonCLIConfig, filepath.Join(configFilePath, configName+configExtension)); err != nil {
return fmt.Errorf("couldn't write config: %v", err)
}

return nil
}

func retrieveTokenViaSsh() ([]byte, error) {
skipAgent := false
privateKey := fmt.Sprintf("%s/.ssh/id_rsa", userPath)
if cmdSSHKey != "" {
Expand All @@ -86,26 +102,18 @@ func loginToken() error {
lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current].Port)
conn, err := ssh.Dial("tcp", sshHost, config)
if err != nil {
return fmt.Errorf("couldn't connect to %s: %v", sshHost, err)
return nil, fmt.Errorf("couldn't connect to %s: %v", sshHost, err)
}
defer conn.Close()

session, err := conn.NewSession()
if err != nil {
return fmt.Errorf("couldn't open session: %v", err)
return nil, fmt.Errorf("couldn't open session: %v", err)
}

out, err := session.CombinedOutput("token")
if err != nil {
return fmt.Errorf("couldn't get token: %v", err)
return nil, fmt.Errorf("couldn't get token: %v", err)
}

lc := lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current]
lc.Token = strings.TrimSpace(string(out))
lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current] = lc
if err = writeLagoonConfig(&lagoonCLIConfig, filepath.Join(configFilePath, configName+configExtension)); err != nil {
return fmt.Errorf("couldn't write config: %v", err)
}

return nil
return out, err
}

0 comments on commit ea2d610

Please sign in to comment.