package main import ( "bytes" "fmt" //"os" "io/ioutil" //"strconv" "github.com/BurntSushi/toml" "github.com/atrox/homedir" "github.com/fatih/color" ) type Config struct { Database string //`toml:"database"` Name string //`toml:"name"` Street string //`toml:"street"` Zip string //`toml:"zip"` City string //`toml:"city"` Country string //`toml:"country"` Telefon string //`toml:"telefon"` Mobile string //`toml:"mobile"` Mail string //`toml:"mail"` Url string //`toml:"url"` Taxid string //`toml:"taxid"` Bankacc string //`toml:"bankacc"` Banklz string //`toml:"banklz"` Bankname string //`toml:"bankname"` Iban string //`toml:"iban"` Bic string //`toml:"bic"` } var conffile string var config Config func init() { conffile, err = homedir.Expand("~/.laboravi.config.toml") //make ~ when finished checkErr(err) } func editConf() { for { if _, err := toml.DecodeFile(conffile, &config); err != nil { //fmt.Println(err) fmt.Println("Configuration file not found") makeNewTOML(false) } else { showConfig() if isInterSure("Is this Configuration Correct?") { break } else { //if isSure("Enter new Configuration?"){ makeNewTOML(true) //}else{ // fmt.Println("I Really Dont think I can Help You then...!") //} } } } } func initConf() { boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() boldRed := color.New(color.FgRed, color.Bold).SprintFunc() for { if _, err := toml.DecodeFile(conffile, &config); err != nil { //fmt.Println(err) fmt.Println(boldRed("Configuration file not found")) makeNewTOML(false) } else { //fmt.Println(boldGreen("Configuration loaded Successfully")) fmt.Println("Config loaded...", boldGreen("Hello ", config.Name)) break } } } func makeNewTOML(gibts bool) { if gibts { fmt.Println("Editing Configuration File\nPress [Enter] to keep the Current setting") } else { fmt.Println("\nMaking new Configuration File") //Examplenames Coming soon n := "" file, err := homedir.Expand("~/.mywork.db") checkErr(err) config = Config{file, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n} } //fmt.Println("Whats Your Namecount\n") database := getNewInterInput("DB File Path: ", config.Database, nli) name := getNewInterInput("Whats your Name?: ", config.Name, nli) street := getNewInterInput("Street: ", config.Street, nli) zip := getNewInterInput("Zip Code: ", config.Zip, nli) city := getNewInterInput("City: ", config.City, nli) country := getNewInterInput("Country: ", config.Country, nli) telefon := getNewInterInput("Telefone number: ", config.Telefon, nli) mobile := getNewInterInput("Mobile number: ", config.Mobile, nli) mail := getNewInterInput("Email: ", config.Mail, nli) url := getNewInterInput("URL: ", config.Url, nli) taxid := getNewInterInput("Tax ID: ", config.Taxid, nli) bankname := getNewInterInput("Bank Name: ", config.Bankname, nli) bankacc := getNewInterInput("Bank Account: ", config.Bankacc, nli) banklz := getNewInterInput("BLZ: ", config.Banklz, nli) iban := getNewInterInput("IBAN: ", config.Iban, nli) bic := getNewInterInput("BIC: ", config.Bic, nli) conf := Config{database, name, street, zip, city, country, telefon, mobile, mail, url, taxid, bankacc, banklz, bankname, iban, bic} buf := new(bytes.Buffer) err = toml.NewEncoder(buf).Encode(conf) if err != nil { panic(err) } else { //fmt.Println("Conffile:\n",buf.String()) fmt.Println("\nWriting Configuration File") err = ioutil.WriteFile(conffile, buf.Bytes(), 0644) checkErr(err) } fmt.Println(" ") } func showConfig() { buf := new(bytes.Buffer) err := toml.NewEncoder(buf).Encode(config) if err != nil { panic(err) } else { fmt.Println("Configuration:\n", buf.String()) //fmt.Println(config) } }