go-zero/tools/goctl/util/file.go
Keson db16115037
rpc service generation (#26)
* add execute files

* add protoc-osx

* add rpc generation

* add rpc generation

* add: rpc template generation

* update usage

* fixed env prepare for project in go path

* optimize gomod cache

* add README.md

* format error

* reactor templatex.go

* remove waste code
2020-08-28 19:24:58 +08:00

50 lines
887 B
Go

package util
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/logrusorgru/aurora"
)
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))
}