Skip to content

Commit

Permalink
Merge pull request #44 from liquidweb/windows-fixes
Browse files Browse the repository at this point in the history
Windows improvements
  • Loading branch information
sgsullivan committed Jun 24, 2020
2 parents 334f2fa + 33f47ac commit c4f6e49
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
3 changes: 2 additions & 1 deletion cmd/authInit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os"
"strings"
"syscall"

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

Expand Down Expand Up @@ -141,7 +142,7 @@ func fetchAuthDataInteractively() (writeConfig bool, err error) {
// password
for !havePasswordAnswer {
fmt.Print("LiquidWeb password: ")
passwordBytes, err := terminal.ReadPassword(0)
passwordBytes, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
userInputError <- err
break WHILEMOREADDS
Expand Down
31 changes: 16 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ limitations under the License.
package cmd

import (
"bufio"
"fmt"
"os"
"strings"

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

for !haveConfirmationAnswer {
utils.PrintRed("This is a destructive operation. Continue (yes/[no])?: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
answer := scanner.Text()

if answer != "" && answer != "yes" && answer != "no" {
utils.PrintYellow("invalid input.\n")
continue
f := func(d prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{
{Text: "yes", Description: "I understand continue"},
{Text: "no", Description: "I would like to cancel"},
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}

haveConfirmationAnswer = true
if answer == "no" || answer == "" {
proceed = false
} else if answer == "yes" {
proceed = true
for !haveConfirmationAnswer {
utils.PrintRed("This is a destructive operation. Continue? ")
answer := strings.ToLower(prompt.Input("> ", f, prompt.OptionShowCompletionAtStart()))
if answer == "yes" || answer == "no" {
haveConfirmationAnswer = true
if answer == "yes" {
proceed = true
}
}
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.13
require (
github.com/c-bata/go-prompt v0.2.3
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
github.com/liquidweb/go-lwApi v0.0.0-20190605172801-52a4864d2738
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 h1:qGQQKEcAR99REcMpsXCp3lJ03zYT1PkRd3kQGPn9GVg=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down
26 changes: 20 additions & 6 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"net"
"os"
"time"

"github.com/k0kubun/go-ansi"
)

func IpIsValid(ip string) bool {
Expand Down Expand Up @@ -54,32 +56,44 @@ func FileExists(file string) bool {

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

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

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

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

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

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

// private
Expand Down

0 comments on commit c4f6e49

Please sign in to comment.