/
data.go
143 lines (115 loc) · 3.05 KB
/
data.go
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
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/jonaswouters/goclerk/models"
"github.com/jonaswouters/goclerk/modules/setting"
"github.com/jonaswouters/goclerk/modules/store"
cliui "github.com/mitchellh/cli"
"github.com/siddontang/go/bson"
"github.com/urfave/cli"
)
// Data command to import and export data from the database
var Data = cli.Command{
Name: "data",
Usage: "Goclerk data tools",
Subcommands: []cli.Command{
{
Name: "export",
Usage: "Export goclerk data",
Action: exportJson,
},
{
Name: "import",
Usage: "Import json data into goclerk",
Subcommands: []cli.Command{
{
Name: "invoice",
Usage: "Import an invoice",
Action: importInvoice,
},
},
},
},
}
// exportJson will export all the data to json files
func exportJson(ctx *cli.Context) {
setting.LoadSettings()
ui := &cliui.BasicUi{Writer: os.Stdout, Reader: os.Stdin}
path, _ := ui.Ask("Export folder (" + setting.Settings.ExportFolder + "):")
if path != "" {
setting.Settings.ExportFolder = path
}
// Check if the directory exists
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0755)
}
fmt.Printf("Exporting to %s", setting.Settings.ExportFolder)
exportInvoices(setting.Settings.ExportFolder)
exportUsers(setting.Settings.ExportFolder)
// Save updated settings
setting.SaveSettings()
}
// reset will drop the database schema and run all migrations again
func importJson(ctx *cli.Context) {
setting.LoadSettings()
}
// check checks for errors and panics if there is one
func check(e error) {
if e != nil {
panic(e)
}
}
// exportUsers exports all the users in the database
func exportUsers(path string) {
var users []models.User
err := store.GetDB().All(&users)
if err != nil {
fmt.Printf("Failed to retrieve users from the database %s", err.Error())
return
}
for _, user := range users {
b, _ := json.Marshal(user)
err := ioutil.WriteFile(path+"users-"+user.ID.Hex()+".json", b, 0644)
check(err)
}
}
// exportInvoices exports all the invoices in the database
func exportInvoices(path string) {
var invoices []models.Invoice
err := store.GetDB().All(&invoices)
if err != nil {
fmt.Printf("Failed to retrieve invoices from the database %s", err.Error())
return
}
for _, invoice := range invoices {
b, _ := json.Marshal(invoice)
// Convert bytes to string.
err := ioutil.WriteFile(path+"invoice-"+invoice.ID.Hex()+".json", b, 0644)
check(err)
}
}
// importInvoice lets you import an invoice in a json file
func importInvoice(ctx *cli.Context) {
setting.LoadSettings()
// Get the passed path
path := ctx.Args().First()
var invoice models.Invoice
// Read the file from the path provided
fmt.Printf("Importing file %s\n", path)
b, err := ioutil.ReadFile(path)
check(err)
// Convert bytes to string.
err = json.Unmarshal(b, &invoice)
check(err)
// Add unique ID
invoice.ID = bson.NewObjectId()
fmt.Printf("Invoice number: %s\n", invoice.Number)
err = store.GetDB().Save(&invoice)
if err != nil {
fmt.Print("Failed to import invoice")
check(err)
}
}