~dvshkn/planfile

ref: 34457ff3cd2b94d8b4d2b1b4ae72208eb4609a5a planfile/main.go -rw-r--r-- 2.2 KiB
34457ff3David Knight add new "latest" command 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main

import (
	"fmt"
	"git.sr.ht/~dvshkn/planfile/utils"
	"os"
	"time"
)

func main() {
	if len(os.Args) == 1 {
		// "today" is the default
		today()
	} else {
		// TODO: do more formal command/flag parsing
		switch os.Args[1] {
		case "today":
			today()
		case "latest":
			latest()
		case "help":
			help()
		default:
			// for now this covers the expected "-h" and "--help"
			help()
		}
	}
}

//
// This opens today's planfile. If necessary, it will migrate up a previous
// planfile or create a new one from scratch.
//
func today() {
	// planfile name for today that may or may not exist
	todayPlanfileName := utils.GetPlanfileName(time.Now())

	// name of most recent planfile in current directory, also may not exist
	latestPlanfileName := utils.FindLatestPlanfile()

	var fileErr error	
	if len(latestPlanfileName) == 0 {
		// create new empty planfile
		fmt.Println("no planfiles in current directory, creating new empty planfile")
		fileErr = utils.CreateEmpty(todayPlanfileName)
	} else if latestPlanfileName != todayPlanfileName {
		// latest is in the past, copy to new planfile
		fmt.Println("no planfile found for today, creating planfile from", latestPlanfileName)
		fileErr = utils.CreateFrom(todayPlanfileName, latestPlanfileName)
	} else {
		// today's planfile already exists
		fmt.Println("using today's existing planfile")
	}

	if fileErr != nil {
		fmt.Println("[!]", fileErr)
	}

	editorErr := utils.RunEditor(todayPlanfileName)

	if editorErr != nil {
		fmt.Println("[!]", editorErr)
	}
}

//
// This opens the most recent planfile. It will not create a new planfile.
//
func latest() {
	latestPlanfileName := utils.FindLatestPlanfile()

	if len(latestPlanfileName) > 0 {
		// a planfile exists, open it in the editor
		fmt.Println("found planfile", latestPlanfileName)
		editorErr := utils.RunEditor(latestPlanfileName)
		if editorErr != nil {
			fmt.Println("[!]", editorErr)
		}
	} else {
		// no suitable planfile
		fmt.Println("no recent planfiles in current directory")
	}
}

func help() {
	fmt.Println("\nUsage: planfile COMMAND")
	fmt.Println("\nCommands:")
	fmt.Println("  latest        Open the most recent planfile if it exists")
	fmt.Println("  today         Open or create today's planfile")
}