intake/cmd/sourceTest.go

50 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) {
l := cmd.Flags().ArgsLenAtDash()
if l == -1 {
2025-01-21 16:42:59 +00:00
sourceTest(nil)
2025-01-17 21:49:23 +00:00
} else {
2025-01-21 16:42:59 +00:00
sourceTest(args[l:])
2025-01-17 21:49:23 +00:00
}
},
}
2025-01-29 17:07:57 +00:00
var sourceTestEnv []string
var sourceTestFormat string
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-29 17:07:57 +00:00
sourceTestCmd.Flags().StringArrayVarP(&sourceTestEnv, "env", "e", nil, "Environment variables to set, in the form KEY=VAL")
sourceTestCmd.Flags().StringVarP(&sourceTestFormat, "format", "f", "headlines", "Feed format for returned items.")
2025-01-17 21:49:23 +00:00
}
2025-01-21 16:42:59 +00:00
func sourceTest(cmd []string) {
2025-01-29 17:07:57 +00:00
formatter := formatAs(sourceTestFormat)
2025-01-17 21:49:23 +00:00
2025-01-29 17:07:57 +00:00
items, err := core.Execute("", cmd, sourceTestEnv, "", time.Minute)
2025-01-17 21:49:23 +00:00
log.Printf("Returned %d items", len(items))
if err != nil {
log.Fatal(err)
}
for _, item := range items {
fmt.Println(formatter(item))
}
}