Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
add config for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 7, 2022
1 parent afc91eb commit 4392d6f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 27 deletions.
30 changes: 3 additions & 27 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package config

import (
"errors"
"os"
"path/filepath"
"sync"
)

Expand All @@ -13,30 +10,9 @@ type config struct {
SysHostFile string
}

func New() *config {
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
baseDir := filepath.Join(homeDir, ".gohost")
_, err = os.Stat(baseDir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
if err = os.MkdirAll(baseDir, os.ModePerm); err != nil {
panic(err)
}
} else {
panic(err)
}
}
dbFile := filepath.Join(baseDir, "gohost.db")
sysHostFile := "/etc/hosts"
return &config{
BaseDir: baseDir,
DBFile: dbFile,
SysHostFile: sysHostFile,
}
}
const (
name = "gohost"
)

var (
cfg *config
Expand Down
24 changes: 24 additions & 0 deletions config/unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build linux || darwin

package config

import (
"os"
"path/filepath"
)

func New() *config {
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
baseDir := filepath.Join(homeDir, ".local", "share", name)
if err = os.MkdirAll(baseDir, os.ModePerm); err != nil {
panic(err)
}
return &config{
BaseDir: baseDir,
DBFile: filepath.Join(baseDir, "gohost.db"),
SysHostFile: "/etc/hosts",
}
}
24 changes: 24 additions & 0 deletions config/windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build windows

package config

import (
"os"
"path/filepath"
)

func New() *config {
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
baseDir := filepath.Join(homeDir, name)
if err = os.MkdirAll(baseDir, os.ModePerm); err != nil {
panic(err)
}
return &config{
BaseDir: baseDir,
DBFile: filepath.Join(baseDir, "gohost.db"),
SysHostFile: "C:\\Windows\\System32\\drivers\\etc\\hosts",
}
}

0 comments on commit 4392d6f

Please sign in to comment.