Stanislav N. aka pztrn
5fc6d3a181
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.
42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
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
|
|
}
|