discordBots/cmd/domaincheckadd/main.go
2024-07-15 19:39:14 +00:00

82 lines
1.6 KiB
Go

package main
import (
"bufio"
"encoding/json"
"fmt"
"grow.rievo.dev/discordBots/cmd/domaincheckbot/config"
"log/slog"
"os"
"sort"
"strings"
)
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
func main() {
logger.Info("add domains to domains.json")
scanner := bufio.NewScanner(os.Stdin)
var newDomains [][]string
fmt.Print("domains (tld separated by ';'): ")
if scanner.Scan() {
input := scanner.Text()
values := strings.Split(input, ",")
// trim space
for i := range values {
values[i] = strings.TrimSpace(values[i])
}
// remove empty
values = removeEmpty(values)
// sort
sort.SliceStable(values, func(i, j int) bool {
return values[i] < values[j]
})
for _, value := range values {
newDomains = append(newDomains, strings.Split(value, ";"))
}
}
logger.Info("domains to add", slog.Any("domains", newDomains))
fmt.Print("add to domains.json? [y|N]: ")
if scanner.Scan() {
input := scanner.Text()
if input == "y" || input == "Y" {
err := storeDomains(newDomains)
if err != nil {
logger.Error("failed storing domains.json", slog.Any("error", err))
return
}
fmt.Println("domains.json updated")
} else {
fmt.Println("canceled!")
}
}
}
func storeDomains(domains [][]string) error {
for _, domain := range domains {
config.AddDomain(domain[0], domain[1])
}
file, _ := json.MarshalIndent(config.Domains, "", " ")
err := os.WriteFile("./cmd/domaincheckbot/config/domain.json", file, 0644)
return err
}
func removeEmpty(sliceList []string) []string {
var list []string
for _, str := range sliceList {
if str != "" {
list = append(list, str)
}
}
return list
}