Skip to content

Commit 95c6fcc

Browse files
fix(config): replace ~ with HOME dir
resolves #237
1 parent 7c0d237 commit 95c6fcc

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/config/config.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ package config
22

33
import (
44
"bytes"
5-
"context"
5+
context_ "context"
66
"fmt"
77
"io"
88
"net"
99
"net/http"
1010
"os"
1111
"path"
12+
"runtime"
1213
"strings"
1314
"time"
1415

1516
"github.com/goccy/go-yaml"
17+
"github.com/jandedobbeleer/aliae/src/context"
1618
)
1719

1820
type httpClient interface {
@@ -73,11 +75,36 @@ func resolveConfigPath(configPath string) string {
7375
configPath = path.Join(home(), ".aliae.yaml")
7476
}
7577

76-
return configPath
78+
return replaceTildePrefixWithHomeDir(configPath)
79+
}
80+
81+
func replaceTildePrefixWithHomeDir(path string) string {
82+
if !strings.HasPrefix(path, "~") {
83+
return path
84+
}
85+
86+
rem := path[1:]
87+
if len(rem) == 0 || isSeparator(rem[0]) {
88+
return home() + rem
89+
}
90+
91+
return path
92+
}
93+
94+
func isSeparator(c uint8) bool {
95+
if c == '/' {
96+
return true
97+
}
98+
99+
if runtime.GOOS == context.WINDOWS && c == '\\' {
100+
return true
101+
}
102+
103+
return false
77104
}
78105

79106
func getRemoteConfig(url string) (*Aliae, error) {
80-
req, err := http.NewRequestWithContext(context.Background(), "GET", url, nil)
107+
req, err := http.NewRequestWithContext(context_.Background(), "GET", url, nil)
81108
if err != nil {
82109
return nil, err
83110
}

0 commit comments

Comments
 (0)