mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 01:30:25 +08:00
0a9c427443
* remove mock generation * add: proto project import * update document * remove mock generation * add: proto project import * update document * remove NL * update document * optimize code * add test * add test
54 lines
909 B
Go
54 lines
909 B
Go
package util
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/logrusorgru/aurora"
|
|
)
|
|
|
|
const (
|
|
NL = "\n"
|
|
)
|
|
|
|
func CreateIfNotExist(file string) (*os.File, error) {
|
|
_, err := os.Stat(file)
|
|
if !os.IsNotExist(err) {
|
|
return nil, fmt.Errorf("%s already exist", file)
|
|
}
|
|
|
|
return os.Create(file)
|
|
}
|
|
|
|
func RemoveIfExist(filename string) error {
|
|
if !FileExists(filename) {
|
|
return nil
|
|
}
|
|
|
|
return os.Remove(filename)
|
|
}
|
|
|
|
func RemoveOrQuit(filename string) error {
|
|
if !FileExists(filename) {
|
|
return nil
|
|
}
|
|
|
|
fmt.Printf("%s exists, overwrite it?\nEnter to overwrite or Ctrl-C to cancel...",
|
|
aurora.BgRed(aurora.Bold(filename)))
|
|
bufio.NewReader(os.Stdin).ReadBytes('\n')
|
|
|
|
return os.Remove(filename)
|
|
}
|
|
|
|
func FileExists(file string) bool {
|
|
_, err := os.Stat(file)
|
|
return err == nil
|
|
}
|
|
|
|
func FileNameWithoutExt(file string) string {
|
|
return strings.TrimSuffix(file, filepath.Ext(file))
|
|
}
|