mirror of
https://github.com/fosrl/newt.git
synced 2026-02-08 05:56:40 +00:00
Try to fix overwriting config file
This commit is contained in:
@@ -37,6 +37,7 @@ type Client struct {
|
|||||||
writeMux sync.Mutex
|
writeMux sync.Mutex
|
||||||
clientType string // Type of client (e.g., "newt", "olm")
|
clientType string // Type of client (e.g., "newt", "olm")
|
||||||
tlsConfig TLSConfig
|
tlsConfig TLSConfig
|
||||||
|
configNeedsSave bool // Flag to track if config needs to be saved
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientOption func(*Client)
|
type ClientOption func(*Client)
|
||||||
|
|||||||
@@ -35,15 +35,25 @@ func getConfigPath(clientType string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) loadConfig() error {
|
func (c *Client) loadConfig() error {
|
||||||
|
originalConfig := *c.config // Store original config to detect changes
|
||||||
|
configPath := getConfigPath(c.clientType)
|
||||||
|
|
||||||
if c.config.ID != "" && c.config.Secret != "" && c.config.Endpoint != "" {
|
if c.config.ID != "" && c.config.Secret != "" && c.config.Endpoint != "" {
|
||||||
logger.Debug("Config already provided, skipping loading from file")
|
logger.Debug("Config already provided, skipping loading from file")
|
||||||
|
// Check if config file exists, if not, we should save it
|
||||||
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||||
|
logger.Info("Config file does not exist at %s, will create it", configPath)
|
||||||
|
c.configNeedsSave = true
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
configPath := getConfigPath(c.clientType)
|
logger.Info("Loading config from: %s", configPath)
|
||||||
data, err := os.ReadFile(configPath)
|
data, err := os.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
|
logger.Info("Config file does not exist at %s, will create it with provided values", configPath)
|
||||||
|
c.configNeedsSave = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
@@ -54,6 +64,12 @@ func (c *Client) loadConfig() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Track what was loaded from file vs provided by CLI
|
||||||
|
fileHadID := c.config.ID == ""
|
||||||
|
fileHadSecret := c.config.Secret == ""
|
||||||
|
fileHadCert := c.config.TlsClientCert == ""
|
||||||
|
fileHadEndpoint := c.config.Endpoint == ""
|
||||||
|
|
||||||
if c.config.ID == "" {
|
if c.config.ID == "" {
|
||||||
c.config.ID = config.ID
|
c.config.ID = config.ID
|
||||||
}
|
}
|
||||||
@@ -68,6 +84,15 @@ func (c *Client) loadConfig() error {
|
|||||||
c.baseURL = config.Endpoint
|
c.baseURL = config.Endpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if CLI args provided values that override file values
|
||||||
|
if (!fileHadID && originalConfig.ID != "") ||
|
||||||
|
(!fileHadSecret && originalConfig.Secret != "") ||
|
||||||
|
(!fileHadCert && originalConfig.TlsClientCert != "") ||
|
||||||
|
(!fileHadEndpoint && originalConfig.Endpoint != "") {
|
||||||
|
logger.Info("CLI arguments provided, config will be updated")
|
||||||
|
c.configNeedsSave = true
|
||||||
|
}
|
||||||
|
|
||||||
logger.Debug("Loaded config from %s", configPath)
|
logger.Debug("Loaded config from %s", configPath)
|
||||||
logger.Debug("Config: %+v", c.config)
|
logger.Debug("Config: %+v", c.config)
|
||||||
|
|
||||||
@@ -75,10 +100,21 @@ func (c *Client) loadConfig() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) saveConfig() error {
|
func (c *Client) saveConfig() error {
|
||||||
|
if !c.configNeedsSave {
|
||||||
|
logger.Debug("Config has not changed, skipping save")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
configPath := getConfigPath(c.clientType)
|
configPath := getConfigPath(c.clientType)
|
||||||
data, err := json.MarshalIndent(c.config, "", " ")
|
data, err := json.MarshalIndent(c.config, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return os.WriteFile(configPath, data, 0644)
|
|
||||||
|
logger.Info("Saving config to: %s", configPath)
|
||||||
|
err = os.WriteFile(configPath, data, 0644)
|
||||||
|
if err == nil {
|
||||||
|
c.configNeedsSave = false // Reset flag after successful save
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user