summaryrefslogtreecommitdiff
path: root/config.go
blob: 734983dbdcb229943096eb3730dd4ff447c39388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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"`
	Folder	 string //`toml:"folder"`
	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(frame("Editing Configuration File",true))
		fmt.Println(nli,"Press [Enter] to keep the Current setting")
	} else {
		fmt.Println(frame("Making new Configuration File",true))
		//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, n}
	}
	//fmt.Println("Whats Your Namecount\n")
	database := getNewInterInput("DB File Path: ", config.Database, nli)
	folder := getNewInterInput("Template Folder: ", config.Folder, 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, folder, 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(frame("Writing Configuration File",false))
		err = ioutil.WriteFile(conffile, buf.Bytes(), 0644)
		checkErr(err)
	}
	fmt.Println(" ")

}

func showConfig() {
	buf := new(bytes.Buffer)
	err := toml.NewEncoder(buf).Encode(config)
	bufarr := String2StringArray(buf.String(),"\n")
	if err != nil {
		panic(err)
	} else {
		fmt.Println(frame("Configuration:",true))
		fmt.Println(StrLines(bufarr,nli))
		fmt.Println(frame("",false))
	}
}