This repository has been archived on 2021-07-08. You can view files and clone it, but cannot push or open issues or pull requests.
opensend/extra.go

31 lines
792 B
Go
Raw Normal View History

2020-12-31 07:54:18 +00:00
package main
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os"
"path/filepath"
"strings"
)
func ExpandPath(s string) string {
// Use ConsoleWriter logger
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{})
2021-01-03 02:00:39 +00:00
// Get user's home directory
2020-12-31 07:54:18 +00:00
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatal().Err(err).Msg("Error getting home directory")
}
2021-01-03 02:00:39 +00:00
// Expand any environment variables in string
2020-12-31 07:54:18 +00:00
expandedString := os.ExpandEnv(s)
2021-01-03 02:00:39 +00:00
// If string starts with ~
2020-12-31 07:54:18 +00:00
if strings.HasPrefix(expandedString, "~") {
2021-01-03 02:00:39 +00:00
// Replace ~ with user's home directory
2020-12-31 07:54:18 +00:00
expandedString = strings.Replace(expandedString, "~", homeDir, 1)
}
2021-01-03 02:00:39 +00:00
// Clean file path
2020-12-31 07:54:18 +00:00
expandedString = filepath.Clean(expandedString)
2021-01-03 02:00:39 +00:00
// Return expanded string
2020-12-31 07:54:18 +00:00
return expandedString
}