Skip to content

Commit

Permalink
Use an alias to create notes in alternate book directory
Browse files Browse the repository at this point in the history
Instead of always creating notes in the primary notebook directory,
allow the 'new' command to be given an alias to lookup the directory
for in the configuration. If a match is found, the note will be created
in alternate directory.
  • Loading branch information
msp301 committed Aug 11, 2023
1 parent d1e7959 commit 382a355
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/spf13/viper"
"log"
"os"
"path/filepath"
Expand All @@ -14,10 +15,21 @@ import (
var newCmd = &cobra.Command{
Use: "new",
Short: "Create a new note",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
dirs := bookDirs()
bookDir := dirs[0]

notePath := filepath.Join(dirs[0], strconv.Itoa(time.Now().Year()))
if len(args) > 0 {
bookAlias := args[0]

aliasIndex := aliasIndex(bookAlias)
if aliasIndex != -1 {
bookDir = dirs[aliasIndex]
}
}

notePath := filepath.Join(bookDir, strconv.Itoa(time.Now().Year()))
if err := os.MkdirAll(notePath, 0755); err != nil {
log.Fatalf("Error creating directory: %s", err)
}
Expand All @@ -37,6 +49,9 @@ var newCmd = &cobra.Command{
}

func init() {
newCmd.PersistentFlags().StringSlice("alias", []string{}, "aliases for notebook directories")
viper.BindPFlag("alias", newCmd.PersistentFlags().Lookup("alias"))

rootCmd.AddCommand(newCmd)
}

Expand All @@ -61,3 +76,12 @@ func createNote(dir string) (*os.File, error) {

return note, nil
}

func aliasIndex(alias string) int {
for i, a := range viper.GetStringSlice("alias") {
if a == alias {
return i
}
}
return -1
}

0 comments on commit 382a355

Please sign in to comment.