Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
Oliver Davies 2025-10-04 00:20:04 +01:00
parent 665ef94ca9
commit a3202aaff3
17 changed files with 63 additions and 56 deletions

2
build
View file

@ -4,6 +4,6 @@ set -euo pipefail
echo "Building..."
go build -o zet main.go
go build -o zet cmd/zet/main.go
echo "Done."

View file

@ -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)")
}

13
cmd/zet/main.go Normal file
View 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)
}
}

View file

@ -1,4 +1,4 @@
package cmd
package cli
import (
"strings"
@ -12,7 +12,7 @@ var createCmd = &cobra.Command{
Aliases: []string{"c", "n", "new"},
Short: "Create a new zettel",
Long: `zet create|new|c|n TITLE`,
Run: func(cmd *cobra.Command, args []string) {
Run: func(cli *cobra.Command, args []string) {
title := ""
if len(args) > 0 {
@ -22,3 +22,7 @@ var createCmd = &cobra.Command{
zet.CreateZet(title)
},
}
func init() {
rootCmd.AddCommand(createCmd)
}

View file

@ -1,4 +1,4 @@
package cmd
package cli
import (
"fmt"
@ -19,7 +19,7 @@ var editCmd = &cobra.Command{
Long: `zet edit|e ID
zet edit|e latest
`,
Run: func(cmd *cobra.Command, args []string) {
Run: func(cli *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Error: No id provided")
os.Exit(1)
@ -71,3 +71,7 @@ zet edit|e latest
zet.EditZet(id)
},
}
func init() {
rootCmd.AddCommand(editCmd)
}

View file

@ -1,4 +1,4 @@
package cmd
package cli
import (
"fmt"
@ -18,7 +18,7 @@ var findCmd = &cobra.Command{
Long: `zet find QUERY
If -j or --json is added, the results will be in JSON format.`,
Run: func(cmd *cobra.Command, args []string) {
Run: func(cli *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("No query")
@ -44,5 +44,7 @@ If -j or --json is added, the results will be in JSON format.`,
}
func init() {
rootCmd.AddCommand(findCmd)
findCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format")
}

View file

@ -1,4 +1,4 @@
package cmd
package cli
import (
"fmt"
@ -14,7 +14,7 @@ var linksCmd = &cobra.Command{
Aliases: []string{"l"},
Short: "TODO",
Long: `zet links QUERY`,
Run: func(cmd *cobra.Command, args []string) {
Run: func(cli *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("No query")
@ -37,3 +37,7 @@ var linksCmd = &cobra.Command{
}
},
}
func init() {
rootCmd.AddCommand(linksCmd)
}

17
internal/cli/root.go Normal file
View 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()
}

View file

@ -1,4 +1,4 @@
package cmd
package cli
import (
"fmt"
@ -17,7 +17,7 @@ var titlesCmd = &cobra.Command{
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) {
Run: func(cli *cobra.Command, args []string) {
zets := zet.GetAllZets()
result := zet.ParseZetList(zets)
@ -37,5 +37,7 @@ If -j or --json is added, the results will be in JSON format.`,
}
func init() {
rootCmd.AddCommand(titlesCmd)
titlesCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format")
}

View file

@ -1,4 +1,4 @@
package cmd
package cli
import (
"fmt"
@ -19,7 +19,7 @@ var viewCmd = &cobra.Command{
Long: `zet view|v ID
zet view|v latest
`,
Run: func(cmd *cobra.Command, args []string) {
Run: func(cli *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Error: No id provided")
os.Exit(1)
@ -71,3 +71,7 @@ zet view|v latest
fmt.Println(zet.ViewZet(id))
},
}
func init() {
rootCmd.AddCommand(viewCmd)
}