Skip to content
This repository has been archived by the owner on Jun 5, 2018. It is now read-only.

Commit

Permalink
Update to handle new terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
calmh committed May 16, 2017
1 parent 35bb114 commit c04d5a6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
8 changes: 5 additions & 3 deletions completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package completion

import (
"fmt"
"golang.org/x/crypto/ssh/terminal"
"io"
"regexp"
"strings"

"golang.org/x/crypto/ssh/terminal"
)

// A Completer provides line completion functionality based on Matchers.
Expand Down Expand Up @@ -36,10 +38,10 @@ func (c Completer) Complete(line string, pos int) (head string, comps []Word, ta
return head, aggrMatch(matchers, words[len(words)-1]), tail
}

func (c Completer) PrintHelp(esc *terminal.EscapeCodes) {
func (c Completer) PrintHelp(out io.Writer, esc *terminal.EscapeCodes) {
for _, m := range c.matchers {
for _, l := range m.Help(esc) {
fmt.Println(l)
fmt.Fprintln(out, l)
}
}
}
Expand Down
53 changes: 27 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"net"
"os"
"strings"
Expand Down Expand Up @@ -85,7 +86,7 @@ func main() {

h, w, err := terminal.GetSize(0)
if err != nil {
fmt.Println(err)
fmt.Fprintln(term, err)
return
}
term.SetSize(h, w)
Expand All @@ -95,26 +96,26 @@ func main() {
term.SetPrompt("Username: ")
user, err = term.ReadLine()
if err != nil {
fmt.Println(err)
fmt.Fprintln(term, err)
return
}
pass, err := term.ReadPassword("Password: ")
if err != nil {
fmt.Println(err)
fmt.Fprintln(term, err)
return
}
res, err = conn.run(command{Method: "system.login", Params: []interface{}{user, pass}})
if err != nil {
fmt.Println(err)
fmt.Fprintln(term, err)
return
}
if res.Error.Code != 0 {
fmt.Println(res.Error.Message)
fmt.Println()
fmt.Fprintln(term, res.Error.Message)
fmt.Fprintln(term)
} else {
res, err = conn.run(command{Method: "system.version"})
if err != nil {
fmt.Println(err)
fmt.Fprintln(term, err)
return
}
}
Expand All @@ -129,19 +130,19 @@ func main() {

res, err = conn.run(command{Method: "system.hostname"})
if err != nil {
fmt.Println(err)
fmt.Fprintln(term, err)
return
}
hostname, ok := res.Result.(string)
if !ok {
hostname = "(unknown)"
}

fmt.Println("PSM version", version, "at", hostname)
fmt.Fprintln(term, "PSM version", version, "at", hostname)

res, err = conn.run(command{Method: "model.isReadOnly"})
if err != nil {
fmt.Println(err)
fmt.Fprintln(term, err)
return
}

Expand All @@ -157,13 +158,13 @@ func main() {
hostname = hostnameParts[0]
term.SetPrompt(user + "@" + hostname + roRw)

fmt.Println()
fmt.Fprintln(term)

// Set up tab completion based on announced commands and parameters

smd, err := conn.smd()
if err != nil {
fmt.Println(err)
fmt.Fprintln(term, err)
os.Exit(1)
}

Expand All @@ -186,17 +187,17 @@ func main() {
}

if line == "help" || line == "?" {
printHelp(term.Escape)
printHelp(term, term.Escape)
continue
}
if line == "commands" {
completer.PrintHelp(term.Escape)
completer.PrintHelp(term, term.Escape)
continue
}

cmd, err := parseCommand(line)
if err != nil {
fmt.Println(err)
fmt.Fprintln(term, err)
continue
}

Expand All @@ -206,18 +207,18 @@ func main() {
if *verbose {
// Print the command locally
bs, _ := json.Marshal(cmd)
fmt.Printf("> %s", bs)
fmt.Fprintf(term, "> %s\n", bs)
}

// Execute command on PSM

res, err := conn.run(cmd)
if err != nil {
fmt.Println(err)
fmt.Fprintln(term, err)
return
}

printResponse(res)
printResponse(term, res)
}
}

Expand All @@ -228,34 +229,34 @@ func usage() {
fmt.Println(" psmcli [-v] <host:port>")
}

func printResponse(res response) {
func printResponse(out io.Writer, res response) {
if res.Error.Code != 0 {
fmt.Printf("Error %d: %s", res.Error.Code, res.Error.Message)
fmt.Fprintf(out, "Error %d: %s", res.Error.Code, res.Error.Message)
} else if res.Result != nil {
switch result := res.Result.(type) {
case []interface{}:
for _, res := range result {
switch res := res.(type) {
case string, int, json.Number:
fmt.Println(res)
fmt.Fprintln(out, res)
default:
bs, _ := json.MarshalIndent(res, "", " ")
fmt.Printf("%s\n\n", bs)
fmt.Fprintf(out, "%s\n\n", bs)
}
}

case map[string]interface{}:
bs, _ := json.MarshalIndent(result, "", " ")
fmt.Printf("%s\n\n", bs)
fmt.Fprintf(out, "%s\n\n", bs)

default:
fmt.Println(result)
fmt.Fprintln(out, result)
}
}
}

func printHelp(esc *terminal.EscapeCodes) {
fmt.Println(`Usage:
func printHelp(out io.Writer, esc *terminal.EscapeCodes) {
fmt.Fprintln(out, `Usage:
help, ?:
Print this help
Expand Down

0 comments on commit c04d5a6

Please sign in to comment.