first commit
This commit is contained in:
60
internal/utils/filesystem.go
Normal file
60
internal/utils/filesystem.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
)
|
||||
|
||||
func SaveStaticFile(path string, data []byte) error {
|
||||
return os.WriteFile(path, data, 0644)
|
||||
}
|
||||
|
||||
func CreateDir(path string) error {
|
||||
return os.Mkdir(path, 0755)
|
||||
}
|
||||
|
||||
func CreateDirIfNotExists(path string) error {
|
||||
exists, err := IsExistsDir(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
if err = CreateDir(path); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func IsExistsDir(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
func RemoveFile(path string) error {
|
||||
return os.Remove(path)
|
||||
}
|
||||
|
||||
func WriteStringFile(path string, data string) error {
|
||||
buffer := &bytes.Buffer{}
|
||||
buffer.WriteString(data)
|
||||
|
||||
return os.WriteFile(path, buffer.Bytes(), 0644)
|
||||
}
|
||||
|
||||
func ReadStringFile(path string) (string, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
}
|
||||
39
internal/utils/logger/logger.go
Normal file
39
internal/utils/logger/logger.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package logger
|
||||
|
||||
import "fmt"
|
||||
|
||||
type LogVerbose uint8
|
||||
|
||||
const (
|
||||
Warning LogVerbose = 0
|
||||
Info = 1
|
||||
Debug = 2
|
||||
Trace = 3
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
// Уровень логирования
|
||||
Verbose LogVerbose
|
||||
}
|
||||
|
||||
func (log *Logger) Warn(message string) {
|
||||
fmt.Printf("[warning] %s\n", message)
|
||||
}
|
||||
|
||||
func (log *Logger) Info(message string) {
|
||||
if log.Verbose >= Info {
|
||||
fmt.Printf("[info] %s\n", message)
|
||||
}
|
||||
}
|
||||
|
||||
func (log *Logger) Debug(message string) {
|
||||
if log.Verbose >= Debug {
|
||||
fmt.Printf("[debug] %s\n", message)
|
||||
}
|
||||
}
|
||||
|
||||
func (log *Logger) Trace(message string) {
|
||||
if log.Verbose >= Trace {
|
||||
fmt.Printf("[trace] %s\n", message)
|
||||
}
|
||||
}
|
||||
36
internal/utils/yaml_utils.go
Normal file
36
internal/utils/yaml_utils.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
)
|
||||
|
||||
func YamlHeader() string {
|
||||
return "# vi: set tabstop=2 shiftwidth=2 expandtab :\n---\n"
|
||||
}
|
||||
|
||||
func WriteYaml(data interface{}, yamlFile string) error {
|
||||
buffer := &bytes.Buffer{}
|
||||
buffer.WriteString(YamlHeader())
|
||||
|
||||
yamlEncoder := yaml.NewEncoder(buffer)
|
||||
yamlEncoder.SetIndent(2)
|
||||
|
||||
if err := yamlEncoder.Encode(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.WriteFile(yamlFile, buffer.Bytes(), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteEmptyYaml(yamlFile string) error {
|
||||
buffer := &bytes.Buffer{}
|
||||
buffer.WriteString(YamlHeader())
|
||||
|
||||
return os.WriteFile(yamlFile, buffer.Bytes(), 0644)
|
||||
}
|
||||
Reference in New Issue
Block a user