-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
104 lines (89 loc) · 2.21 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
type Config struct {
HostsFile string `yaml:"hosts_file"`
Domains []string `yaml:"domains"`
Subdomains []string `yaml:"subdomains"`
}
func LoadConfig(path string) (*Config, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
// Configuration file does not exist; create a default one
err = CreateDefaultConfig(path)
if err != nil {
return nil, fmt.Errorf("failed to create default configuration file: %v", err)
}
// Try reading the file again after creating it
data, err = ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read configuration file after creating it: %v", err)
}
} else {
return nil, err
}
}
var config Config
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, err
}
// Set default hosts file if not specified
if config.HostsFile == "" {
config.HostsFile = "/etc/hosts"
}
return &config, nil
}
func CreateDefaultConfig(path string) error {
defaultConfig := Config{
Domains: []string{
"x.com",
"twitter.com",
"youtube.com",
"reddit.com",
},
Subdomains: []string{
"blog.example.com",
"mail.example.org",
},
}
data, err := yaml.Marshal(&defaultConfig)
if err != nil {
return fmt.Errorf("failed to marshal default configuration: %v", err)
}
// Ensure the configuration directory exists
err = EnsureConfigDir()
if err != nil {
return fmt.Errorf("failed to ensure configuration directory: %v", err)
}
// Write the default configuration file
err = ioutil.WriteFile(path, data, 0644)
if err != nil {
return fmt.Errorf("failed to write default configuration file: %v", err)
}
// Change ownership of the configuration file
uid, gid, err := getOriginalUserIDs()
if err == nil {
os.Chown(path, uid, gid)
}
fmt.Println("Default configuration file created at", path)
return nil
}
func GetHomeDir() string {
home, err := os.UserHomeDir()
if err != nil {
logError(err)
fmt.Println("Error getting home directory:", err)
os.Exit(1)
}
return home
}
func EnsureConfigDir() error {
configDir := filepath.Join(GetHomeDir(), ".dblock")
return EnsureDir(configDir)
}