Skip to content

Commit

Permalink
chore: cleanup utils, cmd was the only package using it
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed Jun 4, 2019
1 parent 2d86bb3 commit 21f6e2f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 130 deletions.
5 changes: 2 additions & 3 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"sync"

"github.com/aeternity/aepp-sdk-go/aeternity"
"github.com/aeternity/aepp-sdk-go/utils"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -58,7 +57,7 @@ func getPassword() (p string, err error) {
if len(password) != 0 {
return password, nil
}
p, err = utils.AskPassword("Enter the password to unlock the keystore: ")
p, err = AskPassword("Enter the password to unlock the keystore: ")
if err != nil {
return "", err
}
Expand All @@ -79,7 +78,7 @@ func addressFunc(cmd *cobra.Command, args []string) error {

aeternity.Pp("Account address", account.Address)
if printPrivateKey {
if utils.AskYes("Are you sure you want to print your private key? This could be insecure.", false) {
if AskYes("Are you sure you want to print your private key? This could be insecure.", false) {
aeternity.Pp("Account private key", account.SigningKeyToHexString())
}
}
Expand Down
56 changes: 56 additions & 0 deletions cmd/text_functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

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

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

// IsEqStr tells if two strings a and b are equals after trimming spaces and lowercasing
func IsEqStr(a, b string) bool {
return strings.ToLower(strings.TrimSpace(a)) == strings.ToLower(strings.TrimSpace(b))
}

// IsEmptyStr tells if a string is empty or not
func IsEmptyStr(s string) bool {
return len(strings.TrimSpace(s)) == 0
}

// DefaultIfEmptyStr set a default for a string if it is nulled
func DefaultIfEmptyStr(s *string, defaultS string) {
if IsEmptyStr(*s) {
*s = defaultS
}
}

// AskYes prompt a yes/no question to the prompt
func AskYes(question string, defaultYes bool) (isYes bool) {
fmt.Print(question)
if defaultYes {
fmt.Print(" [yes]: ")
} else {
fmt.Print(" [no]: ")
}
reader := bufio.NewReader(os.Stdin)
reply, _ := reader.ReadString('\n')
DefaultIfEmptyStr(&reply, "yes")
if IsEqStr(reply, "yes") {
return true
}
return
}

// AskPassword ask a password
func AskPassword(question string) (password string, err error) {
fmt.Println(question)
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return
}
password = string(bytePassword)
return
}
127 changes: 0 additions & 127 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,140 +1,13 @@
package utils

import (
"bufio"
"errors"
"fmt"
"math/big"
"os"
"strconv"
"strings"
"syscall"

"github.com/go-openapi/strfmt"
gonanoid "github.com/matoous/go-nanoid"
"golang.org/x/crypto/ssh/terminal"
)

const (
// DefaultAlphabet default alphabet for string generation
DefaultAlphabet = "asdfghjklqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"
)

// IsEqStr tells if two strings a and b are equals after trimming spaces and lowercasing
func IsEqStr(a, b string) bool {
return strings.ToLower(strings.TrimSpace(a)) == strings.ToLower(strings.TrimSpace(b))
}

// IsEmptyStr tells if a string is empty or not
func IsEmptyStr(s string) bool {
return len(strings.TrimSpace(s)) == 0
}

// DefaultIfEmptyStr set a default for a string if it is nulled
func DefaultIfEmptyStr(s *string, defaultS string) {
if IsEmptyStr(*s) {
*s = defaultS
}
}

// DefaultIfEmptyInt set the value of an int to a default if it is nulled (0)
func DefaultIfEmptyInt(v *int, defaultV int) {
if *v <= 0 {
*v = defaultV
}
}

// DefaultIfEmptyInt64 set the value of an int to a default if it is nulled (0)
func DefaultIfEmptyInt64(v *int64, defaultV int64) {
if *v <= 0 {
*v = defaultV
}
}

// DefaultIfEmptyUint8 set the value of an int to a default if it is nulled (0)
func DefaultIfEmptyUint8(v *uint8, defaultV uint8) {
if *v <= 0 {
*v = defaultV
}
}

// DefaultIfEmptyUint32 set the value of an int to a default if it is nulled (0)
func DefaultIfEmptyUint32(v *uint32, defaultV uint32) {
if *v <= 0 {
*v = defaultV
}
}

// DefaultIfEmptyUint64 set the value of an int to a default if it is nulled (0)
func DefaultIfEmptyUint64(v *uint64, defaultV uint64) {
if *v <= 0 {
*v = defaultV
}
}

// RandomString generate a random string of required lenght an with requested alphabet
func RandomString(alphabet string, length int) (s string, err error) {
if IsEmptyStr(alphabet) {
err = fmt.Errorf("alphabet must not be empty")
return
}
if length <= 0 {
err = fmt.Errorf("string length must be longer than 0")
return
}
return gonanoid.Generate(alphabet, length)
}

// RandomStringL generate a string that can be used as secrete api key
func RandomStringL(l int) string {
secret, _ := RandomString(DefaultAlphabet, l)
return secret
}

// IsInt64 check if a string is a int64
func IsInt64(str string) (isInt bool, val int64) {
if v, err := strconv.Atoi(str); err == nil {
val = int64(v)
isInt = true
}
return
}

// IsPositiveInt64 check if a string is a positive integer
func IsPositiveInt64(str string) (isInt bool, val int64) {
isInt, val = IsInt64(str)
isInt = val > 0
return
}

// AskYes prompt a yes/no question to the prompt
func AskYes(question string, defaultYes bool) (isYes bool) {
fmt.Print(question)
if defaultYes {
fmt.Print(" [yes]: ")
} else {
fmt.Print(" [no]: ")
}
reader := bufio.NewReader(os.Stdin)
reply, _ := reader.ReadString('\n')
DefaultIfEmptyStr(&reply, "yes")
if IsEqStr(reply, "yes") {
return true
}
return
}

// AskPassword ask a password
func AskPassword(question string) (password string, err error) {
fmt.Println(question)
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return
}
password = string(bytePassword)
return
}

// BigInt is an alias for math/big.Int, for use with Swagger generated code.
// Even though it has some corresponding methods, convert it as soon as possible into big.Int.
type BigInt big.Int
Expand Down

0 comments on commit 21f6e2f

Please sign in to comment.