Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
665ef94ca9
commit
a3202aaff3
17 changed files with 63 additions and 56 deletions
43
internal/cli/titles.go
Normal file
43
internal/cli/titles.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/json"
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/zet"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var titlesCmd = &cobra.Command{
|
||||
Use: "titles",
|
||||
Aliases: []string{"t"},
|
||||
Short: "Print IDs and titles of zettels",
|
||||
Long: `Print the IDs and titles of all zettels.
|
||||
|
||||
If -j or --json is added, the results will be in JSON format.`,
|
||||
Run: func(cli *cobra.Command, args []string) {
|
||||
zets := zet.GetAllZets()
|
||||
|
||||
result := zet.ParseZetList(zets)
|
||||
|
||||
if jsonOutput {
|
||||
json, err := json.AsJSON(result)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(json)
|
||||
} else {
|
||||
fmt.Println(strings.Join(result, "\n"))
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(titlesCmd)
|
||||
|
||||
titlesCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue