File: command

package info (click to toggle)
golang-github-digitalocean-go-qemu 0.0~git20200529.1b453d0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,264 kB
  • sloc: sh: 34; makefile: 7
file content (92 lines) | stat: -rw-r--r-- 2,315 bytes parent folder | download | duplicates (3)
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
// {{ .Name }} -> {{ .Name.Go }} (command)

{{ define "funcArg" -}}
  {{- if .List -}}
    {{ unexport .Name.FieldName }} []{{ .Type.Go }}
  {{- else if and .Optional (not (.Type.InterfaceType API)) -}}
    {{ unexport .Name.FieldName }} *{{ .Type.Go }}
  {{- else -}}
    {{ unexport .Name.FieldName }} {{ .Type.Go }}
  {{- end -}}
{{- end }}

{{ define "funcArgs" -}}
{{- if .BoxedInput -}}
  cmd *{{ .Inputs.Ref.Go }}
{{- else -}}
  {{- $inputs := .Inputs.Fields API -}}
  {{- range $i, $f := $inputs -}}
    {{- template "funcArg" $f -}}
    {{- if not (last $inputs $i) -}},{{- end -}}
  {{- end -}}
{{- end -}}
{{- end }}

{{ define "retVal" -}}
  {{- if eq .Type "" -}}
    (err error)
  {{- else if .List -}}
    (ret []{{ .Type.Go }}, err error)
  {{- else if and .Optional (not (.Type.InterfaceType API)) -}}
    (ret *{{ .Type.Go }}, err error)
  {{- else -}}
    (ret {{ .Type.Go }}, err error)
  {{- end -}}
{{- end }}

// {{ .Name.Go }} implements the "{{ .Name }}" QMP API call.
func (m *Monitor) {{ .Name.Go }}({{ template "funcArgs" . }}) {{ template "retVal" .Output }} {
  {{- if not .BoxedInput }}
  cmd := struct{
  {{- range .Inputs.Fields API }}
    {{ render . }}
  {{- end }}
  }{
  {{- range .Inputs.Fields API }}
    {{ unexport .Name.FieldName }},
  {{- end }}
  }
  {{- end }}
  bs, err := json.Marshal(map[string]interface{}{
    "execute": "{{ .Name }}",
    "arguments": cmd,
  })
  if err != nil {
    return
  }
  bs, err = m.mon.Run(bs)
  if err != nil {
    return
  }

  {{- if ne .Output.Type "" }}
    res := struct{
      Res json.RawMessage `json:"return"`
    }{}
    if err = json.Unmarshal(bs, &res); err != nil {
      return
    }
    {{- if .Output.Type.InterfaceType API }}
      {{- if .Output.List }}
        var reslist []json.RawMessage
        if err = json.Unmarshal([]byte(res.Res), &reslist); err != nil {
          return
        }
        for _, r := range reslist {
          v, err := decode{{ .Output.Type.Go }}(r)
          if err != nil {
            return nil, err
          }
          ret = append(ret, v)
        }
      {{- else }}
        return decode{{ .Output.Type.Go }}(res.Res)
      {{- end }}
    {{- else }}
      if err = json.Unmarshal([]byte(res.Res), &ret); err != nil {
        return
      }
    {{- end }}
  {{- end }}
  return
}