package db import ( "os" "path/filepath" "regexp" "strings" ) var ( appIdRE = regexp.MustCompile(`^\d+$`) matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)") matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])") ) func ToSnakeCase(str string) string { snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}") snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}") return strings.ToLower(snake) } func IsAppID(s string) bool { return len(s) > 0 && appIdRE.MatchString(s) } // WriteFile will write the info on array of bytes b to filepath. It will set the file // permission mode to 0660 // Returns an error in case there's any. func WriteFile(filepath string, b []byte) error { return os.WriteFile(filepath, b, 0660) } // GetFile will open filepath. // Returns a tuple with a file and an error in case there's any. func GetFile(filepath string) (*os.File, error) { return os.OpenFile(filepath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0777) } // DeleteFile will delete filepath permanently. // Returns an error in case there's any. func DeleteFile(filepath string) error { _, err := os.Stat(filepath) if err != nil { if os.IsNotExist(err) { return nil } return err } return os.Remove(filepath) } func TempDir(appName string) (string, error) { homeDir, err := os.UserHomeDir() if err != nil { return "", err } tempDir, err := filepath.Abs(homeDir + "/.pmt/" + appName) if err != nil { return "", err } err = os.MkdirAll(tempDir, os.ModePerm) if err != nil { return "", err } return tempDir, nil }