-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
875ea6a
commit 5efc17b
Showing
7 changed files
with
129 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
.vscode | ||
vendor | ||
config.json | ||
whitelist.txt | ||
allowlist.txt | ||
out | ||
pkg_root | ||
go-musicbot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package bot | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"sync" | ||
) | ||
|
||
type AllowList struct { | ||
path string | ||
names map[string]struct{} | ||
lock sync.Mutex | ||
} | ||
|
||
func LoadAllowList(path string) (*AllowList, error) { | ||
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to open file %s: %v", path, err) | ||
} | ||
defer file.Close() | ||
|
||
list := make(map[string]struct{}) | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
list[scanner.Text()] = struct{}{} | ||
} | ||
|
||
err = scanner.Err() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
instance := &AllowList{ | ||
path: path, | ||
names: list, | ||
} | ||
|
||
return instance, nil | ||
} | ||
|
||
func (allowlist *AllowList) Write() error { | ||
allowlist.lock.Lock() | ||
defer allowlist.lock.Unlock() | ||
|
||
return allowlist.write() | ||
} | ||
|
||
func (allowlist *AllowList) write() error { | ||
file, err := os.Create(allowlist.path) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
w := bufio.NewWriter(file) | ||
for line := range allowlist.names { | ||
_, err = fmt.Fprintln(w, line) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
err = w.Flush() | ||
return err | ||
} | ||
|
||
func (allowlist *AllowList) Add(name string) error { | ||
allowlist.lock.Lock() | ||
defer allowlist.lock.Unlock() | ||
|
||
allowlist.names[name] = struct{}{} | ||
|
||
return allowlist.write() | ||
} | ||
|
||
func (allowlist *AllowList) Contains(name string) bool { | ||
_, exists := allowlist.names[name] | ||
|
||
return exists | ||
} | ||
|
||
func (allowlist *AllowList) Remove(name string) error { | ||
allowlist.lock.Lock() | ||
defer allowlist.lock.Unlock() | ||
|
||
_, exists := allowlist.names[name] | ||
|
||
if !exists { | ||
return nil | ||
} | ||
|
||
delete(allowlist.names, name) | ||
|
||
return allowlist.write() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.