package goutil import ( "bufio" "errors" "fmt" "io/ioutil" "os" "path" "path/filepath" "strings" ) func DecorateError(s string, err error) error { if err != nil { return errors.New(s + ": " + err.Error()) } else { return nil } } func WriteFile(filename string, data string) error { return ioutil.WriteFile(filename, []byte(data), 0644) } func ReadFile(filename string) (string, error) { data, err := ioutil.ReadFile(filename) return string(data), err } func TrimExt(path string) string { extension := filepath.Ext(path) return path[0 : len(path)-len(extension)] } func ListFilesExt(dir string, ext string) []string { list := make([]string, 0) ext = strings.ToUpper(ext) files, err := ioutil.ReadDir(dir) if err == nil { for _, file := range files { if !file.IsDir() { if strings.ToUpper(filepath.Ext(file.Name())) == ext { list = append(list, path.Join(dir, file.Name())) } } } } return list } func PathExists(path string) bool { if _, err := os.Stat(path); os.IsNotExist(err) { return false } else { return true } } func AskFor(question string) (string, error) { fmt.Print(question + ": ") reader := bufio.NewReader(os.Stdin) s, err := reader.ReadString('\n') if err == nil { return strings.TrimSpace(s), nil } else { return "", err } } func IntMin(x, y int) int { if x < y { return x } else { return y } } func IntMax(x, y int) int { if x > y { return x } else { return y } } func StrSliceAt(sl []string, i int) string { if i < len(sl) { return sl[i] } else { return "" } } func StrSlice(sl []string, start int, end int) []string { bound := func(i int) int { return IntMin(IntMax(0, i), len(sl)) } start = bound(start) end = bound(end) return sl[start:end] }