2023-03-09 23:26:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"grow.rievo.dev/discordBots/cmd/domaincheckbot/config"
|
2023-11-05 16:03:52 +00:00
|
|
|
"log/slog"
|
2023-03-09 23:26:08 +00:00
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-11-05 16:03:52 +00:00
|
|
|
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
|
|
|
|
2023-03-09 23:26:08 +00:00
|
|
|
func main() {
|
2023-11-05 16:03:52 +00:00
|
|
|
logger.Info("add domains to domains.json")
|
2023-03-09 23:26:08 +00:00
|
|
|
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
|
2024-07-15 19:37:06 +00:00
|
|
|
var newDomains [][]string
|
2023-03-09 23:26:08 +00:00
|
|
|
|
2024-07-15 19:37:06 +00:00
|
|
|
fmt.Print("domains (tld separated by ';'): ")
|
2023-03-09 23:26:08 +00:00
|
|
|
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]
|
|
|
|
})
|
2024-07-15 19:37:06 +00:00
|
|
|
for _, value := range values {
|
|
|
|
newDomains = append(newDomains, strings.Split(value, ";"))
|
|
|
|
}
|
2023-03-09 23:26:08 +00:00
|
|
|
}
|
2023-11-05 16:03:52 +00:00
|
|
|
logger.Info("domains to add", slog.Any("domains", newDomains))
|
2023-03-09 23:26:08 +00:00
|
|
|
|
|
|
|
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 {
|
2023-11-05 16:03:52 +00:00
|
|
|
logger.Error("failed storing domains.json", slog.Any("error", err))
|
|
|
|
return
|
2023-03-09 23:26:08 +00:00
|
|
|
}
|
|
|
|
fmt.Println("domains.json updated")
|
|
|
|
} else {
|
|
|
|
fmt.Println("canceled!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-15 19:37:06 +00:00
|
|
|
func storeDomains(domains [][]string) error {
|
2023-03-09 23:26:08 +00:00
|
|
|
for _, domain := range domains {
|
2024-07-15 19:37:06 +00:00
|
|
|
config.AddDomain(domain[0], domain[1])
|
2023-03-09 23:26:08 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|