Add CreateConfigurationFile and util funcs
This commit is contained in:
		
							parent
							
								
									228d772219
								
							
						
					
					
						commit
						7d010c4b7b
					
				
							
								
								
									
										44
									
								
								ini.go
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								ini.go
									
									
									
									
									
								
							@ -13,6 +13,10 @@ type Configuration struct {
 | 
				
			|||||||
	file         *ini.File
 | 
						file         *ini.File
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func CreateConfigurationFile(fileLocation string, defaultConfig string) {
 | 
				
			||||||
 | 
						createFile(fileLocation, []byte(defaultConfig))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewConfiguration(fileLocation string, defaultConfig string) *Configuration {
 | 
					func NewConfiguration(fileLocation string, defaultConfig string) *Configuration {
 | 
				
			||||||
	conf := new(Configuration)
 | 
						conf := new(Configuration)
 | 
				
			||||||
	conf.fileLocation = fileLocation
 | 
						conf.fileLocation = fileLocation
 | 
				
			||||||
@ -214,24 +218,7 @@ func (config *Configuration) IsSet(path string) bool {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (config *Configuration) createAndLoad(defaultConfig string) {
 | 
					func (config *Configuration) createAndLoad(defaultConfig string) {
 | 
				
			||||||
	_, err := os.Stat(config.fileLocation)
 | 
						createFile(config.fileLocation, []byte(defaultConfig))
 | 
				
			||||||
	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(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()
 | 
						config.load()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -251,3 +238,24 @@ func (config *Configuration) Save() {
 | 
				
			|||||||
		os.Exit(1)
 | 
							os.Exit(1)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func createFile(fileLocation string, data []byte) {
 | 
				
			||||||
 | 
						_, err := os.Stat(fileLocation)
 | 
				
			||||||
 | 
						if errors.Is(err, os.ErrNotExist) {
 | 
				
			||||||
 | 
							file, err := os.Create(fileLocation)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("Failed to create file: %v", err)
 | 
				
			||||||
 | 
								os.Exit(1)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							_, err = file.Write(data)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("Error writing to file: %v", err)
 | 
				
			||||||
 | 
								os.Exit(1)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("Error creating file: %v", err)
 | 
				
			||||||
 | 
							os.Exit(1)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							fmt.Printf("File already exists...")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user