-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
113 lines (94 loc) · 2.97 KB
/
setup.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
package cmd
import (
"database/sql"
"errors"
"fmt"
"log"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
_ "github.com/mattn/go-sqlite3" // Driver for sql
)
var saveDbPath string
// setupCmd represents the setup command
var setupCmd = &cobra.Command{
Use: "setup",
Short: "Creates SQLite Database and config file",
Long: `Used to setup Wakie by creating a SQLite Database file at the specified path
and creates a config file.
If config flag is not set, will default to saving the config file in ~/.config/wakie. If that
fails will attempt to save config file in same folder as the app.`,
Run: func(cmd *cobra.Command, args []string) {
// Check if database file already exists at specified path and creates file if not
if saveDbPath == "$HOME/.config/wakie" {
saveDbPath = homeDir + "/.config/wakie"
}
fullDbPath := saveDbPath + "/wakie.db"
file := fileExists(fullDbPath)
if file {
log.Fatal("Error - Database already exists at the supplied path")
}
fileCreateResult, err := createFile(fullDbPath)
if err != nil {
log.Fatal(err)
}
fmt.Printf(" - Database %s\n", fileCreateResult)
err = createDbTable(fullDbPath)
if err != nil {
log.Fatal(err)
}
fmt.Println(" - Table created in database")
// Writes out config file with path to database file once database has been created
if cfgFile == "" {
cfgFile = homeDir + "/.config/wakie/wakie.yaml"
}
viper.Set("db.dbPath", fullDbPath)
err = viper.WriteConfigAs(cfgFile)
if err != nil {
log.Fatalf("Error writing config file: %s", err)
}
fmt.Printf(" - config file saved at %s\n", cfgFile)
fmt.Println(" - Wakie is now setup and ready for use :-)")
},
}
func init() {
rootCmd.AddCommand(setupCmd)
setupCmd.Flags().StringVar(&saveDbPath, "saveDb", "$HOME/.config/wakie", "Path to folder where to save database file")
}
func fileExists(path string) bool {
_, err := os.Stat(path)
if err != nil {
return false
} else {
return true
}
}
func createFile(filePath string) (string, error) {
_, err := os.Create(filePath)
if err != nil {
errorMsg := "Failed to create file: " + err.Error()
return "", errors.New(errorMsg)
}
return "File created at: " + filePath, nil
}
// Note that IP address column isn't required to be unique. This is because user might have computers on different networks
// with the same IP address. Since this is the case, Wakie supports listing entries by IP but does not support using IP to select entry
// to send magic packet to.
func createDbTable(dbPath string) error {
createTableStmt := `CREATE TABLE 'computers'
('ID' INTEGER PRIMARY KEY,
'MAC_Address' STRING NULL UNIQUE,
'IP_Address' STRING NULL,
'Alias' STRING NULL UNIQUE);`
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
errorMsg := "Unable to open db file: " + err.Error()
return errors.New(errorMsg)
}
_, err = db.Exec(createTableStmt)
if err != nil {
errorMsg := "Unable to create table in db file: " + err.Error()
return errors.New(errorMsg)
}
return nil
}