-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchlist.go
75 lines (61 loc) · 1.58 KB
/
watchlist.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
package main
import (
"fmt"
"os"
"strings"
)
type WatchListOptions struct {
ConfigPath string
}
// WatchListOption represents a functional option for configuring WatchListOptions.
type WatchListOption func(*WatchListOptions)
// getWatchList retrieves the watch list.
func getWatchList(options ...WatchListOption) []string {
// Default options
opt := &WatchListOptions{
ConfigPath: "./ggs.config",
}
// Open the file
file, err := os.Open(opt.ConfigPath)
if err != nil {
// Handle error, e.g., log it or return default value
return []string{}
}
defer file.Close()
// Read content from the file into a string variable
var content string
if _, err := fmt.Fscan(file, &content); err != nil {
// Handle error, e.g., log it or return default value
return []string{}
}
// Split the content into an array by splitting with ","
watchList := strings.Split(content, ",")
// Remove leading and trailing whitespaces from each element
for i, item := range watchList {
watchList[i] = strings.TrimSpace(item)
}
return watchList
}
func updateWatchList(content string, options ...WatchListOption) bool {
opt := &WatchListOptions{
ConfigPath: "./ggs.config",
}
// Apply custom options if provided
for _, option := range options {
option(opt)
}
// Open the file in write mode, create if not exists
file, err := os.Create(opt.ConfigPath)
if err != nil {
// Handle error, e.g., log it
return false
}
defer file.Close()
// Write content to the file
_, err = fmt.Fprintf(file, "%s", content)
if err != nil {
// Handle error, e.g., log it
return false
}
return true
}