/
system.go
162 lines (137 loc) · 4.3 KB
/
system.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package pkg
import (
"bytes"
"encoding/json"
"fmt"
"runtime"
"text/template"
"github.com/MakeNowJust/heredoc"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// SysInfo contains information about the system.
type SysInfo struct {
OS string
Kernel string
KernelVersion string
Arch string
Processor string
}
// SystemConfig defines the configuration for the "system" source.
type SystemConfig struct {
Core []string `yaml:"core,omitempty"`
}
// Render the SystemConfig into its corresponding SystemResult.
func (c SystemConfig) Render() (Result, error) {
l := log.WithField("src", "system")
l.Debug("starting render")
result := NewSystemResult()
info, err := LoadSystemInfo()
if err != nil {
l.WithField("err", err).Debug("error collecting system info")
cliWarnings.Add("system.core", "error collecting system info")
}
for _, opt := range c.Core {
switch opt {
case "os":
result.OS = info.OS
case "arch":
result.Arch = info.Arch
case "cpus":
result.CPUs = runtime.NumCPU()
case "kernel":
result.Kernel = info.Kernel
case "kernel_version", "kernel-version":
result.KernelVersion = info.KernelVersion
case "processor":
result.Processor = info.Processor
default:
l.WithField("opt", opt).Debug("unsupported core system option")
return result, fmt.Errorf("unsupported core system option: %s", opt)
}
}
return result, nil
}
// SystemResult contains the result data from rendering an "system" source.
type SystemResult struct {
// Common
ResultCommon `json:"-" yaml:"-"`
// Core
OS string `yaml:"os,omitempty" json:"os,omitempty"`
Arch string `yaml:"arch,omitempty" json:"arch,omitempty"`
CPUs int `yaml:"cpus,omitempty" json:"cpus,omitempty"`
Kernel string `yaml:"kernel,omitempty" json:"kernel,omitempty"`
KernelVersion string `yaml:"kernel_version,omitempty" json:"kernel_version,omitempty"`
Processor string `yaml:"processor,omitempty" json:"processor,omitempty"`
}
// NewSystemResult creates a new instance of an SystemResult.
func NewSystemResult() SystemResult {
return SystemResult{
ResultCommon: common,
}
}
// IsEmpty checks whether the result contains any data.
func (r SystemResult) IsEmpty() bool {
return r.OS == "" && r.Arch == "" && r.CPUs == 0 && r.KernelVersion == "" && r.Kernel == "" && r.Processor == ""
}
// Markdown renders the SystemResult to markdown.
func (r SystemResult) Markdown() ([]byte, error) {
log.WithField("src", "system").Debug("rendering to markdown")
if r.IsEmpty() {
return []byte{}, nil
}
md := heredoc.Doc(`
**System**{{ if .OS }}
- _os_: {{ .OS }}{{ end }}{{ if .Arch }}
- _arch_: {{ .Arch }}{{ end }}{{ if .CPUs }}
- _cpus_: {{ .CPUs }}{{ end }}{{ if .Kernel }}
- _kernel_: {{ .Kernel }}{{ end }}{{ if .KernelVersion }}
- _kernel version_: {{ .KernelVersion }}{{ end }}{{ if .Processor }}
- _processor_: {{ .Processor }}{{ end }}
`)
t := template.Must(template.New("system-md").Parse(md))
buffer := bytes.Buffer{}
if err := t.Execute(&buffer, r); err != nil {
return []byte{}, err
}
return buffer.Bytes(), nil
}
// Plaintext renders the SystemResult to plaintext.
func (r SystemResult) Plaintext() ([]byte, error) {
log.WithField("src", "system").Debug("rendering to plaintext")
if r.IsEmpty() {
return []byte{}, nil
}
plaintext := heredoc.Doc(`
System
------{{ if .OS }}
os: {{ .OS }}{{ end }}{{ if .Arch }}
arch: {{ .Arch }}{{ end }}{{ if .CPUs }}
cpus: {{ .CPUs }}{{ end }}{{ if .Kernel }}
kernel: {{ .Kernel }}{{ end }}{{ if .KernelVersion }}
kernel version: {{ .KernelVersion }}{{ end }}{{ if .Processor }}
processor: {{ .Processor }}{{ end }}
`)
t := template.Must(template.New("system-txt").Parse(plaintext))
buffer := bytes.Buffer{}
if err := t.Execute(&buffer, r); err != nil {
return []byte{}, err
}
return buffer.Bytes(), nil
}
// YAML renders the SystemResult to YAML.
func (r SystemResult) YAML() ([]byte, error) {
log.WithField("src", "system").Debug("rendering to YAML")
if r.IsEmpty() {
return []byte{}, nil
}
return yaml.Marshal(&r)
}
// JSON renders the SystemResult to JSON.
func (r SystemResult) JSON() ([]byte, error) {
log.WithField("src", "system").Debug("rendering to JSON")
if r.IsEmpty() {
return []byte{}, nil
}
return json.Marshal(&r)
}