A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
update(pkg): make functions in dir more clear
K8sCatf77e455e275c
diff --git a/internal/cli/install.go b/internal/cli/install.goindex c1e0b3b5..151e8570 100644--- a/internal/cli/install.go+++ b/internal/cli/install.go@@ -44,7 +44,7 @@ func installConfigFile() {return}- if _, err := dir.CreatePathIsNotExist(ConfigFilePath); err != nil {+ if err := dir.CreateDirIfNotExist(ConfigFilePath); err != nil {fmt.Printf("[config-file] create directory fail %s\n", err.Error())return}@@ -59,7 +59,7 @@ func installConfigFile() {func installUploadDir() {fmt.Println("[upload-dir] try to install...")- if _, err := dir.CreatePathIsNotExist(UploadFilePath); err != nil {+ if err := dir.CreateDirIfNotExist(UploadFilePath); err != nil {fmt.Printf("[upload-dir] install fail %s\n", err.Error())} else {fmt.Printf("[upload-dir] install success, upload directory is %s\n", UploadFilePath)@@ -68,7 +68,7 @@ func installUploadDir() {func installI18nBundle() {fmt.Println("[i18n] try to install i18n bundle...")- if _, err := dir.CreatePathIsNotExist(I18nPath); err != nil {+ if err := dir.CreateDirIfNotExist(I18nPath); err != nil {fmt.Println(err.Error())return}diff --git a/internal/cli/install_check.go b/internal/cli/install_check.goindex 953d58a8..5ed217a4 100644--- a/internal/cli/install_check.go+++ b/internal/cli/install_check.go@@ -6,11 +6,11 @@ import ()func CheckConfigFile(configPath string) bool {- return dir.CheckPathExist(configPath)+ return dir.CheckFileExist(configPath)}func CheckUploadDir() bool {- return dir.CheckPathExist(UploadFilePath)+ return dir.CheckDirExist(UploadFilePath)}func CheckDB(dataConf *data.Database) bool {diff --git a/internal/service/uploader/upload.go b/internal/service/uploader/upload.goindex a202ba8c..bc629004 100644--- a/internal/service/uploader/upload.go+++ b/internal/service/uploader/upload.go@@ -26,11 +26,11 @@ type UploaderService struct {// NewUploaderService new upload servicefunc NewUploaderService(serviceConfig *service_config.ServiceConfig) *UploaderService {- _, err := dir.CreatePathIsNotExist(filepath.Join(serviceConfig.UploadPath, avatarSubPath))+ err := dir.CreateDirIfNotExist(filepath.Join(serviceConfig.UploadPath, avatarSubPath))if err != nil {panic(err)}- _, err = dir.CreatePathIsNotExist(filepath.Join(serviceConfig.UploadPath, postSubPath))+ err = dir.CreateDirIfNotExist(filepath.Join(serviceConfig.UploadPath, postSubPath))if err != nil {panic(err)}diff --git a/pkg/dir/dir.go b/pkg/dir/dir.goindex 5be4f8ec..559f0d1c 100644--- a/pkg/dir/dir.go+++ b/pkg/dir/dir.go@@ -2,28 +2,16 @@ package dirimport "os"-// CreatePathIsNotExist create path is not exists-func CreatePathIsNotExist(path string) (bool, error) {- _, err := os.Stat(path)- if err == nil {- return true, nil- }- if os.IsNotExist(err) {- // create directory- if err := os.MkdirAll(path, os.ModePerm); err != nil {- return false, err- } else {- return true, nil- }- }- return false, err+func CreateDirIfNotExist(path string) error {+ return os.MkdirAll(path, os.ModePerm)}-// CheckPathExist path if exist-func CheckPathExist(path string) bool {- _, err := os.Stat(path)- if err == nil {- return true- }- return false+func CheckDirExist(path string) bool {+ f, err := os.Stat(path)+ return err == nil && f.IsDir()+}++func CheckFileExist(path string) bool {+ f, err := os.Stat(path)+ return err == nil && !f.IsDir()}