Skip to content

Commit

Permalink
Revert "Read from standard input"
Browse files Browse the repository at this point in the history
This reverts commit 1bc39c2.
  • Loading branch information
spacez320 committed Nov 11, 2024
1 parent 1bc39c2 commit 88dee6a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 93 deletions.
22 changes: 5 additions & 17 deletions cmd/shui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ var (
showStatus bool // Whether or not to show statuses.
showVersion bool // Whether or not to display a version.
silent bool // Whether or not to be quiet.
readStdin bool // Whether input comes from standard input.

// Supplied by the linker at build time.
version string
Expand Down Expand Up @@ -157,21 +156,11 @@ func main() {
os.Exit(0)
}

// Detect if running from standard input.
f, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if f.Mode()&os.ModeNamedPipe != 0 {
// We are reading standard input.
readStdin = true
} else {
// There is no standard input--queries are needed.
if len(queries) == 0 {
flag.Usage()
fmt.Fprintf(os.Stderr, "Missing required argument -query\n")
os.Exit(1)
}
// Check for required flags.
if len(queries) == 0 {
flag.Usage()
fmt.Fprintf(os.Stderr, "Missing required argument -query\n")
os.Exit(1)
}

// Set-up logging.
Expand Down Expand Up @@ -218,7 +207,6 @@ func main() {
PrometheusExporterAddr: promExporterAddr,
PushgatewayAddr: promPushgatewayAddr,
Queries: queries,
ReadStdin: readStdin,
}

// Build display configuration.
Expand Down
2 changes: 1 addition & 1 deletion internal/lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Config struct {
Count, Delay, DisplayMode, Mode int
ElasticsearchAddr, ElasticsearchIndex, ElasticsearchPassword, ElasticsearchUser string
Expressions, Filters, Labels, Queries []string
History, LogMulti, ReadStdin, Silent bool
History, LogMulti, Silent bool
LogLevel string
Port string
PrometheusExporterAddr string
Expand Down
63 changes: 12 additions & 51 deletions internal/lib/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
package lib

import (
"bufio"
"io"
"log/slog"
"os"
"os/exec"
"strconv"
"time"
Expand All @@ -16,11 +14,6 @@ import (
const (
QUERY_MODE_COMMAND int = iota + 1 // Queries are commands.
QUERY_MODE_PROFILE // Queries are PIDs to profile.
QUERY_MODE_STDIN // Results are fron stdin.
)

var (
stdinScanner = bufio.NewScanner(os.Stdin) // Scanner for standard input queries.
)

// Wrapper for query execution.
Expand Down Expand Up @@ -97,17 +90,6 @@ func runQueryProfile(pid string, history bool) {
AddResult(pid, runProfile(pidInt), history)
}

// Reads standard input for results.
func runQueryStdin(query string, history bool) {
slog.Debug("Reading stdin")

stdinScanner.Scan()
err := stdinScanner.Err()
e(err)

AddResult(query, stdinScanner.Text(), history)
}

// Entrypoint for 'query' mode.
func Query(
queryMode, attempts, delay int,
Expand All @@ -125,20 +107,21 @@ func Query(
// Start the RPC server.
initServer(port)

for _, query := range queries {
// Initialize pause channels.
pauseQueryChans[query] = make(chan bool)
}

go func() {
// Wait for result consumption to become ready.
slog.Debug("Waiting for results readiness")
<-resultsReadyChan

// Execute the queries.
switch queryMode {
case QUERY_MODE_COMMAND:
slog.Debug("Executing in query mode command")

for _, query := range queries {
// Initialize pause channels.
pauseQueryChans[query] = make(chan bool)

for _, query := range queries {
// Execute the queries.
switch queryMode {
case QUERY_MODE_COMMAND:
slog.Debug("Executing in query mode command")
go runQuery(
query,
attempts,
Expand All @@ -148,14 +131,8 @@ func Query(
pauseQueryChans[query],
runQueryExec,
)
}
case QUERY_MODE_PROFILE:
slog.Debug("Executing in query mode profile")

for _, query := range queries {
// Initialize pause channels.
pauseQueryChans[query] = make(chan bool)

case QUERY_MODE_PROFILE:
slog.Debug("Executing in query mode profile")
go runQuery(
query,
attempts,
Expand All @@ -166,22 +143,6 @@ func Query(
runQueryProfile,
)
}
case QUERY_MODE_STDIN:
// When executing by reading standard input, there is only ever one "query".
slog.Debug("Executing in query mode stdin")

// Initialize pause channels.
pauseQueryChans[queries[0]] = make(chan bool)

go runQuery(
queries[0],
attempts,
delay,
history,
doneQueryChan,
pauseQueryChans[queries[0]],
runQueryStdin,
)
}
}()

Expand Down
24 changes: 0 additions & 24 deletions shui.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ const (
MODE_READ // For running in 'read' mode.
)

const (
STDIN_QUERY_NAME = "stdin" // Named query value for reading stdin.
)

var (
ctx = context.Background() // Initialize context.
)
Expand All @@ -41,28 +37,8 @@ func Run(config lib.Config, displayConfig lib.DisplayConfig) {
slog.Debug("Running with config", "config", config)
slog.Debug("Running with display config", "displayConfig", displayConfig)

// Define 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,
config.Delay,
config.Queries,
config.Port,
config.History,
resultsReadyChan,
)

// Use labels that match the defined value for queries.
ctx = context.WithValue(ctx, "labels", config.Queries)
case config.Mode == int(MODE_PROFILE):
slog.Debug("Executing in profile mode")

Expand Down

0 comments on commit 88dee6a

Please sign in to comment.