From dcc768981613aa0386e6f40e94f93b70aca12d57 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Mon, 29 Apr 2019 16:35:07 -0600 Subject: [PATCH] consolidate utils --- src/utils/exists.go | 13 ------ src/utils/getinput.go | 15 ------- src/utils/hash.go | 33 -------------- src/utils/ip.go | 39 ---------------- src/utils/ip_test.go | 11 ----- src/utils/randomname.go | 16 ------- src/utils/utils.go | 98 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 98 insertions(+), 127 deletions(-) delete mode 100644 src/utils/exists.go delete mode 100644 src/utils/getinput.go delete mode 100644 src/utils/hash.go delete mode 100644 src/utils/ip.go delete mode 100644 src/utils/ip_test.go delete mode 100644 src/utils/randomname.go create mode 100644 src/utils/utils.go diff --git a/src/utils/exists.go b/src/utils/exists.go deleted file mode 100644 index 4f4c7439..00000000 --- a/src/utils/exists.go +++ /dev/null @@ -1,13 +0,0 @@ -package utils - -import "os" - -// Exists reports whether the named file or directory exists. -func Exists(name string) bool { - if _, err := os.Stat(name); err != nil { - if os.IsNotExist(err) { - return false - } - } - return true -} diff --git a/src/utils/getinput.go b/src/utils/getinput.go deleted file mode 100644 index 7c397cec..00000000 --- a/src/utils/getinput.go +++ /dev/null @@ -1,15 +0,0 @@ -package utils - -import ( - "bufio" - "fmt" - "os" - "strings" -) - -func GetInput(prompt string) string { - reader := bufio.NewReader(os.Stdin) - fmt.Fprintf(os.Stderr, "%s", prompt) - text, _ := reader.ReadString('\n') - return strings.TrimSpace(text) -} diff --git a/src/utils/hash.go b/src/utils/hash.go deleted file mode 100644 index 34d1c0d6..00000000 --- a/src/utils/hash.go +++ /dev/null @@ -1,33 +0,0 @@ -package utils - -import ( - "crypto/md5" - "crypto/sha256" - "fmt" - "io" - "os" -) - -// HashFile returns the md5 hash of a file -func HashFile(fname string) (hash256 []byte, err error) { - f, err := os.Open(fname) - if err != nil { - return - } - defer f.Close() - - h := md5.New() - if _, err = io.Copy(h, f); err != nil { - return - } - - hash256 = h.Sum(nil) - return -} - -// SHA256 returns sha256 sum -func SHA256(s string) string { - sha := sha256.New() - sha.Write([]byte(s)) - return fmt.Sprintf("%x", sha.Sum(nil)) -} diff --git a/src/utils/ip.go b/src/utils/ip.go deleted file mode 100644 index 4142fdfe..00000000 --- a/src/utils/ip.go +++ /dev/null @@ -1,39 +0,0 @@ -package utils - -import ( - "io/ioutil" - "log" - "net" - "net/http" - "strings" -) - -func PublicIP() (ip string, err error) { - resp, err := http.Get("https://canhazip.com") - if err != nil { - return - } - defer resp.Body.Close() - - if resp.StatusCode == http.StatusOK { - bodyBytes, err := ioutil.ReadAll(resp.Body) - if err != nil { - return "", err - } - ip = strings.TrimSpace(string(bodyBytes)) - } - return -} - -// Get preferred outbound ip of this machine -func LocalIP() string { - conn, err := net.Dial("udp", "8.8.8.8:80") - if err != nil { - log.Fatal(err) - } - defer conn.Close() - - localAddr := conn.LocalAddr().(*net.UDPAddr) - - return localAddr.IP.String() -} diff --git a/src/utils/ip_test.go b/src/utils/ip_test.go deleted file mode 100644 index ae3fcf67..00000000 --- a/src/utils/ip_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package utils - -import ( - "fmt" - "testing" -) - -func TestGetIP(t *testing.T) { - fmt.Println(PublicIP()) - fmt.Println(LocalIP()) -} diff --git a/src/utils/randomname.go b/src/utils/randomname.go deleted file mode 100644 index b044e09d..00000000 --- a/src/utils/randomname.go +++ /dev/null @@ -1,16 +0,0 @@ -package utils - -import ( - "crypto/rand" - "strings" - - "github.com/schollz/mnemonicode" -) - -func GetRandomName() string { - result := []string{} - bs := make([]byte, 4) - rand.Read(bs) - result = mnemonicode.EncodeWordList(result, bs) - return strings.Join(result, "-") -} diff --git a/src/utils/utils.go b/src/utils/utils.go new file mode 100644 index 00000000..4b444bed --- /dev/null +++ b/src/utils/utils.go @@ -0,0 +1,98 @@ +package utils + +import ( + "bufio" + "crypto/md5" + "crypto/rand" + "crypto/sha256" + "fmt" + "io" + "io/ioutil" + "log" + "net" + "net/http" + "os" + "strings" + + "github.com/schollz/mnemonicode" +) + +// Exists reports whether the named file or directory exists. +func Exists(name string) bool { + if _, err := os.Stat(name); err != nil { + if os.IsNotExist(err) { + return false + } + } + return true +} + +// GetInput returns the input with a given prompt +func GetInput(prompt string) string { + reader := bufio.NewReader(os.Stdin) + fmt.Fprintf(os.Stderr, "%s", prompt) + text, _ := reader.ReadString('\n') + return strings.TrimSpace(text) +} + +// HashFile returns the md5 hash of a file +func HashFile(fname string) (hash256 []byte, err error) { + f, err := os.Open(fname) + if err != nil { + return + } + defer f.Close() + + h := md5.New() + if _, err = io.Copy(h, f); err != nil { + return + } + + hash256 = h.Sum(nil) + return +} + +// SHA256 returns sha256 sum +func SHA256(s string) string { + sha := sha256.New() + sha.Write([]byte(s)) + return fmt.Sprintf("%x", sha.Sum(nil)) +} + +func PublicIP() (ip string, err error) { + resp, err := http.Get("https://canhazip.com") + if err != nil { + return + } + defer resp.Body.Close() + + if resp.StatusCode == http.StatusOK { + bodyBytes, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + ip = strings.TrimSpace(string(bodyBytes)) + } + return +} + +// Get preferred outbound ip of this machine +func LocalIP() string { + conn, err := net.Dial("udp", "8.8.8.8:80") + if err != nil { + log.Fatal(err) + } + defer conn.Close() + + localAddr := conn.LocalAddr().(*net.UDPAddr) + + return localAddr.IP.String() +} + +func GetRandomName() string { + result := []string{} + bs := make([]byte, 4) + rand.Read(bs) + result = mnemonicode.EncodeWordList(result, bs) + return strings.Join(result, "-") +}