2021-01-09 02:51:36 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2021 Arsen Musayelyan
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
}
|