-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_json.go
More file actions
57 lines (49 loc) · 1.32 KB
/
load_json.go
File metadata and controls
57 lines (49 loc) · 1.32 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
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/go-playground/validator/v10"
"os"
)
type QLab struct {
IP string `json:"ip" validate:"required,ip4_addr"`
OSCPort uint `json:"osc_port" validate:"required,port"`
}
type Resolume struct {
IP string `json:"ip" validate:"required,ip4_addr"`
WebsocketPort uint `json:"websocket_port" validate:"required,port"`
}
type Config struct {
OSCListenPort uint `json:"osc_listen_port" validate:"required,port"`
Resolume Resolume `json:"resolume" validate:"required"`
QLab *QLab `json:"qlab,omitempty" validate:"omitempty"`
}
func LoadJSON[T any](filename string) (T, error) {
var data T
fileData, err := os.ReadFile(filename)
if err != nil {
return data, err
}
err = json.Unmarshal(fileData, &data)
return data, err
}
func LoadConfig() Config {
validate := validator.New()
data, err := LoadJSON[Config]("config.json")
if err != nil {
fmt.Print("Invalid config.json\n")
fmt.Print("Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
os.Exit(1)
}
err = validate.Struct(data)
if err != nil {
fmt.Println(err.(validator.ValidationErrors))
fmt.Print("Invalid config.json\n")
fmt.Print("Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
os.Exit(1)
}
return data
}