Break utils package into subpackages.
This commit is contained in:
parent
06ecd73f67
commit
d7a5adb239
25 changed files with 276 additions and 318 deletions
85
internal/utils/filesys/filesys.go
Normal file
85
internal/utils/filesys/filesys.go
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
package filesys
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/format"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FormatAndSaveGoFile saves go file at folder dir, with name fileName and contents text.
|
||||
func FormatAndSaveGoFile(dirPath, fileName string, text []byte) error {
|
||||
newGoFilePath := filepath.Join(dirPath, fileName)
|
||||
|
||||
if !strings.HasSuffix(newGoFilePath, ".go") {
|
||||
newGoFilePath += ".go"
|
||||
}
|
||||
|
||||
file, err := os.Create(newGoFilePath)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
p, err := format.Source(text)
|
||||
|
||||
// if there is a format error we will write unformulated text for debug purposes
|
||||
if err != nil {
|
||||
file.Write(text)
|
||||
return fmt.Errorf("failed to format '%s', check '%s' for syntax errors: %w", fileName, newGoFilePath, err)
|
||||
}
|
||||
|
||||
_, err = file.Write(p)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to save '%s' file: %w", newGoFilePath, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnsureDirPathExist ensures dir path exists. If path does not exist, creates new path.
|
||||
func EnsureDirPathExist(dirPath string) error {
|
||||
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(dirPath, os.ModePerm)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't create directory - %s: %w", dirPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveDir deletes everything at folder dir.
|
||||
func RemoveDir(dir string) error {
|
||||
exist, err := DirExists(dir)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if exist {
|
||||
err := os.RemoveAll(dir)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DirExists checks if folder at path exist.
|
||||
func DirExists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return true, err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue