change .json to .ini
This commit is contained in:
parent
5660167495
commit
5294421190
5 changed files with 59 additions and 52 deletions
99
init.go
99
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue