forked from pdf/golifx
/
golifx-import-products.go
112 lines (94 loc) · 2.81 KB
/
golifx-import-products.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strings"
"github.com/pdf/golifx/protocol/v2/device"
)
type vendorSorter []*device.Vendor
func (s vendorSorter) Len() int { return len(s) }
func (s vendorSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s vendorSorter) Less(i, j int) bool { return s[i].ID < s[j].ID }
type productSorter []*device.Product
func (s productSorter) Len() int { return len(s) }
func (s productSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s productSorter) Less(i, j int) bool { return s[i].ID < s[j].ID }
func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\t%s [flags] <path/to/products.json>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
func main() {
var (
output = flag.String(`output`, ``, `Output path; must be set`)
err error
)
log.SetFlags(0)
log.SetPrefix(`golifx-import-products: `)
flag.Usage = usage
flag.Parse()
if len(flag.Args()) != 1 || len(*output) == 0 {
flag.Usage()
os.Exit(2)
}
in, err := ioutil.ReadFile(flag.Args()[0])
if err != nil {
log.Fatalln(err)
}
var vendors vendorSorter
if err = json.Unmarshal(in, &vendors); err != nil {
log.Fatalln(err)
}
sort.Sort(vendors)
g := &Generator{}
g.Printf("// Code generated by \"golifx-import-products %s\"; DO NOT EDIT\n\n", strings.Join(os.Args[1:], " "))
g.Printf("package device\n\n")
g.Printf("var (\n")
g.Printf("\t// Vendors automatically generated from products.json\n")
g.Printf("\tVendors = map[uint32]Vendor{\n")
for _, vendor := range vendors {
g.Printf("\t\t%d: Vendor{\n", vendor.ID)
g.Printf("\t\t\tID: %d,\n", vendor.ID)
g.Printf("\t\t\tName: `%s`,\n", vendor.Name)
g.Printf("\t\t\tProducts: map[uint32]*Product{\n")
products := make(productSorter, len(vendor.Products))
i := 0
for _, product := range vendor.Products {
products[i] = product
i++
}
sort.Sort(products)
for _, product := range products {
g.Printf("\t\t\t\t%d: &Product{ID: %d, VendorID: %d, Name: `%s`, Features: %d},\n", product.ID, product.ID, vendor.ID, product.Name, product.Features)
}
g.Printf("\t\t\t},\n")
g.Printf("\t\t},\n")
}
g.Printf("\t}\n")
g.Printf(")\n")
g.Write(filepath.Join(*output, `vendors_import.go`))
}
// Generator provides the output buffer and helper functions
type Generator struct {
buf bytes.Buffer
}
// Printf prints formatted strings to the buffer
func (g *Generator) Printf(format string, a ...interface{}) {
if _, err := fmt.Fprintf(&g.buf, format, a...); err != nil {
log.Fatalln(err)
}
}
// Write outputs the buffer to the specified file path
func (g *Generator) Write(output string) {
if err := ioutil.WriteFile(output, g.buf.Bytes(), 0644); err != nil {
log.Fatalln(err)
}
}