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
|
@ -1,24 +0,0 @@
|
|||
package cmd
|
||||
|
||||
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(cmd *cobra.Command, args []string) {
|
||||
title := ""
|
||||
|
||||
if len(args) > 0 {
|
||||
title = strings.Join(args, " ")
|
||||
}
|
||||
|
||||
zet.CreateZet(title)
|
||||
},
|
||||
}
|
73
cmd/edit.go
73
cmd/edit.go
|
@ -1,73 +0,0 @@
|
|||
package cmd
|
||||
|
||||
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(cmd *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)
|
||||
},
|
||||
}
|
48
cmd/find.go
48
cmd/find.go
|
@ -1,48 +0,0 @@
|
|||
package cmd
|
||||
|
||||
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(cmd *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() {
|
||||
findCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format")
|
||||
}
|
39
cmd/links.go
39
cmd/links.go
|
@ -1,39 +0,0 @@
|
|||
package cmd
|
||||
|
||||
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(cmd *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)
|
||||
}
|
||||
},
|
||||
}
|
43
cmd/root.go
43
cmd/root.go
|
@ -1,43 +0,0 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var jsonOutput bool
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "zet",
|
||||
Short: "Zettlekasten note manager",
|
||||
Long: `Zettlekasten note manager`,
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(createCmd)
|
||||
rootCmd.AddCommand(editCmd)
|
||||
rootCmd.AddCommand(findCmd)
|
||||
rootCmd.AddCommand(linksCmd)
|
||||
rootCmd.AddCommand(titlesCmd)
|
||||
rootCmd.AddCommand(viewCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
|
||||
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.zet.yaml)")
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
package cmd
|
||||
|
||||
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(cmd *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() {
|
||||
titlesCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format")
|
||||
}
|
73
cmd/view.go
73
cmd/view.go
|
@ -1,73 +0,0 @@
|
|||
package cmd
|
||||
|
||||
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(cmd *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))
|
||||
},
|
||||
}
|
13
cmd/zet/main.go
Normal file
13
cmd/zet/main.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/cli"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := cli.Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue