From 38cd567117fb2248a175f23cb8a736a988e3691c Mon Sep 17 00:00:00 2001 From: Nikolaus Gotsche Date: Fri, 2 Nov 2018 13:03:42 +0100 Subject: Full working import/export with TARing of the files --- importexport.go | 106 +++++++++++++++++++++++++++++++++++++++++++++++--------- interact.go | 29 +++++++++------- sqlite.go | 35 +++++++++++-------- utils.go | 58 +++++++++++++++++++++++++++++++ 4 files changed, 184 insertions(+), 44 deletions(-) diff --git a/importexport.go b/importexport.go index e1af448..5431506 100644 --- a/importexport.go +++ b/importexport.go @@ -2,16 +2,27 @@ package main import ( "os" + "os/exec" //"github.com/atotto/encoding/csv" - //"io" - _ "fmt" + "fmt" "time" "bufio" "strconv" "encoding/csv" ) -func ExportTasks(in []Task,filename string) { +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() @@ -19,13 +30,15 @@ func ExportTasks(in []Task,filename string) { 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(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 string) { +func ExportProjects(in []Project) { + filename := projfile file ,err := os.Create(filename) checkErr(err) defer file.Close() @@ -33,14 +46,16 @@ func ExportProjects(in []Project,filename string) { 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(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 string) { +func ExportCustomers(in []Customer) { + filename := custfile file ,err := os.Create(filename) checkErr(err) defer file.Close() @@ -48,14 +63,17 @@ func ExportCustomers(in []Customer,filename string) { 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(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(filename string) ([]Task) { +func ImportTasks() ([]Task) { + filename := taskfile + //file ,err := os.Create(filename) var tsk []Task //w := NewReader(strings.NewReader()) f, _ := os.Open(filename) @@ -72,9 +90,9 @@ func ImportTasks(filename string) ([]Task) { checkErr(err) pr, err := strconv.Atoi(line[1]) checkErr(err) - sta,err := time.Parse(time.RFC3339,line[2]) + sta,err := time.Parse(timeform,line[2]) checkErr(err) - sto,err := time.Parse(time.RFC3339,line[3]) + sto,err := time.Parse(timeform,line[3]) checkErr(err) ch, err := strconv.Atoi(line[6]) checkErr(err) @@ -94,7 +112,9 @@ func ImportTasks(filename string) ([]Task) { } // Import an array of Customers from a csv of given filename -func ImportCustomers(filename string) ([]Customer) { +func ImportCustomers() ([]Customer) { + filename := custfile + //file ,err := os.Create(filename) var cust []Customer //w := NewReader(strings.NewReader()) f, _ := os.Open(filename) @@ -111,7 +131,7 @@ func ImportCustomers(filename string) ([]Customer) { checkErr(err) sat,err := strconv.ParseFloat(line[4],64) checkErr(err) - tim,err := time.Parse(time.RFC3339,line[5]) + tim,err := time.Parse(timeform,line[5]) checkErr(err) data := Customer{ Id: id, @@ -127,7 +147,9 @@ func ImportCustomers(filename string) ([]Customer) { } // Import an array of Projects from a csv of given filename -func ImportProjects(filename string) (prjs []Project) { +func ImportProjects() (prjs []Project) { + filename := projfile + //file ,err := os.Create(filename) //w := NewReader(strings.NewReader()) f, _ := os.Open(filename) @@ -141,9 +163,9 @@ func ImportProjects(filename string) (prjs []Project) { for _, line := range lines { id, err := strconv.Atoi(line[0]) checkErr(err) - fir,err := time.Parse(time.RFC3339,line[3]) + fir,err := time.Parse(timeform,line[3]) checkErr(err) - las,err := time.Parse(time.RFC3339,line[4]) + las,err := time.Parse(timeform,line[4]) checkErr(err) fin,err := strconv.Atoi(line[5]) checkErr(err) @@ -162,3 +184,55 @@ func ImportProjects(filename string) (prjs []Project) { } 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 +} + diff --git a/interact.go b/interact.go index c9b96e6..28c61a0 100644 --- a/interact.go +++ b/interact.go @@ -515,23 +515,26 @@ func interact(fulldb bool) { c.Println("Customers:",len(cus)) //c.Println("Customers:",cus) if isInterSure("Export ?"){ - ExportCustomers(cus,"export.customers") - ExportProjects(prs,"export.projects") - ExportTasks(tas,"export.tasks") + ExportCustomers(cus) + ExportProjects(prs) + ExportTasks(tas) + TarExports("exports.tar") + PurgeTemps() } - icus := ImportCustomers("export.customers") - c.Println("Imported Customers:",len(icus)) - //c.Println("Customers:",icus) - - itas := ImportTasks("export.tasks") - c.Println("Imported Tasks:",len(itas)) - //c.Println("Tasks:",itas) + if isInterSure("Import ?"){ + UnTarExports("exports.tar") + icus := ImportCustomers() + itas := ImportTasks() + iprs := ImportProjects() + PurgeTemps() + c.Println("Imported Customers:",len(icus)) + c.Println("Imported Projects:",len(iprs)) + c.Println("Imported Tasks:",len(itas)) + SaveImportsToDB(icus,iprs,itas) + } - iprs := ImportProjects("export.projects") - c.Println("Imported Projects:",len(iprs)) //c.Println("Projects:",iprs) - SaveImportsToDB(icus,iprs,itas) }, }) diff --git a/sqlite.go b/sqlite.go index cb772a0..e749a6b 100644 --- a/sqlite.go +++ b/sqlite.go @@ -472,7 +472,7 @@ func SaveTask(in Task) { checkErr(err) } -// Loop through Array of []Projects and call SaveProject +// Loop through Array of []Customers, []Projects and []Tasks, update new db ids and call their respected SaveFunctions func SaveImportsToDB(cus []Customer, prs []Project, tsk []Task) { boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() var cusids, prids []IdMap @@ -482,20 +482,25 @@ func SaveImportsToDB(cus []Customer, prs []Project, tsk []Task) { fmt.Println(nli," Projects:",len(prs)) fmt.Println(nli," Tasks:",len(tsk)) fmt.Println(sub("")) - for _,c := range cus { - id := SaveCustomer(c) - cusids=append(cusids,IdMap{c.Id,id}) - } - fmt.Println(cusids) - prs = ChangeCustomerIds(cusids,prs) - for _,p := range prs { - id := SaveProject(p) - prids=append(prids,IdMap{p.Id,id}) - } - fmt.Println(prids) - tsk = ChangeProjectIds(prids,tsk) - for _,t := range tsk { - SaveTask(t) + if isInterSure(nli+" Are You sure to Import them all?") { + for _,c := range cus { + id := SaveCustomer(c) + cusids=append(cusids,IdMap{c.Id,id}) + } + fmt.Println(cusids) + prs = ChangeCustomerIds(cusids,prs) + for _,p := range prs { + id := SaveProject(p) + prids=append(prids,IdMap{p.Id,id}) + } + fmt.Println(prids) + tsk = ChangeProjectIds(prids,tsk) + for _,t := range tsk { + SaveTask(t) + } + fmt.Println(frame(posR(),false)) + }else{ + fmt.Println(frame(negR(),false)) } } diff --git a/utils.go b/utils.go index c9b0aad..c9503ff 100644 --- a/utils.go +++ b/utils.go @@ -5,6 +5,7 @@ import ( "fmt" "math" "os" + "io" "os/exec" "runtime" "strconv" @@ -12,6 +13,8 @@ import ( "unicode/utf8" "math/rand" "time" + "path/filepath" + "archive/tar" ) const ( @@ -304,3 +307,58 @@ func IsStrElement(some string, group []string) bool { return false } +func Tar(src string, writers ...io.Writer) error { + // ensure the src actually exists before trying to tar it + if _, err := os.Stat(src); err != nil { + return fmt.Errorf("Unable to tar files - %v", err.Error()) + } + mw := io.MultiWriter(writers...) + + //gzw := gzip.NewWriter(mw) + //defer gzw.Close() + + tw := tar.NewWriter(mw) + defer tw.Close() + + // walk path + return filepath.Walk(src, func(file string, fi os.FileInfo, err error) error { + // return on any error + if err != nil { + return err + } + // create a new dir/file header + header, err := tar.FileInfoHeader(fi, fi.Name()) + if err != nil { + return err + } + // update the name to correctly reflect the desired destination when untaring + header.Name = strings.TrimPrefix(strings.Replace(file, src, "", -1), string(filepath.Separator)) + + // write the header + if err := tw.WriteHeader(header); err != nil { + return err + } + + // return on non-regular files (thanks to [kumo](https://medium.com/@komuw/just-like-you-did-fbdd7df829d3) for this suggested update) + if !fi.Mode().IsRegular() { + return nil + } + + // open files for taring + f, err := os.Open(file) + if err != nil { + return err + } + // copy file data into tar writer + if _, err := io.Copy(tw, f); err != nil { + return err + } + + // manually close here after each file operation; defering would cause each file close + // to wait until all operations have completed. + f.Close() + + return nil + }) +} + -- cgit v1.2.3