Add timeout parameter to CLI commands that execute actions

This commit is contained in:
Tim Van Baak 2025-02-21 20:53:24 -08:00
parent ee32e8ecd4
commit 1795fe94b1
3 changed files with 42 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"log"
"slices"
"time"
"github.com/Jaculabilis/intake/core"
"github.com/spf13/cobra"
@ -30,6 +31,7 @@ In a dry run, the item will be printed in the chosen format and not updated.
stringArg(cmd, "action"),
stringArg(cmd, "item"),
stringArg(cmd, "format"),
stringArg(cmd, "timeout"),
boolArg(cmd, "dry-run"),
boolArg(cmd, "diff"),
boolArg(cmd, "force"),
@ -50,6 +52,10 @@ func init() {
actionExecuteCmd.MarkFlagRequired("action")
actionExecuteCmd.Flags().StringP("format", "f", "headlines", "Feed format for returned items")
actionExecuteCmd.Flags().StringP("timeout", "t", core.DefaultTimeout.String(),
fmt.Sprintf("Timeout duration (default: %s)", core.DefaultTimeout.String()))
actionExecuteCmd.Flags().Bool("dry-run", false, "Instead of updating the item, print it")
actionExecuteCmd.Flags().Bool("diff", false, "Show which fields of the item changed")
@ -62,6 +68,7 @@ func actionExecute(
action string,
itemId string,
format string,
timeout string,
dryRun bool,
diff bool,
force bool,
@ -77,6 +84,10 @@ func actionExecute(
if itemId == "" {
log.Fatal("error: --item is empty")
}
duration, err := time.ParseDuration(timeout)
if err != nil {
log.Fatalf("error: invalid duration: %v", err)
}
db := openAndMigrateDb()
@ -98,7 +109,7 @@ func actionExecute(
}
}
newItem, newState, errItem, err := core.ExecuteItemAction(item, argv, envs, state, core.DefaultTimeout, postProcess)
newItem, newState, errItem, err := core.ExecuteItemAction(item, argv, envs, state, duration, postProcess)
if err != nil {
core.AddErrorItem(db, errItem)
log.Fatalf("error executing %s: %v", action, err)

View File

@ -24,7 +24,12 @@ the source will not be updated with the fetch result.
%s`, makeFormatHelpText()),
Run: func(cmd *cobra.Command, args []string) {
sourceFetch(stringArg(cmd, "source"), stringArg(cmd, "format"), boolArg(cmd, "dry-run"))
sourceFetch(
stringArg(cmd, "source"),
stringArg(cmd, "format"),
stringArg(cmd, "timeout"),
boolArg(cmd, "dry-run"),
)
},
}
@ -36,11 +41,18 @@ func init() {
sourceFetchCmd.Flags().StringP("format", "f", "headlines", "Feed format for returned items.")
sourceFetchCmd.Flags().Bool("dry-run", false, "Instead of updating the source, print the fetched items")
sourceFetchCmd.Flags().StringP("timeout", "t", core.DefaultTimeout.String(),
fmt.Sprintf("Timeout duration (default: %s)", core.DefaultTimeout.String()))
}
func sourceFetch(source string, format string, dryRun bool) {
func sourceFetch(source string, format string, timeout string, dryRun bool) {
formatter := formatAs(format)
duration, err := time.ParseDuration(timeout)
if err != nil {
log.Fatalf("error: invalid duration: %v", err)
}
db := openAndMigrateDb()
state, envs, argv, postProcess, err := core.GetSourceActionInputs(db, source, "fetch")
@ -48,7 +60,7 @@ func sourceFetch(source string, format string, dryRun bool) {
log.Fatalf("error: failed to load data for %s: %v", source, err)
}
items, newState, errItem, err := core.Execute(source, argv, envs, state, "", core.DefaultTimeout, postProcess)
items, newState, errItem, err := core.Execute(source, argv, envs, state, "", duration, postProcess)
if err != nil {
core.AddErrorItem(db, errItem)
log.Fatalf("error: failed to execute fetch: %v", err)

View File

@ -19,7 +19,12 @@ such as INTAKE_TTL, will not be applied by --env.
%s`, makeFormatHelpText()),
Run: func(cmd *cobra.Command, args []string) {
sourceTest(stringArrayArg(cmd, "env"), stringArg(cmd, "format"), getArgv(cmd, args))
sourceTest(
stringArrayArg(cmd, "env"),
stringArg(cmd, "format"),
stringArg(cmd, "timeout"),
getArgv(cmd, args),
)
},
}
@ -28,12 +33,19 @@ func init() {
sourceTestCmd.Flags().StringArrayP("env", "e", nil, "Environment variables to set, in the form KEY=VAL")
sourceTestCmd.Flags().StringP("format", "f", "headlines", "Feed format for returned items.")
sourceTestCmd.Flags().StringP("timeout", "t", core.DefaultTimeout.String(),
fmt.Sprintf("Timeout duration (default: %s)", core.DefaultTimeout.String()))
}
func sourceTest(env []string, format string, cmd []string) {
func sourceTest(env []string, format string, timeout string, cmd []string) {
formatter := formatAs(format)
items, state, _, err := core.Execute("test", cmd, env, nil, "", core.DefaultTimeout, nil)
duration, err := time.ParseDuration(timeout)
if err != nil {
log.Fatalf("error: invalid duration: %v", err)
}
items, state, _, err := core.Execute("test", cmd, env, nil, "", duration, nil)
log.Printf("returned %d items", len(items))
log.Printf("wrote %d bytes of state", len(state))
if err != nil {