Skip to content

Commit f08a0ea

Browse files
committed
feat: Add configurable result limit and fix display
- Implements a `--limit` flag for `run` and `results` commands to control the number of results returned. - Allows setting a default limit in the configuration file. A limit of 0 fetches all results, which is the new default. - Fixes a display issue where the "Waiting for job..." message was not printed on a new line.
1 parent 63f0999 commit f08a0ea

5 files changed

Lines changed: 11 additions & 5 deletions

File tree

cmd/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func addCommonFlags(fs *flag.FlagSet, cfg *splunk.Config) {
2626
fs.BoolVar(&cfg.Insecure, "insecure", cfg.Insecure, "Skip TLS certificate verification")
2727
fs.DurationVar(&cfg.HTTPTimeout, "http-timeout", cfg.HTTPTimeout, "Timeout for individual HTTP requests (e.g., '5s', '1m')")
2828
fs.BoolVar(&cfg.Debug, "debug", false, "Enable verbose debug logging")
29+
fs.IntVar(&cfg.Limit, "limit", cfg.Limit, "Maximum number of results to return (0 for all)")
2930
}
3031

3132
// getChoiceFromTTY reads a single line of input from the terminal, bypassing stdin.

cmd/results.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func resultsCmd(args []string, baseCfg splunk.Config) error {
4545
}
4646

4747
client.Log.Println("Fetching results...")
48-
results, err := client.Results(*sid)
48+
results, err := client.Results(*sid, baseCfg.Limit)
4949
if err != nil {
5050
return err
5151
}

cmd/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func runCmd(args []string, baseCfg splunk.Config) error {
5050
if err != nil {
5151
return err
5252
}
53-
client.Log.Printf("Job started with SID: %s", sid)
53+
client.Log.Printf("Job started with SID: %s\n", sid)
5454

5555
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
5656
defer cancel()
@@ -95,7 +95,7 @@ func runCmd(args []string, baseCfg splunk.Config) error {
9595
}
9696

9797
client.Log.Println("Fetching results...")
98-
results, err := client.Results(sid)
98+
results, err := client.Results(sid, baseCfg.Limit)
9999
if err != nil {
100100
return err
101101
}

splunk/client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,20 +292,22 @@ func (c *Client) WaitForJob(ctx context.Context, sid string) error {
292292
}
293293

294294
// Results fetches the results of a completed search job.
295-
func (c *Client) Results(sid string) (string, error) {
295+
func (c *Client) Results(sid string, limit int) (string, error) {
296296
endpoint, err := c.createAPIURL("search", "jobs", sid, "results")
297297
if err != nil {
298298
return "", err
299299
}
300300
c.Log.Debugf(`Request: GET %s
301-
`, endpoint)
301+
`,
302+
endpoint)
302303

303304
req, err := http.NewRequest("GET", endpoint, nil)
304305
if err != nil {
305306
return "", err
306307
}
307308
q := req.URL.Query()
308309
q.Add("output_mode", "json")
310+
q.Add("count", fmt.Sprintf("%d", limit))
309311
req.URL.RawQuery = q.Encode()
310312

311313
resp, err := c.doRequest(req)

splunk/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Config struct {
1919
Owner string `json:"owner"`
2020
Insecure bool `json:"insecure"`
2121
HTTPTimeout time.Duration `json:"httpTimeout"`
22+
Limit int `json:"limit"`
2223
Debug bool `json:"-"` // Exclude from JSON marshalling
2324
}
2425

@@ -55,6 +56,7 @@ func LoadConfigFromFile(customConfigPath string) (Config, string, error) {
5556
Owner string `json:"owner"`
5657
Insecure bool `json:"insecure"`
5758
HTTPTimeout string `json:"httpTimeout"`
59+
Limit int `json:"limit"`
5860
}
5961
var helper configHelper
6062
if err := json.NewDecoder(file).Decode(&helper); err != nil {
@@ -68,6 +70,7 @@ func LoadConfigFromFile(customConfigPath string) (Config, string, error) {
6870
cfg.App = strings.TrimSpace(helper.App)
6971
cfg.Owner = strings.TrimSpace(helper.Owner)
7072
cfg.Insecure = helper.Insecure
73+
cfg.Limit = helper.Limit
7174
if helper.HTTPTimeout != "" {
7275
parsedDuration, err := time.ParseDuration(helper.HTTPTimeout)
7376
if err != nil {

0 commit comments

Comments
 (0)