56 lines
1 KiB
Go
56 lines
1 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/json"
|
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/zet"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var findCmd = &cobra.Command{
|
|
Use: "find",
|
|
Aliases: []string{"f", "s", "search"},
|
|
Short: "Print IDs and titles of zettels matching QUERY",
|
|
Long: `zet find QUERY
|
|
|
|
If -j or --json is added, the results will be in JSON format.`,
|
|
Run: func(cli *cobra.Command, args []string) {
|
|
if len(args) < 1 {
|
|
fmt.Println("No query")
|
|
|
|
os.Exit(1)
|
|
}
|
|
|
|
zets := zet.SearchZets(args[0])
|
|
|
|
if jsonOutput {
|
|
zetList := zet.CreateZetList(zets, false)
|
|
|
|
result := zetList.Parse()
|
|
|
|
json, err := json.AsJSON(result)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
fmt.Println(json)
|
|
} else {
|
|
zetList := zet.CreateZetList(zets)
|
|
|
|
result := zetList.Parse()
|
|
|
|
fmt.Println(strings.Join(result, "\n"))
|
|
}
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(findCmd)
|
|
|
|
findCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format")
|
|
}
|