Small Fixes
This commit is contained in:
		
							parent
							
								
									88e928e51a
								
							
						
					
					
						commit
						5e1e8723eb
					
				
							
								
								
									
										25
									
								
								ini.go
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								ini.go
									
									
									
									
									
								
							@ -13,7 +13,14 @@ type Configuration struct {
 | 
			
		||||
	file         *ini.File
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewConfiguration(fileLocation string) *Configuration {
 | 
			
		||||
func NewConfiguration(fileLocation string, defaultConfig string) *Configuration {
 | 
			
		||||
	conf := new(Configuration)
 | 
			
		||||
	conf.fileLocation = fileLocation
 | 
			
		||||
	conf.createAndLoad(defaultConfig)
 | 
			
		||||
	return conf
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func LoadConfiguration(fileLocation string) *Configuration {
 | 
			
		||||
	conf := new(Configuration)
 | 
			
		||||
	conf.fileLocation = fileLocation
 | 
			
		||||
	conf.load()
 | 
			
		||||
@ -197,19 +204,29 @@ func (config *Configuration) GetAsBooleans(path string) []bool {
 | 
			
		||||
	return key.Bools(",")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (config *Configuration) load() {
 | 
			
		||||
	if _, err := os.Stat(config.fileLocation); err != nil {
 | 
			
		||||
func (config *Configuration) createAndLoad(defaultConfig string) {
 | 
			
		||||
	_, err := os.Stat(config.fileLocation)
 | 
			
		||||
	if errors.Is(err, os.ErrNotExist) {
 | 
			
		||||
		file, err := os.Create(config.fileLocation)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Printf("Failed to create configuration file: %v", err)
 | 
			
		||||
			os.Exit(1)
 | 
			
		||||
		}
 | 
			
		||||
		_, err = file.WriteString("[General]\nserver = sfs.example.com\nport = 7392\nusername = user\nauth-token = token\n; http:// or https:// is necessary\n; port is optional\nhttp-server = http://sfs.example.com:80\npublic-key =\n\n[Share]\nsync-interval = 60")
 | 
			
		||||
		_, err = file.WriteString(defaultConfig)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Printf("Error creating configuration file: %v", err)
 | 
			
		||||
			os.Exit(1)
 | 
			
		||||
		}
 | 
			
		||||
	} else if err != nil {
 | 
			
		||||
		fmt.Printf("Error loading configuration file: %v", err)
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Printf("Configuration File already exists... loading")
 | 
			
		||||
	}
 | 
			
		||||
	config.load()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (config *Configuration) load() {
 | 
			
		||||
	var err error
 | 
			
		||||
	config.file, err = ini.Load(config.fileLocation)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user