Application superstructure (or supersingleton, if you want).
Some checks failed
continuous-integration/drone/push Build is failing
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:
41
internal/helpers/path.go
Normal file
41
internal/helpers/path.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user