-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshui.go
More file actions
154 lines (128 loc) · 3.71 KB
/
Copy pathshui.go
File metadata and controls
154 lines (128 loc) · 3.71 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
//
// Launcher for Shui.
package shui
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"github.com/spacez320/shui/internal/lib"
)
// Represents the mode value.
type QueryMode int
// Fetches a common name from a query mode value.
func (q *QueryMode) String() string {
return QueryModes[*q]
}
// Mode constants.
const (
MODE_QUERY QueryMode = iota // For running in 'query' mode. First to serve as the 'default.'
MODE_PROFILE // For running in 'profile' mode.
MODE_READ // For running in 'read' mode.
)
// Misc. constants.
const (
STDIN_QUERY_NAME = "stdin" // Named query value for reading stdin.
)
var (
ctx = context.Background() // Initialize context.
// Mapping of mode constants to a common mode name.
QueryModes = map[QueryMode]string{
MODE_PROFILE: "profile",
MODE_QUERY: "query",
MODE_READ: "read",
}
)
// Fetches a query mode value from its common name.
func QueryModeFromString(s string) (QueryMode, error) {
for k, v := range QueryModes {
if s == v {
return k, nil
}
}
return 0, errors.New(fmt.Sprintf("Unknown query mode %s", s))
}
// Executes a Shui.
func Run(config lib.Config, displayConfig lib.DisplayConfig) {
var (
doneQueriesChan chan bool // Channel for tracking query completion.
pauseQueryChans map[string]chan bool // Channels for pausing queries.
resultsReadyChan = make(chan bool) // Channel for signaling results readiness.
)
slog.Debug("Running with config", "config", config)
slog.Debug("Running with display config", "displayConfig", displayConfig)
// Define a special query value when reading standard input.
if config.ReadStdin {
config.Queries = []string{STDIN_QUERY_NAME}
}
// Execute the specified mode.
switch {
case config.ReadStdin:
slog.Debug("Reading from standard input")
doneQueriesChan, pauseQueryChans = lib.Query(
lib.QUERY_MODE_STDIN,
-1, // Stdin mode is always continuous and the query itself must detect EOF.
config.Delay,
config.Queries,
config.Port,
config.History,
resultsReadyChan,
)
// Use labels that match the defined value for queries.
ctx = context.WithValue(ctx, "labels", config.Labels)
case config.Mode == int(MODE_PROFILE):
slog.Debug("Executing in profile mode")
doneQueriesChan, pauseQueryChans = lib.Query(
lib.QUERY_MODE_PROFILE,
config.Count,
config.Delay,
config.Queries,
config.Port,
config.History,
resultsReadyChan,
)
// Process mode has specific labels--ignore user provided ones.
ctx = context.WithValue(ctx, "labels", lib.ProfileLabels)
case config.Mode == int(MODE_QUERY):
slog.Debug("Executing in query mode")
doneQueriesChan, pauseQueryChans = lib.Query(
lib.QUERY_MODE_COMMAND,
config.Count,
config.Delay,
config.Queries,
config.Port,
config.History,
resultsReadyChan,
)
// Rely on user-defined labels.
ctx = context.WithValue(ctx, "labels", config.Labels)
case config.Mode == int(MODE_READ):
slog.Debug("Executing in read mode")
// FIXME Temporarily disabling read mode.
// done = lib.Read(port)
default:
slog.Error(fmt.Sprintf("Invalid mode: %d\n", config.Mode))
os.Exit(1)
}
// Initialize remaining context.
ctx = context.WithValue(ctx, "expressions", config.Expressions)
ctx = context.WithValue(ctx, "filters", config.Filters)
ctx = context.WithValue(ctx, "queries", config.Queries)
// Execute result viewing.
if !config.Silent {
go lib.Results(
ctx,
lib.DisplayMode(config.DisplayMode),
ctx.Value("queries").([]string)[0], // Always start with the first query.
config.History,
&displayConfig,
&config,
pauseQueryChans,
resultsReadyChan,
)
}
<-doneQueriesChan
slog.Debug("Received the last result, nothing left to do")
close(doneQueriesChan)
}