53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/Jaculabilis/intake/core"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var testCmd = &cobra.Command{
|
|
Use: "test [flags] -- command",
|
|
Short: "Test a feed source",
|
|
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 {
|
|
test(nil)
|
|
} else {
|
|
test(args[l:])
|
|
}
|
|
},
|
|
}
|
|
|
|
var testEnv []string
|
|
var testFormat string
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(testCmd)
|
|
|
|
testCmd.Flags().StringArrayVarP(&testEnv, "env", "e", nil, "Environment variables to set, in the form KEY=VAL")
|
|
testCmd.Flags().StringVarP(&testFormat, "format", "f", "headlines", "Feed format for returned items.")
|
|
}
|
|
|
|
func test(cmd []string) {
|
|
formatter, err := core.FormatAs(testFormat)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
items, err := core.Execute(cmd, testEnv, "", time.Minute)
|
|
log.Printf("Returned %d items", len(items))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
for _, item := range items {
|
|
fmt.Println(formatter(item))
|
|
}
|
|
}
|