summaryrefslogtreecommitdiff
path: root/importexport.go
blob: 5431506c4814c08151cbb6fad7659586620915f5 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main

import (
	"os"
	"os/exec"
	//"github.com/atotto/encoding/csv"
	"fmt"
	"time"
	"bufio"
	"strconv"
	"encoding/csv"
)

const (
	timeform string = "2006-01-02 15:04"
	taskfile string = "/tmp/laboravi.export.tasks"
	custfile string = "/tmp/laboravi.export.customers"
	projfile string = "/tmp/laboravi.export.projects"
	//taskfile string = "laboravi.export.tasks"
	//custfile string = "laboravi.export.customers"
	//projfile string = "laboravi.export.projects"
)

func ExportTasks(in []Task) {
	filename := taskfile
	file ,err := os.Create(filename)
	checkErr(err)
	defer file.Close()

	w := csv.NewWriter(file)
	defer w.Flush()
	for _,i := range in {
		//str := []string{Sint(i.Id),Sint(i.Projectid),i.Start.Format(time.RFC3339),i.Stop.Format(time.RFC3339),i.Taskname,i.Comment,Sint(i.Checkout)}
		str := []string{Sint(i.Id),Sint(i.Projectid),i.Start.Format(timeform),i.Stop.Format(timeform),i.Taskname,i.Comment,Sint(i.Checkout)}
		w.Write(str)
		//w.WriteStruct(i)
	}
}

func ExportProjects(in []Project) {
	filename := projfile
	file ,err := os.Create(filename)
	checkErr(err)
	defer file.Close()

	w := csv.NewWriter(file)
	defer w.Flush()
	for _,i := range in {
		//str := []string{Sint(i.Id),i.Name,i.Comment,i.First.Format(time.RFC3339),i.Last.Format(time.RFC3339),Sint(i.Finished),Sint(i.Customer)}
		str := []string{Sint(i.Id),i.Name,i.Comment,i.First.Format(timeform),i.Last.Format(timeform),Sint(i.Finished),Sint(i.Customer)}
		w.Write(str)
		//w.Write([]string{" Fuck","Master","Flash"})
		//w.WriteStruct(i)
	}
}

func ExportCustomers(in []Customer) {
	filename := custfile
	file ,err := os.Create(filename)
	checkErr(err)
	defer file.Close()

	w := csv.NewWriter(file)
	defer w.Flush()
	for _,i := range in {
		//str := []string{Sint(i.Id),i.Company,i.Name,i.Address,Sflo(i.Satz),i.Lastbill.Format(time.RFC3339)}
		str := []string{Sint(i.Id),i.Company,i.Name,i.Address,Sflo(i.Satz),i.Lastbill.Format(timeform)}
		w.Write(str)
		//w.WriteStruct(i)
	}
}

// Import an array of Tasks from a csv of given filename
func ImportTasks() ([]Task) {
	filename := taskfile
	//file ,err := os.Create(filename)
	var tsk []Task
	//w := NewReader(strings.NewReader())
	f, _ := os.Open(filename)

	// Create a new reader.
	r := csv.NewReader(bufio.NewReader(f))
	//err := r.ReadStructAll(&cust)
	//checkErr(err)
	lines, err := r.ReadAll()
	checkErr(err)

	for _, line := range lines {
		id, err := strconv.Atoi(line[0])
		checkErr(err)
		pr, err := strconv.Atoi(line[1])
		checkErr(err)
		sta,err := time.Parse(timeform,line[2])
		checkErr(err)
		sto,err := time.Parse(timeform,line[3])
		checkErr(err)
		ch, err := strconv.Atoi(line[6])
		checkErr(err)
		data := Task{
			Id: id,
			Projectid: pr,
			Start: sta,
			Stop: sto,
			Taskname: line[4],
			Comment: line[5],
			Checkout: ch,
		}
		tsk=append(tsk,data)

	}
	return tsk
}

// Import an array of Customers from a csv of given filename
func ImportCustomers() ([]Customer) {
	filename := custfile
	//file ,err := os.Create(filename)
	var cust []Customer
	//w := NewReader(strings.NewReader())
	f, _ := os.Open(filename)

	// Create a new reader.
	r := csv.NewReader(bufio.NewReader(f))
	//err := r.ReadStructAll(&cust)
	//checkErr(err)
	lines, err := r.ReadAll()
	checkErr(err)

	for _, line := range lines {
		id, err := strconv.Atoi(line[0])
		checkErr(err)
		sat,err := strconv.ParseFloat(line[4],64)
		checkErr(err)
		tim,err := time.Parse(timeform,line[5])
		checkErr(err)
		data := Customer{
			Id: id,
			Company: line[1],
			Name: line[2],
			Address: line[3],
			Satz: sat,
			Lastbill: tim,
		}
		cust=append(cust,data)
	}
	return cust
}

// Import an array of Projects from a csv of given filename
func ImportProjects() (prjs []Project) {
	filename := projfile
	//file ,err := os.Create(filename)
	//w := NewReader(strings.NewReader())
	f, _ := os.Open(filename)

	// Create a new reader.
	r := csv.NewReader(bufio.NewReader(f))
	//err := r.ReadStructAll(&cust)
	//checkErr(err)
	lines, err := r.ReadAll()
	checkErr(err)

	for _, line := range lines {
		id, err := strconv.Atoi(line[0])
		checkErr(err)
		fir,err := time.Parse(timeform,line[3])
		checkErr(err)
		las,err := time.Parse(timeform,line[4])
		checkErr(err)
		fin,err := strconv.Atoi(line[5])
		checkErr(err)
		cu,err := strconv.Atoi(line[6])
		checkErr(err)
		data := Project{
			Id: id,
			Name: line[1],
			Comment: line[2],
			First: fir,
			Last: las,
			Finished: fin,
			Customer: cu,
		}
		prjs=append(prjs,data)
	}
	return
}

func TarExports(filename string) {
	var (
		cmdOut []byte
		err error
	)
	cmd := "tar"
	fln := fmt.Sprintf("/tmp/%s",filename)
	//args := []string{"-cf",filename,"/tmp/laboravi.export.tasks","/tmp/laboravi.export.customers","/tmp/laboravi.export.projects"}
	args := []string{"-cf",fln,taskfile,projfile,custfile}
	if cmdOut, err = exec.Command(cmd, args...).Output(); err != nil {
		//panic(err)
	}
	 _ = cmdOut
	cmd = "mv"
	args = []string{fln,"."}
	if cmdOut, err = exec.Command(cmd, args...).Output(); err != nil {
		//panic(err)
	}
	 _ = cmdOut
}

func UnTarExports(filename string) {
	var (
		cmdOut []byte
		err error
	)
	cmd := "tar"
	//args := []string{"-xf",filename,"/tmp/laboravi.export.tasks","/tmp/laboravi.export.customers","/tmp/laboravi.export.projects"}
	//args := []string{"-xf",filename,taskfile,projfile,custfile}
	args := []string{"-xf",filename,"-C","/"}
	if cmdOut, err = exec.Command(cmd, args...).Output(); err != nil {
		//panic(err)
	}
	 _ = cmdOut
}

func PurgeTemps() {
	var (
		cmdOut []byte
		err error
	)
	cmd := "rm"
	//args := []string{"/tmp/laboravi.export.tasks","/tmp/laboravi.export.customers","/tmp/laboravi.export.projects"}
	args := []string{taskfile,projfile,custfile}
	//args := []string{"/tmp/laboravi.export.*"}
	if cmdOut, err = exec.Command(cmd, args...).Output(); err != nil {
		//panic(err)
	}
	 _ = cmdOut
}