Skip to content

Commit c4f6e49

Browse files
authored
Merge pull request #44 from liquidweb/windows-fixes
Windows improvements
2 parents 334f2fa + 33f47ac commit c4f6e49

File tree

5 files changed

+41
-22
lines changed

5 files changed

+41
-22
lines changed

cmd/authInit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"os"
2222
"strings"
23+
"syscall"
2324

2425
"golang.org/x/crypto/ssh/terminal"
2526

@@ -141,7 +142,7 @@ func fetchAuthDataInteractively() (writeConfig bool, err error) {
141142
// password
142143
for !havePasswordAnswer {
143144
fmt.Print("LiquidWeb password: ")
144-
passwordBytes, err := terminal.ReadPassword(0)
145+
passwordBytes, err := terminal.ReadPassword(int(syscall.Stdin))
145146
if err != nil {
146147
userInputError <- err
147148
break WHILEMOREADDS

cmd/root.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ limitations under the License.
1616
package cmd
1717

1818
import (
19-
"bufio"
2019
"fmt"
2120
"os"
21+
"strings"
2222

23+
"github.com/c-bata/go-prompt"
2324
homedir "github.com/mitchellh/go-homedir"
2425
"github.com/spf13/cobra"
2526
"github.com/spf13/viper"
@@ -110,22 +111,22 @@ func dialogDesctructiveConfirmProceed() (proceed bool) {
110111
var haveConfirmationAnswer bool
111112
utils.PrintTeal("Tip: Avoid future confirmations by passing --force\n\n")
112113

113-
for !haveConfirmationAnswer {
114-
utils.PrintRed("This is a destructive operation. Continue (yes/[no])?: ")
115-
scanner := bufio.NewScanner(os.Stdin)
116-
scanner.Scan()
117-
answer := scanner.Text()
118-
119-
if answer != "" && answer != "yes" && answer != "no" {
120-
utils.PrintYellow("invalid input.\n")
121-
continue
114+
f := func(d prompt.Document) []prompt.Suggest {
115+
s := []prompt.Suggest{
116+
{Text: "yes", Description: "I understand continue"},
117+
{Text: "no", Description: "I would like to cancel"},
122118
}
119+
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
120+
}
123121

124-
haveConfirmationAnswer = true
125-
if answer == "no" || answer == "" {
126-
proceed = false
127-
} else if answer == "yes" {
128-
proceed = true
122+
for !haveConfirmationAnswer {
123+
utils.PrintRed("This is a destructive operation. Continue? ")
124+
answer := strings.ToLower(prompt.Input("> ", f, prompt.OptionShowCompletionAtStart()))
125+
if answer == "yes" || answer == "no" {
126+
haveConfirmationAnswer = true
127+
if answer == "yes" {
128+
proceed = true
129+
}
129130
}
130131
}
131132

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.13
55
require (
66
github.com/c-bata/go-prompt v0.2.3
77
github.com/fsnotify/fsnotify v1.4.9 // indirect
8+
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
89
github.com/liquidweb/go-lwApi v0.0.0-20190605172801-52a4864d2738
910
github.com/mattn/go-colorable v0.1.6 // indirect
1011
github.com/mattn/go-runewidth v0.0.9 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22
5757
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
5858
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
5959
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
60+
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 h1:qGQQKEcAR99REcMpsXCp3lJ03zYT1PkRd3kQGPn9GVg=
61+
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
6062
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
6163
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
6264
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=

utils/utils.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"net"
2222
"os"
2323
"time"
24+
25+
"github.com/k0kubun/go-ansi"
2426
)
2527

2628
func IpIsValid(ip string) bool {
@@ -54,32 +56,44 @@ func FileExists(file string) bool {
5456

5557
func PrintRed(m string, args ...interface{}) {
5658
msg := fmt.Sprintf(m, args...)
57-
fmt.Printf(red(msg))
59+
if _, err := ansi.Print(red(msg)); err != nil {
60+
fmt.Printf("Error printing to console. Error was [%s] original message: [%s]\n", err, msg)
61+
}
5862
}
5963

6064
func PrintTeal(m string, args ...interface{}) {
6165
msg := fmt.Sprintf(m, args...)
62-
fmt.Printf(teal(msg))
66+
if _, err := ansi.Print(teal(msg)); err != nil {
67+
fmt.Printf("Error printing to console. Error was [%s] original message: [%s]\n", err, msg)
68+
}
6369
}
6470

6571
func PrintGreen(m string, args ...interface{}) {
6672
msg := fmt.Sprintf(m, args...)
67-
fmt.Printf(green(msg))
73+
if _, err := ansi.Print(green(msg)); err != nil {
74+
fmt.Printf("Error printing to console. Error was [%s] original message: [%s]\n", err, msg)
75+
}
6876
}
6977

7078
func PrintYellow(m string, args ...interface{}) {
7179
msg := fmt.Sprintf(m, args...)
72-
fmt.Printf(yellow(msg))
80+
if _, err := ansi.Print(yellow(msg)); err != nil {
81+
fmt.Printf("Error printing to console. Error was [%s] original message: [%s]\n", err, msg)
82+
}
7383
}
7484

7585
func PrintMagenta(m string, args ...interface{}) {
7686
msg := fmt.Sprintf(m, args...)
77-
fmt.Printf(magenta(msg))
87+
if _, err := ansi.Print(magenta(msg)); err != nil {
88+
fmt.Printf("Error printing to console. Error was [%s] original message: [%s]\n", err, msg)
89+
}
7890
}
7991

8092
func PrintPurple(m string, args ...interface{}) {
8193
msg := fmt.Sprintf(m, args...)
82-
fmt.Printf(purple(msg))
94+
if _, err := ansi.Print(purple(msg)); err != nil {
95+
fmt.Printf("Error printing to console. Error was [%s] original message: [%s]\n", err, msg)
96+
}
8397
}
8498

8599
// private

0 commit comments

Comments
 (0)