diff --git a/.gitignore b/.gitignore index 0034634..41819de 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ config.json -opensearch.xml \ No newline at end of file +opensearch.xml +config.ini \ No newline at end of file diff --git a/go.mod b/go.mod index 5dcd6b3..f6755b1 100644 --- a/go.mod +++ b/go.mod @@ -22,4 +22,7 @@ require ( golang.org/x/time v0.5.0 // indirect ) -require github.com/fsnotify/fsnotify v1.7.0 // indirect +require ( + github.com/fsnotify/fsnotify v1.7.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect +) diff --git a/go.sum b/go.sum index 732af13..7fab23f 100644 --- a/go.sum +++ b/go.sum @@ -71,3 +71,5 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= diff --git a/init.go b/init.go index 8872f57..915bd3c 100644 --- a/init.go +++ b/init.go @@ -4,7 +4,6 @@ import ( "bufio" "crypto/rand" "encoding/base64" - "encoding/json" "fmt" "log" "os" @@ -14,28 +13,23 @@ import ( "time" "github.com/fsnotify/fsnotify" + "gopkg.in/ini.v1" ) type Config struct { - Port int - AuthCode string - Peers []string - PeerID string - OpenSearch OpenSearchConfig -} - -type OpenSearchConfig struct { - Domain string + Port int + AuthCode string + Peers []string + PeerID string + Domain string } var defaultConfig = Config{ - Port: 5000, - OpenSearch: OpenSearchConfig{ - Domain: "localhost", - }, + Port: 5000, + Domain: "localhost", } -const configFilePath = "config.json" +const configFilePath = "config.ini" var config Config var configLock sync.RWMutex @@ -91,33 +85,25 @@ func createConfig() error { fmt.Print("Do you want to use default values? (yes/no): ") useDefaults, _ := reader.ReadString('\n') - config := defaultConfig - if useDefaults != "yes\n" { + if strings.TrimSpace(useDefaults) != "yes" { fmt.Print("Enter port (default 5000): ") portStr, _ := reader.ReadString('\n') if portStr != "\n" { - port, err := strconv.Atoi(portStr[:len(portStr)-1]) + port, err := strconv.Atoi(strings.TrimSpace(portStr)) if err != nil { - return err + config.Port = 5000 + } else { + config.Port = port } - config.Port = port } - fmt.Print("Enter your domain address (e.g., domain.com): ") + fmt.Print("Enter your domain address (default localhost): ") domain, _ := reader.ReadString('\n') if domain != "\n" { - config.OpenSearch.Domain = domain[:len(domain)-1] - } - - fmt.Print("Do you want to connect to other nodes? (yes/no): ") - connectNodes, _ := reader.ReadString('\n') - if strings.TrimSpace(connectNodes) == "yes" { - fmt.Println("Enter peer addresses (comma separated, e.g., /ip4/127.0.0.1/tcp/5000,/ip4/127.0.0.1/tcp/5001): ") - peersStr, _ := reader.ReadString('\n') - if peersStr != "\n" { - config.Peers = strings.Split(strings.TrimSpace(peersStr), ",") - } + config.Domain = strings.TrimSpace(domain) } + } else { + config = defaultConfig } if config.AuthCode == "" { @@ -130,35 +116,50 @@ func createConfig() error { } func saveConfig(config Config) { - file, err := os.Create(configFilePath) - if err != nil { - fmt.Println("Error creating config file:", err) - return - } - defer file.Close() + cfg := ini.Empty() + sec := cfg.Section("") + sec.Key("Port").SetValue(strconv.Itoa(config.Port)) + sec.Key("AuthCode").SetValue(config.AuthCode) + sec.Key("PeerID").SetValue(config.PeerID) - configData, err := json.MarshalIndent(config, "", " ") - if err != nil { - fmt.Println("Error marshalling config data:", err) - return - } + peers := strings.Join(config.Peers, ",") + sec.Key("Peers").SetValue(peers) + sec.Key("Domain").SetValue(config.Domain) - _, err = file.Write(configData) + err := cfg.SaveTo(configFilePath) if err != nil { fmt.Println("Error writing to config file:", err) } } func loadConfig() Config { - configFile, err := os.Open(configFilePath) + cfg, err := ini.Load(configFilePath) if err != nil { log.Fatalf("Error opening config file: %v", err) } - defer configFile.Close() - var config Config - if err := json.NewDecoder(configFile).Decode(&config); err != nil { - log.Fatalf("Error decoding config file: %v", err) + port, err := cfg.Section("").Key("Port").Int() + if err != nil || port == 0 { + port = 5000 // Default to 5000 if not set or error + } + + peersStr := cfg.Section("").Key("Peers").String() + var peers []string + if peersStr != "" { + peers = strings.Split(peersStr, ",") + } + + domain := cfg.Section("").Key("Domain").String() + if domain == "" { + domain = "localhost" // Default to localhost if not set + } + + config = Config{ + Port: port, + AuthCode: cfg.Section("").Key("AuthCode").String(), + PeerID: cfg.Section("").Key("PeerID").String(), + Peers: peers, + Domain: domain, } return config diff --git a/open-search.go b/open-search.go index b685e42..2d5d0b4 100644 --- a/open-search.go +++ b/open-search.go @@ -28,7 +28,7 @@ func generateOpenSearchXML(config Config) { Tags: "search, engine", URL: URL{ Type: "text/html", - Template: fmt.Sprintf("https://%s/search?q={searchTerms}", config.OpenSearch.Domain), + Template: fmt.Sprintf("https://%s/search?q={searchTerms}", config.Domain), }, }