This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgenerate_payload_code.go
86 lines (76 loc) · 1.67 KB
/
generate_payload_code.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
// +build ignore
package main
import (
"bytes"
"flag"
"html/template"
"io/ioutil"
"log"
"reflect"
"golang.org/x/tools/imports"
"github.com/micromdm/mdm"
)
const stub = `
// generated by go:generate, DO NOT EDIT
package mdm
func NewPayload(request *CommandRequest) (*Payload, error) {
requestType := request.RequestType
payload := newPayload(requestType)
switch requestType {
{{range .ManyFields}}
case "{{.Name}}":
payload.Command.{{.Name}} = request.{{.Name}}
{{ end }}
case "ProfileList",
"ProvisioningProfileList",
"CertificateList",
"SecurityInfo",
"StopMirroring",
"ClearRestrictionsPassword",
"UserList",
"LogOutUser",
"DisableLostMode",
"DeviceLocation",
"ManagedMediaList",
"OSUpdateStatus",
"DeviceConfigured",
"AvailableOSUpdates",
"Restrictions",
"ShutDownDevice",
"RestartDevice":
return payload, nil
default:
return nil, fmt.Errorf("Unsupported MDM RequestType %v", requestType)
}
return payload, nil
}
`
func main() {
out := flag.String("out", "new_payload.go", "path to output file")
flag.Parse()
type param struct {
Name string
}
type params struct {
ManyFields []param
Simple []param
}
var p params
val := reflect.ValueOf(mdm.Command{})
for i := 1; i < val.NumField(); i++ {
name := val.Field(i).Type().Name()
if val.Field(i).NumField() == 0 {
p.Simple = append(p.Simple, param{Name: name})
} else {
p.ManyFields = append(p.ManyFields, param{Name: name})
}
}
var tmpl = template.Must(template.New("test").Parse(stub))
var buf bytes.Buffer
tmpl.Execute(&buf, p)
pretty, err := imports.Process("", buf.Bytes(), nil)
if err != nil {
log.Fatal(err)
}
ioutil.WriteFile(*out, pretty, 0644)
}