intake/cmd/sourceTest.go

43 lines
1.1 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-01-23 21:22:38 +00:00
%s`, makeFormatHelpText()),
2025-01-17 21:49:23 +00:00
Run: func(cmd *cobra.Command, args []string) {
2025-01-30 15:54:13 +00:00
sourceTest(stringArrayArg(cmd, "env"), stringArg(cmd, "format"), 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.")
2025-01-17 21:49:23 +00:00
}
2025-01-30 15:54:13 +00:00
func sourceTest(env []string, format string, cmd []string) {
formatter := formatAs(format)
2025-01-17 21:49:23 +00:00
items, state, err := core.Execute("test", cmd, env, nil, "", time.Minute)
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))
}
}