123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package config
- import (
- "bufio"
- "fmt"
- "os"
- "path/filepath"
- "strings"
- )
- // ByomConfig holds the configuration key-value pairs
- var ByomConfig map[string]string
- // InitByomConfig initializes the configuration by reading from a byom.conf file
- func InitByomConfig() error {
- // Initialize the map
- ByomConfig = make(map[string]string)
- // Get absolute path to byom.conf in current directory
- configPath, err := filepath.Abs("./byom.conf")
- if err != nil {
- return fmt.Errorf("error getting config path: %v", err)
- }
- // Open the configuration file
- file, err := os.Open(configPath)
- if err != nil {
- return fmt.Errorf("error opening config file: %v", err)
- }
- defer file.Close()
- // Read the file line by line
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- line := strings.TrimSpace(scanner.Text())
- // Skip empty lines and comments
- if line == "" || strings.HasPrefix(line, "#") {
- continue
- }
- // Split the line into key and value
- parts := strings.SplitN(line, "=", 2)
- if len(parts) != 2 {
- continue // Skip invalid lines
- }
- key := strings.TrimSpace(parts[0])
- value := strings.TrimSpace(parts[1])
- // Store in the configuration map
- ByomConfig[key] = value
- }
- if err := scanner.Err(); err != nil {
- return fmt.Errorf("error reading config file: %v", err)
- }
- return nil
- }
- // GetConfig returns the value for a given configuration key
- func GetConfig(key string) (string, bool) {
- value, exists := ByomConfig[key]
- return value, exists
- }
|