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
|
/*
datamaps-go is a simple tool to extract from and send data to spreadsheets.
*/
package main
import (
"log"
"os"
"path/filepath"
"github.com/yulqen/datamaps-go/pkg/datamaps"
)
const (
config_dir_name = "datamaps-go"
db_name = "datamaps.db"
)
func setUp() (string, error) {
dir, err := os.UserConfigDir()
if err != nil {
return "", err
}
// check if config folder exists
config_path := filepath.Join(dir, config_dir_name)
db_path := filepath.Join(config_path, db_name)
if _, err := os.Stat(config_path); os.IsNotExist(err) {
log.Println("Config directory does not exist.")
log.Printf("Creating config directory %s\n", config_path)
if err := os.Mkdir(filepath.Join(dir, "datamaps-go"), 0700); err != nil {
return "", err
}
} else {
log.Println("Config directory found.")
}
if _, err := os.Stat(db_path); os.IsNotExist(err) {
log.Println("Database does not exist.")
_, err = os.Create(db_path)
if err != nil {
return "", err
}
log.Printf("Creating database file at %s\n", db_path)
_, err := datamaps.SetupDB(db_path)
if err != nil {
return "", err
}
} else {
log.Println("Database file found.")
}
return dir, nil
}
func main() {
opts := datamaps.ParseOptions()
switch opts.Command {
case "datamap":
data, err := datamaps.ReadDML(opts.DMPath)
if err != nil {
log.Fatal(err)
}
opts.DMData = data
err = datamaps.DatamapToDB(opts)
if err != nil {
log.Fatal(err)
}
case "setup":
_, err := setUp()
if err != nil {
log.Fatal(err)
}
}
// switch os.Args[1] {
// case "server":
// opts.Command.Parse(os.Args[2:])
// http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// if r.URL.Path != "/" {
// http.NotFound(w, r)
// return
// }
// fmt.Fprintf(w, "Welcome to datamaps!")
// // or you could write it thus
// // w.Write([]byte("Hello from Snippetbox"))
// })
// log.Println("Starting server on :8080")
// err := http.ListenAndServe(":8080", nil)
// log.Fatal(err)
// case "datamap":
// datamapCmd.Parse(os.Args[2:])
// fmt.Println("subcommand 'datamap'")
// fmt.Println(" import:", *dmPath)
// fmt.Println(" name:", *dmName)
// fmt.Println(" overwrite:", *dmOverwrite)
// fmt.Println(" initial:", *dmInitial)
// dir, err := os.UserConfigDir()
// if err != nil {
// os.Exit(1)
// }
// // check if config folder exists
// config_path := filepath.Join(dir, config_dir_name)
// if _, err := os.Stat(config_path); os.IsNotExist(err) {
// fmt.Println("Config directory and database does not exist. Run datamaps setup to fix.")
// os.Exit(1)
// }
// // Here we actually read the data from the file
// data, err := datamaps.ReadDML(opts.ImportPath)
// if err != nil {
// log.Fatal(err)
// }
// opts.DMData = data
// opts.DBPath = filepath.Join(config_path, db_name)
// err = datamaps.DatamapToDB(opts)
// if err != nil {
// log.Fatal(err)
// }
// case "setup":
// setupCmd.Parse(os.Args[2:])
// _, err := setUp()
// if err != nil {
// log.Fatal(err)
// }
// default:
// fmt.Println("Do not recognised that command. Expected 'datamap' subcommand.")
// os.Exit(1)
// }
}
|