1package utils 2 3import ( 4 "fmt" 5 "io/ioutil" 6 "os/exec" 7 "regexp" 8 "runtime" 9 "strings" 10) 11 12const ( 13 SimpleDateFormat = "Jan 2" 14 SimpleTimeFormat = "15:04 MST" 15 MinimumTimeFormat = "15:04" 16 17 FullDateFormat = "Monday, Jan 2" 18 FriendlyDateFormat = "Mon, Jan 2" 19 FriendlyDateTimeFormat = "Mon, Jan 2, 15:04" 20 21 TimestampFormat = "2006-01-02T15:04:05-0700" 22) 23 24// DoesNotInclude takes a slice of strings and a target string and returns 25// TRUE if the slice does not include the target, FALSE if it does 26// 27// Example: 28// 29// x := DoesNotInclude([]string{"cat", "dog", "rat"}, "dog") 30// > false 31// 32// x := DoesNotInclude([]string{"cat", "dog", "rat"}, "pig") 33// > true 34// 35func DoesNotInclude(strs []string, val string) bool { 36 return !Includes(strs, val) 37} 38 39// ExecuteCommand executes an external command on the local machine as the current user 40func ExecuteCommand(cmd *exec.Cmd) string { 41 if cmd == nil { 42 return "" 43 } 44 45 stdout, err := cmd.StdoutPipe() 46 if err != nil { 47 return fmt.Sprintf("%v\n", err) 48 } 49 50 if err := cmd.Start(); err != nil { 51 return fmt.Sprintf("%v\n", err) 52 } 53 54 var str string 55 if b, err := ioutil.ReadAll(stdout); err == nil { 56 str += string(b) 57 } 58 59 err = cmd.Wait() 60 if err != nil { 61 return fmt.Sprintf("%v\n", err) 62 } 63 64 return str 65} 66 67// FindMatch takes a regex pattern and a string of data and returns back all the matches 68// in that string 69func FindMatch(pattern string, data string) [][]string { 70 r := regexp.MustCompile(pattern) 71 return r.FindAllStringSubmatch(data, -1) 72} 73 74// Includes takes a slice of strings and a target string and returns 75// TRUE if the slice includes the target, FALSE if it does not 76// 77// Example: 78// 79// x := Includes([]string{"cat", "dog", "rat"}, "dog") 80// > true 81// 82// x := Includes([]string{"cat", "dog", "rat"}, "pig") 83// > false 84// 85func Includes(strs []string, val string) bool { 86 for _, str := range strs { 87 if val == str { 88 return true 89 } 90 } 91 return false 92} 93 94// OpenFile opens the file defined in `path` via the operating system 95func OpenFile(path string) { 96 if (strings.HasPrefix(path, "http://")) || (strings.HasPrefix(path, "https://")) { 97 switch runtime.GOOS { 98 case "linux": 99 exec.Command("xdg-open", path).Start() 100 case "windows": 101 exec.Command("rundll32", "url.dll,FileProtocolHandler", path).Start() 102 case "darwin": 103 exec.Command("open", path).Start() 104 default: 105 // for the BSDs 106 exec.Command("xdg-open", path).Start() 107 } 108 } else { 109 filePath, _ := ExpandHomeDir(path) 110 cmd := exec.Command(OpenFileUtil, filePath) 111 ExecuteCommand(cmd) 112 } 113} 114 115// ReadFileBytes reads the contents of a file and returns those contents as a slice of bytes 116func ReadFileBytes(filePath string) ([]byte, error) { 117 fileData, err := ioutil.ReadFile(filePath) 118 if err != nil { 119 return []byte{}, err 120 } 121 122 return fileData, nil 123} 124