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
2
build
2
build
|
@ -4,6 +4,6 @@ set -euo pipefail
|
||||||
|
|
||||||
echo "Building..."
|
echo "Building..."
|
||||||
|
|
||||||
go build -o zet main.go
|
go build -o zet cmd/zet/main.go
|
||||||
|
|
||||||
echo "Done."
|
echo "Done."
|
||||||
|
|
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)")
|
|
||||||
}
|
|
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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package cmd
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -12,7 +12,7 @@ var createCmd = &cobra.Command{
|
||||||
Aliases: []string{"c", "n", "new"},
|
Aliases: []string{"c", "n", "new"},
|
||||||
Short: "Create a new zettel",
|
Short: "Create a new zettel",
|
||||||
Long: `zet create|new|c|n TITLE`,
|
Long: `zet create|new|c|n TITLE`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cli *cobra.Command, args []string) {
|
||||||
title := ""
|
title := ""
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
|
@ -22,3 +22,7 @@ var createCmd = &cobra.Command{
|
||||||
zet.CreateZet(title)
|
zet.CreateZet(title)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(createCmd)
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package cmd
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -19,7 +19,7 @@ var editCmd = &cobra.Command{
|
||||||
Long: `zet edit|e ID
|
Long: `zet edit|e ID
|
||||||
zet edit|e latest
|
zet edit|e latest
|
||||||
`,
|
`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cli *cobra.Command, args []string) {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
fmt.Println("Error: No id provided")
|
fmt.Println("Error: No id provided")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -71,3 +71,7 @@ zet edit|e latest
|
||||||
zet.EditZet(id)
|
zet.EditZet(id)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(editCmd)
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package cmd
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -18,7 +18,7 @@ var findCmd = &cobra.Command{
|
||||||
Long: `zet find QUERY
|
Long: `zet find QUERY
|
||||||
|
|
||||||
If -j or --json is added, the results will be in JSON format.`,
|
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 {
|
if len(args) < 1 {
|
||||||
fmt.Println("No query")
|
fmt.Println("No query")
|
||||||
|
|
||||||
|
@ -44,5 +44,7 @@ If -j or --json is added, the results will be in JSON format.`,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
rootCmd.AddCommand(findCmd)
|
||||||
|
|
||||||
findCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format")
|
findCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format")
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package cmd
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -14,7 +14,7 @@ var linksCmd = &cobra.Command{
|
||||||
Aliases: []string{"l"},
|
Aliases: []string{"l"},
|
||||||
Short: "TODO",
|
Short: "TODO",
|
||||||
Long: `zet links QUERY`,
|
Long: `zet links QUERY`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cli *cobra.Command, args []string) {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
fmt.Println("No query")
|
fmt.Println("No query")
|
||||||
|
|
||||||
|
@ -37,3 +37,7 @@ var linksCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package cmd
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -17,7 +17,7 @@ var titlesCmd = &cobra.Command{
|
||||||
Long: `Print the IDs and titles of all zettels.
|
Long: `Print the IDs and titles of all zettels.
|
||||||
|
|
||||||
If -j or --json is added, the results will be in JSON format.`,
|
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()
|
zets := zet.GetAllZets()
|
||||||
|
|
||||||
result := zet.ParseZetList(zets)
|
result := zet.ParseZetList(zets)
|
||||||
|
@ -37,5 +37,7 @@ If -j or --json is added, the results will be in JSON format.`,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
rootCmd.AddCommand(titlesCmd)
|
||||||
|
|
||||||
titlesCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format")
|
titlesCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format")
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package cmd
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -19,7 +19,7 @@ var viewCmd = &cobra.Command{
|
||||||
Long: `zet view|v ID
|
Long: `zet view|v ID
|
||||||
zet view|v latest
|
zet view|v latest
|
||||||
`,
|
`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cli *cobra.Command, args []string) {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
fmt.Println("Error: No id provided")
|
fmt.Println("Error: No id provided")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -71,3 +71,7 @@ zet view|v latest
|
||||||
fmt.Println(zet.ViewZet(id))
|
fmt.Println(zet.ViewZet(id))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(viewCmd)
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue