// Package util define universal utility functions package util import ( "bytes" "errors" "fmt" "io/fs" "os" "os/exec" "path/filepath" "strings" "wave_record/constant" ) // FileExists return boolean value result specifying whether the file path exists func FileExists(filePath string) bool { _, err := os.Stat(filePath) // return !os.IsNotExist(err) return !errors.Is(err, fs.ErrNotExist) } func commandExec(name string, args ...string) (outString string, err error) { var stdout bytes.Buffer var stderr bytes.Buffer cmd := exec.Command(name, args...) cmd.Stdout = &stdout cmd.Stderr = &stderr err = cmd.Run() outString = stdout.String() if err != nil { err = fmt.Errorf("exec failed:%v,stderr=%s. name=%s,args=%v", err, stderr.String(), name, args) } return } // RemoveFile return file movement result func RemoveFile(src, dst string) error { _, err := commandExec("mv", src, dst) return err } // FileNameConvert return the absolute path of the converted config file or data file func FileNameConvert(filePath string) (string, error) { dirPath := filepath.Dir(filePath) fileName := filepath.Base(filePath) name := strings.Split(fileName, ".")[0] suffix := filepath.Ext(fileName) if suffix == constant.ConfigFileSuffix { fileName := strings.Join([]string{name, constant.DataFileSuffix}, "") return filepath.Join(dirPath, fileName), nil } else if suffix == constant.DataFileSuffix { fileName := strings.Join([]string{name, constant.ConfigFileSuffix}, "") return filepath.Join(dirPath, fileName), nil } return "", constant.ErrNotSupportFileType }