utils.go raw

   1  /*
   2  Copyright © LiquidWeb
   3  
   4  Licensed under the Apache License, Version 2.0 (the "License");
   5  you may not use this file except in compliance with the License.
   6  You may obtain a copy of the License at
   7  
   8      http://www.apache.org/licenses/LICENSE-2.0
   9  
  10  Unless required by applicable law or agreed to in writing, software
  11  distributed under the License is distributed on an "AS IS" BASIS,
  12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13  See the License for the specific language governing permissions and
  14  limitations under the License.
  15  */
  16  package utils
  17  
  18  import (
  19  	"fmt"
  20  	"math/rand"
  21  	"net"
  22  	"os"
  23  	"time"
  24  
  25  	"github.com/k0kubun/go-ansi"
  26  )
  27  
  28  func IpIsValid(ip string) bool {
  29  	if parsedIp := net.ParseIP(ip); parsedIp == nil {
  30  		return false
  31  	}
  32  
  33  	return true
  34  }
  35  
  36  func RandomString(length int) string {
  37  	charset := "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + "0123456789"
  38  	var seededRand *rand.Rand = rand.New(
  39  		rand.NewSource(time.Now().UnixNano()))
  40  
  41  	b := make([]byte, length)
  42  	for i := range b {
  43  		b[i] = charset[seededRand.Intn(len(charset))]
  44  	}
  45  	return string(b)
  46  }
  47  
  48  func FileExists(file string) bool {
  49  	fileStat, err := os.Stat(file)
  50  	if os.IsNotExist(err) {
  51  		return false
  52  	}
  53  
  54  	return !fileStat.IsDir()
  55  }
  56  
  57  func PrintRed(m string, args ...interface{}) {
  58  	msg := fmt.Sprintf(m, args...)
  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  	}
  62  }
  63  
  64  func PrintTeal(m string, args ...interface{}) {
  65  	msg := fmt.Sprintf(m, args...)
  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  	}
  69  }
  70  
  71  func PrintGreen(m string, args ...interface{}) {
  72  	msg := fmt.Sprintf(m, args...)
  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  	}
  76  }
  77  
  78  func PrintYellow(m string, args ...interface{}) {
  79  	msg := fmt.Sprintf(m, args...)
  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  	}
  83  }
  84  
  85  func PrintMagenta(m string, args ...interface{}) {
  86  	msg := fmt.Sprintf(m, args...)
  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  	}
  90  }
  91  
  92  func PrintPurple(m string, args ...interface{}) {
  93  	msg := fmt.Sprintf(m, args...)
  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  	}
  97  }
  98  
  99  // private
 100  
 101  var (
 102  	teal    = colorize("\033[1;36m%s\033[0m")
 103  	red     = colorize("\033[1;31m%s\033[0m")
 104  	green   = colorize("\033[1;32m%s\033[0m")
 105  	yellow  = colorize("\033[1;33m%s\033[0m")
 106  	magenta = colorize("\033[1;35m%s\033[0m")
 107  	purple  = colorize("\033[1;34m%s\033[0m")
 108  )
 109  
 110  func colorize(color string) func(...interface{}) string {
 111  	colorized := func(args ...interface{}) string {
 112  		return fmt.Sprintf(color,
 113  			fmt.Sprint(args...))
 114  	}
 115  
 116  	return colorized
 117  }
 118