Add timeout parameter to CLI commands that execute actions
This commit is contained in:
parent
ee32e8ecd4
commit
1795fe94b1
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"slices"
|
"slices"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/Jaculabilis/intake/core"
|
"github.com/Jaculabilis/intake/core"
|
||||||
"github.com/spf13/cobra"
|
"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, "action"),
|
||||||
stringArg(cmd, "item"),
|
stringArg(cmd, "item"),
|
||||||
stringArg(cmd, "format"),
|
stringArg(cmd, "format"),
|
||||||
|
stringArg(cmd, "timeout"),
|
||||||
boolArg(cmd, "dry-run"),
|
boolArg(cmd, "dry-run"),
|
||||||
boolArg(cmd, "diff"),
|
boolArg(cmd, "diff"),
|
||||||
boolArg(cmd, "force"),
|
boolArg(cmd, "force"),
|
||||||
@ -50,6 +52,10 @@ func init() {
|
|||||||
actionExecuteCmd.MarkFlagRequired("action")
|
actionExecuteCmd.MarkFlagRequired("action")
|
||||||
|
|
||||||
actionExecuteCmd.Flags().StringP("format", "f", "headlines", "Feed format for returned items")
|
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("dry-run", false, "Instead of updating the item, print it")
|
||||||
|
|
||||||
actionExecuteCmd.Flags().Bool("diff", false, "Show which fields of the item changed")
|
actionExecuteCmd.Flags().Bool("diff", false, "Show which fields of the item changed")
|
||||||
@ -62,6 +68,7 @@ func actionExecute(
|
|||||||
action string,
|
action string,
|
||||||
itemId string,
|
itemId string,
|
||||||
format string,
|
format string,
|
||||||
|
timeout string,
|
||||||
dryRun bool,
|
dryRun bool,
|
||||||
diff bool,
|
diff bool,
|
||||||
force bool,
|
force bool,
|
||||||
@ -77,6 +84,10 @@ func actionExecute(
|
|||||||
if itemId == "" {
|
if itemId == "" {
|
||||||
log.Fatal("error: --item is empty")
|
log.Fatal("error: --item is empty")
|
||||||
}
|
}
|
||||||
|
duration, err := time.ParseDuration(timeout)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error: invalid duration: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
db := openAndMigrateDb()
|
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 {
|
if err != nil {
|
||||||
core.AddErrorItem(db, errItem)
|
core.AddErrorItem(db, errItem)
|
||||||
log.Fatalf("error executing %s: %v", action, err)
|
log.Fatalf("error executing %s: %v", action, err)
|
||||||
|
@ -24,7 +24,12 @@ the source will not be updated with the fetch result.
|
|||||||
|
|
||||||
%s`, makeFormatHelpText()),
|
%s`, makeFormatHelpText()),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
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().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().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)
|
formatter := formatAs(format)
|
||||||
|
|
||||||
|
duration, err := time.ParseDuration(timeout)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error: invalid duration: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
db := openAndMigrateDb()
|
db := openAndMigrateDb()
|
||||||
|
|
||||||
state, envs, argv, postProcess, err := core.GetSourceActionInputs(db, source, "fetch")
|
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)
|
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 {
|
if err != nil {
|
||||||
core.AddErrorItem(db, errItem)
|
core.AddErrorItem(db, errItem)
|
||||||
log.Fatalf("error: failed to execute fetch: %v", err)
|
log.Fatalf("error: failed to execute fetch: %v", err)
|
||||||
|
@ -19,7 +19,12 @@ such as INTAKE_TTL, will not be applied by --env.
|
|||||||
|
|
||||||
%s`, makeFormatHelpText()),
|
%s`, makeFormatHelpText()),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
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().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("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)
|
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("returned %d items", len(items))
|
||||||
log.Printf("wrote %d bytes of state", len(state))
|
log.Printf("wrote %d bytes of state", len(state))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user