Documentation
¶
Overview ¶
Package ccconfig provides a simple configuration management system for the ClusterCockpit ecosystem.
The package supports loading JSON configuration files with a flexible structure that allows both inline configuration and external file references. Configuration sections can be accessed by key, enabling modular configuration management across different components.
Configuration File Structure ¶
Configuration files should follow this JSON structure:
{
"main": {
// Main configuration for the component
},
"foo": {
// Configuration for sub-component 'foo'
},
"bar-file": "path/to/bar.json"
}
File References ¶
Keys ending with "-file" are treated as file references. The value should be a string path to an external JSON file. The referenced file's content will be loaded and stored under the key prefix (without "-file" suffix).
For example, "sinks-file": "sinks.json" will load the content of sinks.json and make it available via GetPackageConfig("sinks").
Usage Example ¶
package main
import (
"encoding/json"
"log"
ccconfig "github.com/ClusterCockpit/cc-lib/ccConfig"
)
type AppConfig struct {
Interval string `json:"interval"`
Debug bool `json:"debug"`
}
func main() {
// Initialize with config file
ccconfig.Init("config.json")
// Get configuration for your package
rawConfig := ccconfig.GetPackageConfig("myapp")
if rawConfig == nil {
log.Fatal("myapp configuration not found")
}
// Unmarshal into your config struct
var config AppConfig
if err := json.Unmarshal(rawConfig, &config); err != nil {
log.Fatalf("failed to parse config: %v", err)
}
// Use the configuration
log.Printf("Running with interval: %s", config.Interval)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetKeys ¶ added in v0.11.0
func GetKeys() []string
GetKeys returns a list of all available configuration keys. This can be useful for debugging or for components that need to discover what configuration sections are available.
Returns:
- A slice of strings containing all configuration keys
Example:
keys := ccconfig.GetKeys()
for _, key := range keys {
fmt.Printf("Found config section: %s\n", key)
}
func GetPackageConfig ¶
func GetPackageConfig(key string) json.RawMessage
GetPackageConfig retrieves the raw JSON configuration for a given key. It returns the configuration as json.RawMessage which can then be unmarshaled into the appropriate structure.
If the key does not exist, it logs an informational message and returns nil. Callers should check for nil before attempting to unmarshal the result.
Parameters:
- key: The configuration key to retrieve
Returns:
- json.RawMessage containing the configuration data, or nil if the key doesn't exist
Example:
rawConfig := ccconfig.GetPackageConfig("database")
if rawConfig != nil {
var dbConfig DatabaseConfig
json.Unmarshal(rawConfig, &dbConfig)
}
func HasKey ¶ added in v0.11.0
HasKey checks whether a configuration key exists. This can be used to determine if a component's configuration was loaded before attempting to retrieve it.
Parameters:
- key: The configuration key to check
Returns:
- true if the key exists, false otherwise
Example:
if ccconfig.HasKey("optional-feature") {
config := ccconfig.GetPackageConfig("optional-feature")
// ... use config
}
func Init ¶
func Init(filename string)
Init initializes the configuration system by loading and parsing a JSON configuration file. It reads the specified file and processes all configuration sections. Keys ending with "-file" are treated as references to external files, which are loaded and stored under the base key name.
If the file does not exist, Init will silently continue with an empty configuration. Other errors (permission denied, invalid JSON, etc.) will cause the program to terminate with a fatal error message.
Parameters:
- filename: Path to the main JSON configuration file
Example:
ccconfig.Init("./config.json")
Types ¶
This section is empty.