-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
51 lines (43 loc) · 1.38 KB
/
config.go
File metadata and controls
51 lines (43 loc) · 1.38 KB
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
package main
import (
"encoding/json"
"os"
)
// Configuration holds all the settings for the application.
// The list of domains to monitor is now stored in the database.
type Configuration struct {
DiscordBotToken string `json:"discord_bot_token"`
DiscordChannelID string `json:"discord_channel_id"`
CheckIntervalMinutes int `json:"check_interval_minutes"`
}
// LoadConfiguration reads a configuration file from the given path and decodes it
// into a Configuration struct.
func LoadConfiguration(filePath string) (*Configuration, error) {
// Read the file content
content, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
// Decode the JSON content into the struct
var config Configuration
err = json.Unmarshal(content, &config)
if err != nil {
return nil, err
}
return &config, nil
}
// createDefaultConfiguration creates a new config.json with default values.
func createDefaultConfiguration(filePath string) error {
defaultConfig := Configuration{
DiscordBotToken: "", // PLEASE FILL IN YOUR DISCORD BOT TOKEN
DiscordChannelID: "", // PLEASE FILL IN YOUR DISCORD CHANNEL ID
CheckIntervalMinutes: 60,
}
// Marshal the struct into a nicely formatted JSON string
content, err := json.MarshalIndent(defaultConfig, "", " ")
if err != nil {
return err
}
// Write the content to the file
return os.WriteFile(filePath, content, 0644)
}