intake/cmd/sourceTest.go

58 lines
1.5 KiB
Go
Raw Normal View History

2025-01-17 21:49:23 +00:00
package cmd
import (
"fmt"
"log"
"time"
"github.com/Jaculabilis/intake/core"
"github.com/spf13/cobra"
)
2025-01-21 16:42:59 +00:00
var sourceTestCmd = &cobra.Command{
Use: "test [flags] -- argv",
Short: "Test a fetch action",
2025-01-23 21:22:38 +00:00
Long: fmt.Sprintf(`Execute a command as if it were a feed source's fetch action.
2025-01-17 21:49:23 +00:00
2025-02-05 21:21:31 +00:00
Source-level configuration that is normally set via environment variable,
such as INTAKE_TTL, will not be applied by --env.
2025-01-23 21:22:38 +00:00
%s`, makeFormatHelpText()),
2025-01-17 21:49:23 +00:00
Run: func(cmd *cobra.Command, args []string) {
sourceTest(
stringArrayArg(cmd, "env"),
stringArg(cmd, "format"),
stringArg(cmd, "timeout"),
getArgv(cmd, args),
)
2025-01-17 21:49:23 +00:00
},
}
func init() {
2025-01-21 16:42:59 +00:00
sourceCmd.AddCommand(sourceTestCmd)
2025-01-17 21:49:23 +00:00
2025-01-30 15:54:13 +00:00
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()))
2025-01-17 21:49:23 +00:00
}
func sourceTest(env []string, format string, timeout string, cmd []string) {
2025-01-30 15:54:13 +00:00
formatter := formatAs(format)
2025-01-17 21:49:23 +00:00
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))
2025-01-17 21:49:23 +00:00
if err != nil {
log.Fatal(err)
}
for _, item := range items {
fmt.Println(formatter(item))
}
}