-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata.go
More file actions
145 lines (128 loc) · 2.89 KB
/
data.go
File metadata and controls
145 lines (128 loc) · 2.89 KB
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
144
145
package main
import (
"fmt"
"strconv"
"github.com/jritsema/go-htmx-starter/lib"
)
var (
devices []Device
AddDevicePageInfo AddDevicePage
PossibleDeviceStates []DeviceState
)
type Device struct {
ID string
Hostname string
IPAddress string
State lib.StateEnum
UserName string
Notes string
DeviceStateOptions []DeviceState
}
type AddDevicePage struct {
Devices []Device
PossibleDeviceStates []DeviceState
}
// DeviceState is for tracking a device's current state
// Value is the string value of State
// DisplayValue is the string that should be displayed to the end user
type DeviceState struct {
DisplayValue string
State lib.StateEnum
}
func init() {
fmt.Println("Init Data")
devices = []Device{
{
ID: "1",
Hostname: "lab-label1",
IPAddress: "10.80.80.10",
State: lib.StateEnumAvailable,
UserName: "",
Notes: "Custom config for hardware bug",
},
{
ID: "2",
Hostname: "lab-label2",
IPAddress: "10.80.80.11",
State: lib.StateEnumInUse,
UserName: "elimorga",
Notes: "",
},
{
ID: "3",
Hostname: "lab-label3",
IPAddress: "10.80.80.12",
State: lib.StateEnumOffline,
UserName: "jenkins",
Notes: "under maintenance",
},
}
MakePossibleDeviceStates()
AddDevicePageInfo.Devices = devices
AddDevicePageInfo.PossibleDeviceStates = PossibleDeviceStates
}
func MakePossibleDeviceStates() {
// I need a range of values with state key, and display value
for _, state := range lib.StateEnumValues() {
displayValue := lib.StateDisplay[state]
PossibleDeviceStates = append(PossibleDeviceStates, DeviceState{DisplayValue: displayValue, State: state})
}
}
func getDeviceByID(id string) Device {
var result Device
for _, i := range devices {
if i.ID == id {
result = i
break
}
}
result.DeviceStateOptions = GetDeviceStateOptions()
return result
}
func updateDevice(device Device) {
result := []Device{}
for _, i := range devices {
if i.ID == device.ID {
i.Hostname = device.Hostname
i.IPAddress = device.IPAddress
i.UserName = device.UserName
i.State = device.State
i.Notes = device.Notes
}
result = append(result, i)
}
devices = result
}
func addDevice(device Device) {
max := 0
for _, i := range devices {
n, _ := strconv.Atoi(i.ID)
if n > max {
max = n
}
}
max++
id := strconv.Itoa(max)
devices = append(devices, Device{
ID: id,
Hostname: device.Hostname,
UserName: device.UserName,
IPAddress: device.IPAddress,
Notes: device.Notes,
State: device.State,
})
}
func deleteDevice(id string) {
result := []Device{}
for _, i := range devices {
if i.ID != id {
result = append(result, i)
}
}
devices = result
}
func GetDeviceStateOptions() []DeviceState {
s := make([]DeviceState, len(PossibleDeviceStates))
copy(s, PossibleDeviceStates)
return s
}