package main import ( "bytes" "fmt" //"os" "io/ioutil" //"strconv" "github.com/fatih/color" "github.com/BurntSushi/toml" "github.com/atrox/homedir" ) 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 isSure("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("Making 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 := getNewInput("DB File Path: ",config.Database) name := getNewInput("Whats your Name?: ",config.Name) street := getNewInput("Street: ",config.Street) zip := getNewInput("Zip Code: ",config.Zip) city := getNewInput("City: ",config.City) country := getNewInput("Country: ",config.City) telefon := getNewInput("Telefone number: ",config.Telefon) mobile := getNewInput("Mobile number: ",config.Mobile) mail := getNewInput("Email: ",config.Mail) url := getNewInput("URL: ",config.Url) taxid := getNewInput("Tax ID: ",config.Taxid) bankname := getNewInput("Bank Name: ",config.Bankname) bankacc := getNewInput("Bank Account: ",config.Bankacc) banklz := getNewInput("BLZ: ",config.Banklz) iban := getNewInput("IBAN: ",config.Iban) bic := getNewInput("BIC: ",config.Bic) 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("Writing Configuration File") err = ioutil.WriteFile(conffile, buf.Bytes(), 0644) checkErr(err) } } 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) } }