discordBots/cmd/domaincheckadd/main.go

79 lines
1.4 KiB
Go
Raw Normal View History

package main
import (
"bufio"
"encoding/json"
"fmt"
"grow.rievo.dev/discordBots/cmd/domaincheckbot/config"
"log"
"os"
"sort"
"strings"
)
func main() {
log.Println("add domains to domains.json")
scanner := bufio.NewScanner(os.Stdin)
var newDomains []string
fmt.Print("domains: ")
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]
})
newDomains = values
}
log.Printf("domains to add: %v\n", 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 {
log.Fatal("failed storing domains.json")
}
fmt.Println("domains.json updated")
} else {
fmt.Println("canceled!")
}
}
}
func storeDomains(domains []string) error {
for _, domain := range domains {
config.AddDomain(domain)
}
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
}