// Load config values from file func LoadConfig(filename string) { LoadDefaults() log.Printf("\nLoading Configuration from %v\n", filename) config, err := configparser.Read(filename) fmt.Print(config) if err != nil { log.Fatal(err) } serviceSection, err := config.Section("Service") if err != nil { log.Fatal(err) } dbSection, err := config.Section("Database") if err != nil { log.Fatal(err) } logSection, err := config.Section("Logging") if err != nil { log.Fatal(err) } authSection, err := config.Section("Auth") if err != nil { log.Fatal(err) } registrySection, err := config.Section("ServiceRegistry") if err != nil { log.Fatal(err) } //Optional sections frontendSection, err := config.Section("Frontend") userSection, err := config.Section("Users") searchSection, err := config.Section("Search") notifSection, err := config.Section("Notifications") setServiceConfig(serviceSection) if frontendSection != nil { SetFrontendConfig(frontendSection) } if searchSection != nil { SetSearchConfig(searchSection) } if userSection != nil { setUsersConfig(userSection) } if notifSection != nil { setNotificationConfig(notifSection) } setDbConfig(dbSection) setLogConfig(logSection) setAuthConfig(authSection) setRegistryConfig(registrySection) }
// Loads Plugin Data from the plugins ini file func LoadPluginData(filename string) { readSinglePluginData := func(pluginSection *configparser.Section) *PluginData { theData := NewPluginData() for key, value := range pluginSection.Options() { switch key { case "name": theData.Name = value case "author": theData.Author = value case "version": theData.Version = value case "pluginDir": theData.PluginDir = value case "mainScript": theData.MainScript = value case "stylesheet": theData.Stylesheet = value case "enabled": if value == "true" { theData.Enabled = true } else { theData.Enabled = false } } } return theData } config, err := configparser.Read(filename) if err != nil { log.Fatal(err) } fmt.Print(config) pluginSections, err := config.Find(".plugin$") if err != nil { log.Fatal(err) } for _, section := range pluginSections { theData := readSinglePluginData(section) if theData.Enabled { enabledPlugins[theData.Name] = *theData } } }
// Read and modify a configuration file func Example() { config, err := configparser.Read("/etc/config.ini") if err != nil { log.Fatal(err) } // Print the full configuration fmt.Println(config) // get a section section, err := config.Section("MYSQLD DEFAULT") if err != nil { log.Fatal(err) } else { fmt.Printf("TotalSendBufferMemory=%s\n", section.ValueOf("TotalSendBufferMemory")) // set new value var oldValue = section.SetValueFor("TotalSendBufferMemory", "256M") fmt.Printf("TotalSendBufferMemory=%s, old value=%s\n", section.ValueOf("TotalSendBufferMemory"), oldValue) // delete option oldValue = section.Delete("DefaultOperationRedoProblemAction") fmt.Println("Deleted DefaultOperationRedoProblemAction: " + oldValue) // add new options section.Add("innodb_buffer_pool_size", "64G") section.Add("innodb_buffer_pool_instances", "8") } // add a new section and options section = config.NewSection("NDBD MGM") section.Add("NodeId", "2") section.Add("HostName", "10.10.10.10") section.Add("PortNumber", "1186") section.Add("ArbitrationRank", "1") // find all sections ending with .webservers sections, err := config.Find(".webservers$") if err != nil { log.Fatal(err) } for _, section := range sections { fmt.Print(section) } // or config.PrintSection("dc1.webservers") sections, err = config.Delete("NDB_MGMD DEFAULT") if err != nil { log.Fatal(err) } // deleted sections for _, section := range sections { fmt.Print(section) } options := section.Options() fmt.Println(options["HostName"]) // save the new config. the original will be renamed to /etc/config.ini.bak err = configparser.Save(config, "/etc/config.ini") if err != nil { log.Fatal(err) } }