-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery.go
More file actions
92 lines (81 loc) · 2.49 KB
/
query.go
File metadata and controls
92 lines (81 loc) · 2.49 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
// Copyright (c) 2020–2024 The query developers. All rights reserved.
// Project site: https://github.com/gotmc/query
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.
package query
import (
"fmt"
"strconv"
"strings"
)
// Querier provides the interface to query using a given command and provide
// the resultant string. The command string should include the appropriate
// terminator for the instrument.
type Querier interface {
Query(cmd string) (string, error)
}
// Bool queries a Querier with the given command and returns a bool.
func Bool(q Querier, cmd string) (bool, error) {
s, err := q.Query(cmd)
if err != nil {
return false, err
}
switch strings.TrimSpace(s) {
case "OFF", "0":
return false, nil
case "ON", "1":
return true, nil
default:
return false, fmt.Errorf("could not determine boolean status from %s", s)
}
}
// Boolf queries the Querier according to a format specifier and returns a
// bool.
func Boolf(q Querier, format string, a ...any) (bool, error) {
return Bool(q, fmt.Sprintf(format, a...))
}
// Float64 queries the Querier with the given command and returns a float64.
func Float64(q Querier, cmd string) (float64, error) {
s, err := q.Query(cmd)
if err != nil {
return 0.0, err
}
return strconv.ParseFloat(strings.TrimSpace(s), 64)
}
// Float64f queries the querier according to a format specifier and returns a
// float.
func Float64f(q Querier, format string, a ...any) (float64, error) {
return Float64(q, fmt.Sprintf(format, a...))
}
// Int queries the querier with the given command and returns an int.
func Int(q Querier, cmd string) (int, error) {
s, err := q.Query(cmd)
if err != nil {
return 0, err
}
s = strings.TrimSpace(s)
i, err := strconv.Atoi(s)
if err != nil {
// The string might be formatted to in scientific format.
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0, err
}
return int(f), nil
}
return i, nil
}
// Intf queries the querier according to a format specifier and returns a
// int.
func Intf(q Querier, format string, a ...any) (int, error) {
return Int(q, fmt.Sprintf(format, a...))
}
// String queries the querier with the given command and returns a string.
func String(q Querier, cmd string) (string, error) {
return q.Query(cmd)
}
// Stringf queries the querier according to a format specifier and returns a
// string.
func Stringf(q Querier, format string, a ...any) (string, error) {
return String(q, fmt.Sprintf(format, a...))
}