-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
140 lines (118 loc) · 4.3 KB
/
update.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
package cmd
import (
"database/sql"
"fmt"
"log"
"net"
"os"
"github.com/jedib0t/go-pretty/v6/table"
_ "github.com/mattn/go-sqlite3" // Driver for sql
"github.com/spf13/cobra"
)
var (
idFromDB int64
macAddressFromDB string
ipAddressFromDB string
aliasFromDB string
dbQueryString string
newAliasValue string
newMacAddrValue string
newIPAddrValue string
)
// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update an existing record in the database",
Long: `Update an existing record in the database.
Can search for record to update by ID, MAC Address, or Alias but not by IP Address since Wakie allows for duplicate IP's.
Can update Alias, IP Address or MAC Address, or all at the same time.`,
Run: func(cmd *cobra.Command, args []string) {
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
log.Fatalf("Error opening database file. %s", err)
}
// Create sql query string based on user inputed flag to get record.
// All update requests will specify record by ID.
switch {
case idNum != "":
dbQueryString = fmt.Sprintf("SELECT * FROM computers WHERE `ID` = '%s';", idNum)
case alias != "":
dbQueryString = fmt.Sprintf("SELECT * FROM computers WHERE `Alias` = '%s';", alias)
case macAddress != "":
dbQueryString = fmt.Sprintf("SELECT * FROM computers WHERE `MAC_Address` = '%s';", macAddress)
default:
fmt.Println("Please specify an ID(-i), MAC Address(-m), or Alias(-a). To see list of all records in database, run 'wakie list'")
os.Exit(1)
}
// Create table that will be printed out, showing old and updated record
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"ID", "MAC Address", "IP Address", "Alias"})
// Query db for old record
listDB := db.QueryRow(dbQueryString)
if err != nil {
log.Fatalf("Unable to query database: %s", err)
}
err = listDB.Scan(&idFromDB, &macAddressFromDB, &ipAddressFromDB, &aliasFromDB)
if err != nil {
log.Fatal(err)
}
t.AppendRow(table.Row{"Old record:"})
t.AppendRow([]interface{}{idFromDB, macAddressFromDB, ipAddressFromDB, aliasFromDB})
t.AppendSeparator()
// Update Alias if updateAlias flag is set
if newAliasValue != "" {
updateStmt, err := db.Prepare("UPDATE computers SET Alias=? where ID=?")
cobra.CheckErr(err)
_, err = updateStmt.Exec(newAliasValue, idFromDB)
cobra.CheckErr(err)
updateStmt.Close()
}
// Update MAC address if updateMac flag is set
if newMacAddrValue != "" {
// First confirm valid mac address
newMacAddrValue, err := net.ParseMAC(newMacAddrValue)
if err != nil {
log.Fatalf("Updated MAC Address is not valid. Please double check the entered address. %s", err)
}
updateStmt, err := db.Prepare("UPDATE computers SET MAC_Address=? where ID=?")
cobra.CheckErr(err)
_, err = updateStmt.Exec(newMacAddrValue.String(), idFromDB)
cobra.CheckErr(err)
updateStmt.Close()
}
// Update IP address if updateIP flag is set
if newIPAddrValue != "" {
// First confirm valid mac address
newIPAddrValue := net.ParseIP(newIPAddrValue)
if newIPAddrValue == nil {
log.Fatalf("Updated IP Address is not valid. Please double check the entered address. %s", err)
}
updateStmt, err := db.Prepare("UPDATE computers SET IP_Address=? where ID=?")
cobra.CheckErr(err)
_, err = updateStmt.Exec(newIPAddrValue.String(), idFromDB)
cobra.CheckErr(err)
updateStmt.Close()
}
// Query DB again for updated record and print out table
listDB = db.QueryRow(fmt.Sprintf("SELECT * FROM computers WHERE `ID` = '%d';", idFromDB))
if err != nil {
log.Fatalf("Unable to query database: %s", err)
}
err = listDB.Scan(&idFromDB, &macAddressFromDB, &ipAddressFromDB, &aliasFromDB)
if err != nil {
log.Fatal(err)
}
fmt.Println(" - Record has been updated:")
t.AppendRow(table.Row{"Updated record:"})
t.AppendRow([]interface{}{idFromDB, macAddressFromDB, ipAddressFromDB, aliasFromDB})
t.AppendSeparator()
t.Render()
},
}
func init() {
rootCmd.AddCommand(updateCmd)
updateCmd.Flags().StringVar(&newAliasValue, "updateAlias", "", "Specify updated Alias")
updateCmd.Flags().StringVar(&newMacAddrValue, "updateMac", "", "Specify updated MAC Address")
updateCmd.Flags().StringVar(&newIPAddrValue, "updateIP", "", "Specify updated IP Address")
}