Skip to content

Commit

Permalink
Add command to request a VNC console (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
minus7 committed Jul 13, 2020
1 parent 50a7de3 commit 5d6f1bb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func newServerCommand(cli *CLI) *cobra.Command {
newServerDetachFromNetworkCommand(cli),
newServerChangeAliasIPsCommand(cli),
newServerIPCommand(cli),
newServerRequestConsoleCommand(cli),
)
return cmd
}
Expand Down
57 changes: 57 additions & 0 deletions cli/server_request_console.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"
)

func newServerRequestConsoleCommand(cli *CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "request-console [FLAGS] SERVER",
Short: "Request a WebSocket VNC console for a server",
Args: cobra.ExactArgs(1),
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: cli.ensureToken,
RunE: cli.wrap(runServerRequestConsole),
}
addOutputFlag(cmd, outputOptionJSON())
return cmd
}

func runServerRequestConsole(cli *CLI, cmd *cobra.Command, args []string) error {
outOpts := outputFlagsForCommand(cmd)
idOrName := args[0]
server, _, err := cli.Client().Server.Get(cli.Context, idOrName)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("server not found: %s", idOrName)
}

result, _, err := cli.Client().Server.RequestConsole(cli.Context, server)
if err != nil {
return err
}

if err := cli.ActionProgress(cli.Context, result.Action); err != nil {
return err
}

if outOpts.IsSet("json") {
return describeJSON(struct {
WSSURL string
Password string
}{
WSSURL: result.WSSURL,
Password: result.Password,
})
}

fmt.Printf("Console for server %d:\n", server.ID)
fmt.Printf("WebSocket URL: %s\n", result.WSSURL)
fmt.Printf("VNC Password: %s\n", result.Password)
return nil
}

0 comments on commit 5d6f1bb

Please sign in to comment.