Skip to content

Commit

Permalink
feat(): make commandline windows compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
ArmanTaheriGhaleTaki committed Jan 28, 2025
1 parent 69600b7 commit 283f5dc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
38 changes: 28 additions & 10 deletions internal/common/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
)

Expand Down Expand Up @@ -52,14 +53,12 @@ func FormatDataSize(bytes int64) string {
}

func DownloadConfigFile(url, path string) error {

homeDir := os.Getenv("HOME")
homeDir := GetHomeDir()
if homeDir == "" {
fmt.Println("HOME environment variable not set")
os.Exit(1)
}
filePath := homeDir + "/" + path

filePath := AddPathToDir(homeDir, path)
dir := filepath.Dir(filePath)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
fmt.Printf("Error creating directory: %v\n", err)
Expand Down Expand Up @@ -100,14 +99,12 @@ func DownloadConfigFile(url, path string) error {
}

func WriteDNSToFile(filename string, dnsList []string) error {
homeDir := os.Getenv("HOME")
homeDir := GetHomeDir()
if homeDir == "" {
fmt.Println("HOME environment variable not set")
os.Exit(1)
}

filename = homeDir + "/" + filename

filename = AddPathToDir(homeDir, filename)
_, err := os.Stat(filename)
if os.IsNotExist(err) {
file, err := os.Create(filename)
Expand All @@ -130,12 +127,12 @@ func WriteDNSToFile(filename string, dnsList []string) error {
}

func ReadDNSFromFile(filename string) ([]string, error) {
homeDir := os.Getenv("HOME")
homeDir := GetHomeDir()
if homeDir == "" {
fmt.Println("HOME environment variable not set")
os.Exit(1)
}
filename = homeDir + "/" + filename
filename = AddPathToDir(homeDir, filename)
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
Expand Down Expand Up @@ -164,3 +161,24 @@ func ChangeDNS(dns string) *http.Client {
}
return client
}
func GetHomeDir() string {
if runtime.GOOS == "windows" {
return os.Getenv("USERPROFILE")
} else {
return os.Getenv("HOME")
}
}
func GetTempDir() string {
if runtime.GOOS == "windows" {
return os.Getenv("TEMP")
} else {
return "/tmp"
}
}
func AddPathToDir(baseDir, extraDir string) string {
if runtime.GOOS == "windows" {
return baseDir + "\\" + extraDir
} else {
return baseDir + "/" + extraDir
}
}
10 changes: 7 additions & 3 deletions internal/dns/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ func CheckWithURL(c *cli.Context) error {
fmt.Printf("| %-18s | %-14s |\n", "DNS Server", "Download Speed")
fmt.Println("+--------------------+----------------+")

tempDir := time.Now().UnixMilli()
rand := time.Now().UnixMilli()
tempDir := common.AddPathToDir(common.GetTempDir(), strconv.Itoa(int(rand)))

var wg sync.WaitGroup
for _, dns := range dnsList {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
Expand All @@ -170,8 +172,10 @@ func CheckWithURL(c *cli.Context) error {
clientWithCustomDNS := common.ChangeDNS(dns)
client := grab.NewClient()
client.HTTPClient = clientWithCustomDNS
var req *grab.Request
var err error
req, err = grab.NewRequest(fmt.Sprintf("%v", tempDir), fileToDownload)

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

os.RemoveAll(fmt.Sprintf("/tmp/%v", tempDir))
os.RemoveAll(fmt.Sprintf("%v", tempDir))
return nil
}
10 changes: 6 additions & 4 deletions internal/docker/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync/atomic"
"time"
Expand Down Expand Up @@ -102,8 +103,8 @@ func CheckWithDockerImage(c *cli.Context) error {
registrySizeMap := make(map[string]int64)
timeout := c.Int("timeout")
imageName := c.Args().First()
tempDir := time.Now().UnixMilli()

rand := time.Now().UnixMilli()
tempDir := common.AddPathToDir(common.GetTempDir(), strconv.Itoa(int(rand)))
fmt.Printf("\nTimeout: %d seconds\n", timeout)
fmt.Printf("Docker Image: %s\n\n", imageName)

Expand Down Expand Up @@ -147,11 +148,12 @@ func CheckWithDockerImage(c *cli.Context) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()

size, err := DownloadDockerImage(ctx, imageName, registry, fmt.Sprintf("/tmp/%v", tempDir))
size, err := DownloadDockerImage(ctx, imageName, registry, fmt.Sprintf("%v", tempDir))
if err != nil {
fmt.Printf("| %-*s | %s%-16s%s |\n",
maxLength, registry,
common.Red, "failed", common.Reset)
// fmt.Println(err)
continue
}

Expand Down Expand Up @@ -181,6 +183,6 @@ func CheckWithDockerImage(c *cli.Context) error {
fmt.Println("No registry was able to download any data.")
}

os.RemoveAll(fmt.Sprintf("/tmp/%v", tempDir))
os.RemoveAll(fmt.Sprintf("%v", tempDir))
return nil
}

0 comments on commit 283f5dc

Please sign in to comment.