Add --config-file

This commit is contained in:
Owen
2026-03-26 17:31:04 -07:00
parent b43572dd8d
commit baca04ee58
5 changed files with 29 additions and 3 deletions

4
config.json Normal file
View File

@@ -0,0 +1,4 @@
{
"endpoint": "http://you.fosrl.io",
"provisioningKey": "spk-xt1opb0fkoqb7qb.hi44jciamqcrdaja4lvz3kp52pl3lssamp6asuyx"
}

4
config.json.bak Normal file
View File

@@ -0,0 +1,4 @@
{
"endpoint": "http://you.fosrl.io",
"provisioningKey": "spk-xt1opb0fkoqb7qb.hi44jciamqcrdaja4lvz3kp52pl3lssamp6asuyx"
}

View File

@@ -162,6 +162,9 @@ var (
// Provisioning key exchanged once for a permanent newt ID + secret // Provisioning key exchanged once for a permanent newt ID + secret
provisioningKey string provisioningKey string
// Path to config file (overrides CONFIG_FILE env var and default location)
configFile string
) )
func main() { func main() {
@@ -268,6 +271,7 @@ func runNewtMain(ctx context.Context) {
noCloudEnv := os.Getenv("NO_CLOUD") noCloudEnv := os.Getenv("NO_CLOUD")
noCloud = noCloudEnv == "true" noCloud = noCloudEnv == "true"
provisioningKey = os.Getenv("NEWT_PROVISIONING_KEY") provisioningKey = os.Getenv("NEWT_PROVISIONING_KEY")
configFile = os.Getenv("CONFIG_FILE")
if endpoint == "" { if endpoint == "" {
flag.StringVar(&endpoint, "endpoint", "", "Endpoint of your pangolin server") flag.StringVar(&endpoint, "endpoint", "", "Endpoint of your pangolin server")
@@ -319,6 +323,9 @@ func runNewtMain(ctx context.Context) {
if provisioningKey == "" { if provisioningKey == "" {
flag.StringVar(&provisioningKey, "provisioning-key", "", "One-time provisioning key used to obtain a newt ID and secret from the server") flag.StringVar(&provisioningKey, "provisioning-key", "", "One-time provisioning key used to obtain a newt ID and secret from the server")
} }
if configFile == "" {
flag.StringVar(&configFile, "config-file", "", "Path to config file (overrides CONFIG_FILE env var and default location)")
}
// Add new mTLS flags // Add new mTLS flags
if tlsClientCert == "" { if tlsClientCert == "" {
@@ -593,6 +600,7 @@ func runNewtMain(ctx context.Context) {
endpoint, endpoint,
30*time.Second, 30*time.Second,
opt, opt,
websocket.WithConfigFile(configFile),
) )
if err != nil { if err != nil {
logger.Fatal("Failed to create client: %v", err) logger.Fatal("Failed to create client: %v", err)

View File

@@ -42,6 +42,7 @@ type Client struct {
onTokenUpdate func(token string) onTokenUpdate func(token string)
writeMux sync.Mutex writeMux sync.Mutex
clientType string // Type of client (e.g., "newt", "olm") clientType string // Type of client (e.g., "newt", "olm")
configFilePath string // Optional override for the config file path
tlsConfig TLSConfig tlsConfig TLSConfig
metricsCtxMu sync.RWMutex metricsCtxMu sync.RWMutex
metricsCtx context.Context metricsCtx context.Context
@@ -77,6 +78,12 @@ func WithBaseURL(url string) ClientOption {
} }
// WithTLSConfig sets the TLS configuration for the client // WithTLSConfig sets the TLS configuration for the client
func WithConfigFile(path string) ClientOption {
return func(c *Client) {
c.configFilePath = path
}
}
func WithTLSConfig(config TLSConfig) ClientOption { func WithTLSConfig(config TLSConfig) ClientOption {
return func(c *Client) { return func(c *Client) {
c.tlsConfig = config c.tlsConfig = config

View File

@@ -19,7 +19,10 @@ import (
"github.com/fosrl/newt/logger" "github.com/fosrl/newt/logger"
) )
func getConfigPath(clientType string) string { func getConfigPath(clientType string, overridePath string) string {
if overridePath != "" {
return overridePath
}
configFile := os.Getenv("CONFIG_FILE") configFile := os.Getenv("CONFIG_FILE")
if configFile == "" { if configFile == "" {
var configDir string var configDir string
@@ -45,7 +48,7 @@ func getConfigPath(clientType string) string {
func (c *Client) loadConfig() error { func (c *Client) loadConfig() error {
originalConfig := *c.config // Store original config to detect changes originalConfig := *c.config // Store original config to detect changes
configPath := getConfigPath(c.clientType) configPath := getConfigPath(c.clientType, c.configFilePath)
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")
@@ -118,7 +121,7 @@ func (c *Client) saveConfig() error {
return nil return nil
} }
configPath := getConfigPath(c.clientType) configPath := getConfigPath(c.clientType, c.configFilePath)
data, err := json.MarshalIndent(c.config, "", " ") data, err := json.MarshalIndent(c.config, "", " ")
if err != nil { if err != nil {
return err return err