-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.go
70 lines (57 loc) · 2.07 KB
/
save.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
package cmd
import (
"fmt"
"log"
"net"
"database/sql"
_ "github.com/mattn/go-sqlite3" // Driver for sql
"github.com/spf13/cobra"
)
// saveCmd represents the save command
var saveCmd = &cobra.Command{
Use: "save 'MAC Address'",
Short: "Saves a computer's MAC address, along with an alias and IP address for that MAC",
Long: `Saves a computer's MAC address, giving it a unique ID, along with an alias and IP address for that MAC address.
Wakie can use alias, ID or IP address to lookup saved MAC Address.`,
Run: func(cmd *cobra.Command, args []string) {
// First validate that MAC address is in the correct format
formattedMAC, err := net.ParseMAC(args[0])
if err != nil {
log.Fatalf("Error validating MAC address. Please double check entered MAC address. \n %s", err)
}
// Also validate that IP address is in the correct format
formattedIP := net.ParseIP(ipAddress)
if formattedIP == nil {
log.Fatal("Error validating IP address. Please double check entered IP address.")
}
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
log.Fatalf("Error opening database file. %s", err)
}
insertSQLStmt, err := db.Prepare("INSERT INTO 'main'.'computers'('MAC_Address', 'IP_Address', 'Alias') VALUES(?, ?, ?);")
if err != nil {
cobra.CheckErr(err)
}
insertEntry, err := insertSQLStmt.Exec(formattedMAC.String(), formattedIP.String(), alias)
if err != nil {
insertSQLStmt.Close()
if err.Error() == "UNIQUE constraint failed: computers.Alias" {
log.Fatalln("Error saving: Computer alias already exists in the database.")
} else if err.Error() == "UNIQUE constraint failed: computers.MAC_Address" {
log.Fatalln("Error saving: MAC Address already exists in the database.")
} else {
log.Fatalf("Error saving: %s", err)
}
}
dbSaveID, err := insertEntry.LastInsertId()
if err != nil {
insertSQLStmt.Close()
fmt.Println("Unable to get status of save to database operation")
}
fmt.Printf(" - MAC address saved to database with ID: %d\n", dbSaveID)
insertSQLStmt.Close()
},
}
func init() {
rootCmd.AddCommand(saveCmd)
}