/
githubmirror
/
GoFrame
Обзор
Документация
Войти
/
githubmirror
/
GoFrame
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
cmd/gf/internal/utility/utils/utils.go
54 строки
1 KB
John Guo
add auto `go mod tidy` after version upgraded for command `up` (#2407)
18 янв 2023, 06:28
Не верифицирован
18 янв 2023, 06:28
d984f1a
Код
Авторство
О чём код?
package utils import ( "context" "fmt" "github.com/gogf/gf/cmd/gf/v2/internal/consts" "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog" "github.com/gogf/gf/v2/os/gfile" "github.com/gogf/gf/v2/os/gproc" "github.com/gogf/gf/v2/text/gstr" "golang.org/x/tools/imports" ) // GoFmt formats the source file and adds or removes import statements as necessary. func GoFmt(path string) { replaceFunc := func(path, content string) string { res, err := imports.Process(path, []byte(content), nil) if err != nil { mlog.Printf(`error format "%s" go files: %v`, path, err) return content } return string(res) } var err error if gfile.IsFile(path) { // File format. if gfile.ExtName(path) != "go" { return } err = gfile.ReplaceFileFunc(replaceFunc, path) } else { // Folder format. err = gfile.ReplaceDirFunc(replaceFunc, path, "*.go", true) } if err != nil { mlog.Printf(`error format "%s" go files: %v`, path, err) } } // GoModTidy executes `go mod tidy` at specified directory `dirPath`. func GoModTidy(ctx context.Context, dirPath string) error { command := fmt.Sprintf(`cd %s && go mod tidy`, dirPath) err := gproc.ShellRun(ctx, command) return err } // IsFileDoNotEdit checks and returns whether file contains `do not edit` key. func IsFileDoNotEdit(filePath string) bool { if !gfile.Exists(filePath) { return true } return gstr.Contains(gfile.GetContents(filePath), consts.DoNotEditKey) }