-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.go
More file actions
188 lines (163 loc) · 7.64 KB
/
api.go
File metadata and controls
188 lines (163 loc) · 7.64 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// package main contains the api for the CLI
package main
import (
"fmt"
)
type AgeCmp struct {
Name string `arg:"" required:"" help:"[name of command or path to command]"`
Op string `arg:"" required:"" enum:"gt,lt" help:"[gt|lt]"`
Val string `arg:"" required:""`
Unit string `arg:"" required:"" enum:"s,second,seconds,m,minute,minutes,h,hour,hours,d,day,days"`
}
//nolint:lll,govet,nolintlint
type OutputCmp struct {
Stream string `arg:"" required:"" enum:"stdout,stderr,combined" help:"[output stream to capture: (stdout|stderr|combined)]"`
Command string `arg:"" required:"" help:"[name of command or path to command plus any arguments e.g. \"uname -a\"]"`
Op string `arg:"" required:"" enum:"eq,ne,gt,gte,in,lt,lte,like,unlike" help:"[eq|ne|gt|gte|in|like|lt|lte|unlike]"`
Val string `arg:"" required:""`
Arg []string `short:"a" optional:"" help:"--arg=\"-V\" --arg foo"`
Compare string `default:"optimistic" enum:"float,integer,string,version,optimistic" help:"[float|integer|string|version|optimistic]"`
}
//nolint:lll,govet,nolintlint
type VersionCmp struct {
Name string `arg:"" required:"" help:"[name of command or path to command]"`
Op string `arg:"" required:"" enum:"eq,ne,gt,gte,in,lt,lte,like,unlike" help:"[eq|ne|gt|gte|in|like|lt|lte|unlike]"`
Val string `arg:"" required:""`
Major bool `xor:"Major,Minor,Patch" help:"Only match on the major version (e.g. major.minor.patch)"`
Minor bool `xor:"Major,Minor,Patch" help:"Only match on the minor version (e.g. major.minor.patch)"`
Patch bool `xor:"Major,Minor,Patch" help:"Only match on the patch version (e.g. major.minor.patch)"`
}
type ArchCmd struct {
Op string `arg:"" required:"" enum:"eq,ne,in,like,unlike" help:"[eq|ne|in|like|unlike]"`
Val string `arg:"" required:""`
}
// CLICmd type is configuration for CLI checks.
//
//nolint:lll,govet,nolintlint
type CLICmd struct {
Version VersionCmp `cmd:"" help:"Check version of command. e.g. \"is cli version tmux gte 3\""`
Age AgeCmp `cmd:"" help:"Check last modified time of cli (2h, 4d). e.g. \"is cli age tmux gt 1 d\""`
Output OutputCmp `cmd:"" help:"Check output of a command. e.g. \"is cli output stdout \"uname -a\" like \"Kernel Version 22.5\""`
}
// FSOCmd type is configuration for FSO checks.
//
//nolint:lll,govet,nolintlint
type FSOCmd struct {
Age AgeCmp `cmd:"" help:"Check age (last modified time) of an fso (2h, 4d). e.g. \"is fso age /tmp/log.txt gt 1 d\""`
}
// OSCmd type is configuration for OS level checks.
//
//nolint:lll,govet,nolintlint
type OSCmd struct {
Attr string `arg:"" required:"" name:"attribute" help:"[id|id-like|pretty-name|name|version|version-codename]"`
Op string `arg:"" required:"" enum:"eq,ne,gt,gte,in,lt,lte,like,unlike" help:"[eq|ne|gt|gte|in|like|lt|lte|unlike]"`
Val string `arg:"" required:""`
Major bool `xor:"Major,Minor,Patch" help:"Only match on the major OS version (e.g. major.minor.patch)"`
Minor bool `xor:"Major,Minor,Patch" help:"Only match on the minor OS version (e.g. major.minor.patch)"`
Patch bool `xor:"Major,Minor,Patch" help:"Only match on the patch OS version (e.g. major.minor.patch)"`
}
// Battery type is configuration for battery information.
//
//nolint:lll,govet,nolintlint
type Battery struct {
Attr string `arg:"" required:"" name:"attribute" enum:"charge-rate,count,current-capacity,current-charge,design-capacity,design-voltage,last-full-capacity,state,voltage" help:"[charge-rate|count|current-capacity|current-charge|design-capacity|design-voltage|last-full-capacity|state|voltage]"`
Nth int `optional:"" default:"1" help:"Specify which battery to use (1 for the first battery)"`
Round bool `help:"Round float values to the nearest integer"`
}
type Audio struct {
Attr string `arg:"" required:"" enum:"level,muted" help:"[level|muted]"`
}
//nolint:lll,govet,nolintlint
type AudioCmd struct {
Audio
Op string `arg:"" required:"" default:"eq" enum:"eq,ne,gt,gte,in,lt,lte,like,unlike" help:"[eq|ne|gt|gte|in|like|lt|lte|unlike]"`
Val string `arg:"" optional:"" help:"Value to compare against"`
}
func (r *AudioCmd) Validate() error {
// For "muted" attribute without a value, we're just checking if audio is muted
if r.Attr == "muted" && r.Val == "" {
return nil
}
// For other attributes or when comparing muted to a specific value
if r.Val == "" && r.Op != "eq" && r.Op != "ne" {
return fmt.Errorf("missing required argument: val")
}
return nil
}
type Summary struct {
Attr string `arg:"" required:"" name:"attribute" enum:"battery,os,var" help:"[battery|os|var]"`
Nth int `optional:"" default:"1" help:"Specify which battery to use (1 for the first battery)"`
JSON bool `xor:"format" help:"print summary as JSON"`
MD bool `xor:"format" help:"print summary as a Markdown table"`
}
//nolint:lll,govet,nolintlint
type BatteryCmd struct {
Battery
Op string `arg:"" required:"" enum:"eq,ne,gt,gte,in,lt,lte,like,unlike" help:"[eq|ne|gt|gte|in|like|lt|lte|unlike]"`
Val string `arg:"" required:""`
}
// UserCmd type is configuration for user level checks.
//
//nolint:lll,govet,nolintlint
type UserCmd struct {
Sudoer string `arg:"" required:"" default:"sudoer" enum:"sudoer" help:"is current user a passwordless sudoer. e.g. \"is user sudoer\""`
}
// VarCmd type is configuration for environment variable checks.
//
//nolint:lll,govet,nolintlint
type VarCmd struct {
Name string `arg:"" required:""`
Op string `arg:"" required:"" enum:"set,unset,true,false,eq,ne,gt,gte,in,lt,lte,like,unlike" help:"[set|unset|true|false|eq|ne|gt|gte|in|like|lt|lte|unlike]"`
Val string `arg:"" optional:""`
Compare string `default:"optimistic" enum:"float,integer,string,version,optimistic" help:"[float|integer|string|version|optimistic]"`
}
// Validate allows for some commands to forego requiring an additional argument.
func (r *VarCmd) Validate() error {
switch r.Op {
case "set", "unset", "true", "false", "eq", "ne":
return nil
default:
if r.Val == "" {
return fmt.Errorf("missing required argument: val")
}
}
return nil
}
type Version struct {
Major bool `xor:"Major,Minor,Patch" help:"Only print the major version (e.g. major.minor.patch)"`
Minor bool `xor:"Major,Minor,Patch" help:"Only print the minor version (e.g. major.minor.patch)"`
Patch bool `xor:"Major,Minor,Patch" help:"Only print the patch version (e.g. major.minor.patch)"`
}
type KnownCLI struct {
Attr string `arg:"" name:"attribute" required:"" enum:"version"`
Name string `arg:"" required:""`
Version
}
type KnownVar struct {
Name string `arg:"" required:""`
JSON bool `help:"Print output in JSON format"`
}
type KnownOS struct {
//nolint:lll
Attr string `arg:"" required:"" name:"attribute" help:"[id|id-like|pretty-name|name|version|version-codename]"`
Version
}
// KnownCmd type is configuration for printing environment info.
type KnownCmd struct {
Arch struct {
Attr string `arg:"" required:"" default:"arch" enum:"arch"`
} `cmd:"" help:"Print arch without check. e.g. \"is known arch\""`
OS KnownOS `cmd:"" help:"Print without check. e.g. \"is known os name\""`
CLI KnownCLI `cmd:"" help:"Print without check. e.g. \"is known cli version git\""`
Var KnownVar `cmd:"" help:"Print env var without a check. e.g. \"is known var PATH\""`
Battery Battery `cmd:"" help:"Print battery information. e.g. \"is known battery state\""`
Summary Summary `cmd:"" help:"summary of available data."`
Audio Audio `cmd:"" help:"Print without check. e.g. \"is known audio level\""`
}
// ThereCmd is configuration for finding executables.
type ThereCmd struct {
Name string `arg:"" required:""`
All bool `help:"Print all found binaries"`
JSON bool `help:"Print output in JSON format"`
Verbose bool `help:"Show binary versions"`
}