Skip to content

Commit 937f394

Browse files
feat(): make command line windows compatible (#29)
1 parent 69600b7 commit 937f394

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

internal/common/function.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"os"
1010
"path/filepath"
11+
"runtime"
1112
"strings"
1213
)
1314

@@ -29,6 +30,9 @@ const (
2930
DOCKER_CONFIG_FILE = ".config/403unlocker/dockerRegistry.conf"
3031
DNS_CONFIG_URL = "https://raw.githubusercontent.com/403unlocker/403Unlocker-cli/refs/heads/main/config/dns.conf"
3132
DOCKER_CONFIG_URL = "https://raw.githubusercontent.com/403unlocker/403Unlocker-cli/refs/heads/main/config/dockerRegistry.conf"
33+
34+
// OS names
35+
WINDOWS_OS_NAME = "windows"
3236
)
3337

3438
// FormatDataSize converts the size in bytes to a human-readable string in KB, MB, or GB.
@@ -52,14 +56,12 @@ func FormatDataSize(bytes int64) string {
5256
}
5357

5458
func DownloadConfigFile(url, path string) error {
55-
56-
homeDir := os.Getenv("HOME")
59+
homeDir := GetHomeDir()
5760
if homeDir == "" {
5861
fmt.Println("HOME environment variable not set")
5962
os.Exit(1)
6063
}
61-
filePath := homeDir + "/" + path
62-
64+
filePath := AddPathToDir(homeDir, path)
6365
dir := filepath.Dir(filePath)
6466
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
6567
fmt.Printf("Error creating directory: %v\n", err)
@@ -100,14 +102,12 @@ func DownloadConfigFile(url, path string) error {
100102
}
101103

102104
func WriteDNSToFile(filename string, dnsList []string) error {
103-
homeDir := os.Getenv("HOME")
105+
homeDir := GetHomeDir()
104106
if homeDir == "" {
105107
fmt.Println("HOME environment variable not set")
106108
os.Exit(1)
107109
}
108-
109-
filename = homeDir + "/" + filename
110-
110+
filename = AddPathToDir(homeDir, filename)
111111
_, err := os.Stat(filename)
112112
if os.IsNotExist(err) {
113113
file, err := os.Create(filename)
@@ -130,12 +130,12 @@ func WriteDNSToFile(filename string, dnsList []string) error {
130130
}
131131

132132
func ReadDNSFromFile(filename string) ([]string, error) {
133-
homeDir := os.Getenv("HOME")
133+
homeDir := GetHomeDir()
134134
if homeDir == "" {
135135
fmt.Println("HOME environment variable not set")
136136
os.Exit(1)
137137
}
138-
filename = homeDir + "/" + filename
138+
filename = AddPathToDir(homeDir, filename)
139139
data, err := os.ReadFile(filename)
140140
if err != nil {
141141
return nil, err
@@ -164,3 +164,24 @@ func ChangeDNS(dns string) *http.Client {
164164
}
165165
return client
166166
}
167+
func GetHomeDir() string {
168+
if runtime.GOOS == WINDOWS_OS_NAME {
169+
return os.Getenv("USERPROFILE")
170+
} else {
171+
return os.Getenv("HOME")
172+
}
173+
}
174+
func GetTempDir() string {
175+
if runtime.GOOS == WINDOWS_OS_NAME {
176+
return os.Getenv("TEMP")
177+
} else {
178+
return "/tmp"
179+
}
180+
}
181+
func AddPathToDir(baseDir, extraDir string) string {
182+
if runtime.GOOS == WINDOWS_OS_NAME {
183+
return baseDir + "\\" + extraDir
184+
} else {
185+
return baseDir + "/" + extraDir
186+
}
187+
}

internal/dns/function.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ func CheckWithURL(c *cli.Context) error {
161161
fmt.Printf("| %-18s | %-14s |\n", "DNS Server", "Download Speed")
162162
fmt.Println("+--------------------+----------------+")
163163

164-
tempDir := time.Now().UnixMilli()
164+
rand := time.Now().UnixMilli()
165+
tempDir := common.AddPathToDir(common.GetTempDir(), strconv.Itoa(int(rand)))
166+
165167
var wg sync.WaitGroup
166168
for _, dns := range dnsList {
167169
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
@@ -170,8 +172,8 @@ func CheckWithURL(c *cli.Context) error {
170172
clientWithCustomDNS := common.ChangeDNS(dns)
171173
client := grab.NewClient()
172174
client.HTTPClient = clientWithCustomDNS
175+
req, err := grab.NewRequest(fmt.Sprintf("%v", tempDir), fileToDownload)
173176

174-
req, err := grab.NewRequest(fmt.Sprintf("/tmp/%v", tempDir), fileToDownload)
175177
if err != nil {
176178
fmt.Fprintf(os.Stderr, "Error creating request for DNS %s: %v\n", dns, err)
177179
}
@@ -213,6 +215,6 @@ func CheckWithURL(c *cli.Context) error {
213215
fmt.Println("No DNS server was able to download any data.")
214216
}
215217

216-
os.RemoveAll(fmt.Sprintf("/tmp/%v", tempDir))
218+
os.RemoveAll(fmt.Sprintf("%v", tempDir))
217219
return nil
218220
}

internal/docker/function.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path/filepath"
1111
"regexp"
12+
"strconv"
1213
"strings"
1314
"sync/atomic"
1415
"time"
@@ -102,8 +103,8 @@ func CheckWithDockerImage(c *cli.Context) error {
102103
registrySizeMap := make(map[string]int64)
103104
timeout := c.Int("timeout")
104105
imageName := c.Args().First()
105-
tempDir := time.Now().UnixMilli()
106-
106+
rand := time.Now().UnixMilli()
107+
tempDir := common.AddPathToDir(common.GetTempDir(), strconv.Itoa(int(rand)))
107108
fmt.Printf("\nTimeout: %d seconds\n", timeout)
108109
fmt.Printf("Docker Image: %s\n\n", imageName)
109110

@@ -147,7 +148,7 @@ func CheckWithDockerImage(c *cli.Context) error {
147148
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
148149
defer cancel()
149150

150-
size, err := DownloadDockerImage(ctx, imageName, registry, fmt.Sprintf("/tmp/%v", tempDir))
151+
size, err := DownloadDockerImage(ctx, imageName, registry, fmt.Sprintf("%v", tempDir))
151152
if err != nil {
152153
fmt.Printf("| %-*s | %s%-16s%s |\n",
153154
maxLength, registry,
@@ -181,6 +182,6 @@ func CheckWithDockerImage(c *cli.Context) error {
181182
fmt.Println("No registry was able to download any data.")
182183
}
183184

184-
os.RemoveAll(fmt.Sprintf("/tmp/%v", tempDir))
185+
os.RemoveAll(fmt.Sprintf("%v", tempDir))
185186
return nil
186187
}

0 commit comments

Comments
 (0)