intake/cmd/sourceTest.go

53 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-17 21:49:23 +00:00
Long: `Execute a command as if it were a feed source's fetch action.
The display format of the returned items is the same as "intake feed".`,
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
}
},
}
var testEnv []string
var testFormat string
func init() {
2025-01-21 16:42:59 +00:00
sourceCmd.AddCommand(sourceTestCmd)
2025-01-17 21:49:23 +00:00
2025-01-21 16:42:59 +00:00
sourceTestCmd.Flags().StringArrayVarP(&testEnv, "env", "e", nil, "Environment variables to set, in the form KEY=VAL")
sourceTestCmd.Flags().StringVarP(&testFormat, "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-17 21:49:23 +00:00
formatter, err := core.FormatAs(testFormat)
if err != nil {
log.Fatal(err)
}
2025-01-23 20:26:21 +00:00
items, err := core.Execute("", cmd, testEnv, "", 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))
}
}