byom.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package config
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. )
  9. // ByomConfig holds the configuration key-value pairs
  10. var ByomConfig map[string]string
  11. // InitByomConfig initializes the configuration by reading from a byom.conf file
  12. func InitByomConfig() error {
  13. // Initialize the map
  14. ByomConfig = make(map[string]string)
  15. // Get absolute path to byom.conf in current directory
  16. configPath, err := filepath.Abs("./byom.conf")
  17. if err != nil {
  18. return fmt.Errorf("error getting config path: %v", err)
  19. }
  20. // Open the configuration file
  21. file, err := os.Open(configPath)
  22. if err != nil {
  23. return fmt.Errorf("error opening config file: %v", err)
  24. }
  25. defer file.Close()
  26. // Read the file line by line
  27. scanner := bufio.NewScanner(file)
  28. for scanner.Scan() {
  29. line := strings.TrimSpace(scanner.Text())
  30. // Skip empty lines and comments
  31. if line == "" || strings.HasPrefix(line, "#") {
  32. continue
  33. }
  34. // Split the line into key and value
  35. parts := strings.SplitN(line, "=", 2)
  36. if len(parts) != 2 {
  37. continue // Skip invalid lines
  38. }
  39. key := strings.TrimSpace(parts[0])
  40. value := strings.TrimSpace(parts[1])
  41. // Store in the configuration map
  42. ByomConfig[key] = value
  43. }
  44. if err := scanner.Err(); err != nil {
  45. return fmt.Errorf("error reading config file: %v", err)
  46. }
  47. return nil
  48. }
  49. // GetConfig returns the value for a given configuration key
  50. func GetConfig(key string) (string, bool) {
  51. value, exists := ByomConfig[key]
  52. return value, exists
  53. }