-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshui.go
129 lines (107 loc) · 3.15 KB
/
shui.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
//
// Launcher for Shui.
package shui
import (
"context"
"fmt"
"log/slog"
"os"
"github.com/spacez320/shui/internal/lib"
)
// Represents the mode value.
type queryMode int
// Mode constants.
const (
MODE_QUERY queryMode = iota + 1 // For running in 'query' mode.
MODE_PROFILE // For running in 'profile' mode.
MODE_READ // For running in 'read' mode.
)
const (
STDIN_QUERY_NAME = "stdin" // Named query value for reading stdin.
)
var (
ctx = context.Background() // Initialize context.
)
// 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)
}