Application superstructure (or supersingleton, if you want).
Some checks failed
continuous-integration/drone/push Build is failing

Got rid of context thing which misleads due to existance of stdlib's
context package.

Also fixed golangci-lint configuration.

Fixes #20.
This commit is contained in:
2022-08-19 21:52:49 +05:00
parent b87921c811
commit 5fc6d3a181
35 changed files with 589 additions and 440 deletions

41
internal/helpers/path.go Normal file
View File

@@ -0,0 +1,41 @@
package helpers
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
var (
// ErrHelperError indicates that error came from helper.
ErrHelperError = errors.New("helper")
// ErrHelperPathNormalizationError indicates that error came from path normalization helper.
ErrHelperPathNormalizationError = errors.New("path normalization")
)
// NormalizePath normalizes passed path:
// * Path will be absolute.
// * Symlinks will be resolved.
// * Support for tilde (~) for home path.
func NormalizePath(path string) (string, error) {
// Replace possible tilde in the beginning (and only beginning!) of data path.
if strings.HasPrefix(path, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("%s: %s: %w", ErrHelperError, ErrHelperPathNormalizationError, err)
}
path = strings.Replace(path, "~", homeDir, 1)
}
// Normalize path.
absPath, err := filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("%s: %s: %w", ErrHelperError, ErrHelperPathNormalizationError, err)
}
return absPath, nil
}