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
28
internal/cli/create.go
Normal file
28
internal/cli/create.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/zet"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var createCmd = &cobra.Command{
|
||||
Use: "create",
|
||||
Aliases: []string{"c", "n", "new"},
|
||||
Short: "Create a new zettel",
|
||||
Long: `zet create|new|c|n TITLE`,
|
||||
Run: func(cli *cobra.Command, args []string) {
|
||||
title := ""
|
||||
|
||||
if len(args) > 0 {
|
||||
title = strings.Join(args, " ")
|
||||
}
|
||||
|
||||
zet.CreateZet(title)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(createCmd)
|
||||
}
|
77
internal/cli/edit.go
Normal file
77
internal/cli/edit.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/fzf"
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/zet"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var editCmd = &cobra.Command{
|
||||
Use: "edit",
|
||||
Aliases: []string{"e"},
|
||||
Short: "Edit a specific zettel",
|
||||
Long: `zet edit|e ID
|
||||
zet edit|e latest
|
||||
`,
|
||||
Run: func(cli *cobra.Command, args []string) {
|
||||
if len(args) < 1 {
|
||||
fmt.Println("Error: No id provided")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var id int
|
||||
var query string
|
||||
|
||||
if args[0] == "latest" {
|
||||
id = zet.GetLatestZet()
|
||||
|
||||
zet.EditZet(id)
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if id, err := strconv.Atoi(args[0]); err == nil {
|
||||
zet.EditZet(id)
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
query = args[0]
|
||||
|
||||
ids := zet.SearchZets(query)
|
||||
|
||||
if len(ids) == 1 {
|
||||
zet.EditZet(ids[0])
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
zets := zet.ParseZetList(ids)
|
||||
|
||||
selected, err := fzf.SelectWithFzf(zets)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("No zet selected.")
|
||||
}
|
||||
|
||||
parts := strings.SplitN(selected, " ", 2)
|
||||
|
||||
id, err = strconv.Atoi(parts[0])
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
zet.EditZet(id)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(editCmd)
|
||||
}
|
50
internal/cli/find.go
Normal file
50
internal/cli/find.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
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])
|
||||
|
||||
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(findCmd)
|
||||
|
||||
findCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format")
|
||||
}
|
43
internal/cli/links.go
Normal file
43
internal/cli/links.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/zet"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var linksCmd = &cobra.Command{
|
||||
Use: "links",
|
||||
Aliases: []string{"l"},
|
||||
Short: "TODO",
|
||||
Long: `zet links QUERY`,
|
||||
Run: func(cli *cobra.Command, args []string) {
|
||||
if len(args) < 1 {
|
||||
fmt.Println("No query")
|
||||
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
zets := zet.SearchZets(args[0])
|
||||
|
||||
lines := zet.ParseZetList(zets)
|
||||
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
|
||||
parts := strings.SplitN(line, " ", 2)
|
||||
|
||||
id := parts[0]
|
||||
title := parts[1]
|
||||
|
||||
fmt.Printf("* link:../%s/index.adoc[%s]\n", id, title)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(linksCmd)
|
||||
}
|
17
internal/cli/root.go
Normal file
17
internal/cli/root.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var jsonOutput bool
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "zet",
|
||||
Short: "Zettlekasten note manager",
|
||||
Long: `Zettlekasten note manager`,
|
||||
}
|
||||
|
||||
func Execute() error {
|
||||
return rootCmd.Execute()
|
||||
}
|
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")
|
||||
}
|
77
internal/cli/view.go
Normal file
77
internal/cli/view.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/fzf"
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/zet"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var viewCmd = &cobra.Command{
|
||||
Use: "view",
|
||||
Aliases: []string{"v"},
|
||||
Short: "View a specific zettel",
|
||||
Long: `zet view|v ID
|
||||
zet view|v latest
|
||||
`,
|
||||
Run: func(cli *cobra.Command, args []string) {
|
||||
if len(args) < 1 {
|
||||
fmt.Println("Error: No id provided")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var id int
|
||||
var query string
|
||||
|
||||
if args[0] == "latest" {
|
||||
id = zet.GetLatestZet()
|
||||
|
||||
fmt.Println(zet.ViewZet(id))
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if id, err := strconv.Atoi(args[0]); err == nil {
|
||||
fmt.Println(zet.ViewZet(id))
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
query = args[0]
|
||||
|
||||
ids := zet.SearchZets(query)
|
||||
|
||||
if len(ids) == 1 {
|
||||
fmt.Println(zet.ViewZet(ids[0]))
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
zets := zet.ParseZetList(ids)
|
||||
|
||||
selected, err := fzf.SelectWithFzf(zets)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("No zet selected.")
|
||||
}
|
||||
|
||||
parts := strings.SplitN(selected, " ", 2)
|
||||
|
||||
id, err = strconv.Atoi(parts[0])
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(zet.ViewZet(id))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(viewCmd)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue