Skip to content

Commit

Permalink
Snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
seanwash committed May 8, 2024
1 parent c051fc8 commit d38fd1c
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ go.work

.DS_Store
install.sh
bin/
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: run install

run:
go run cmd/jn/main.go

clean:
rm -rf bin

install:
go build -o bin/jn cmd/jn/main.go
mv bin/jn $$GOPATH/bin/jn
57 changes: 57 additions & 0 deletions cmd/jn/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"github.com/seanwash/jn/internal"
"log"
"os"
"os/exec"
)

func main() {
args := os.Args[1:]
config := internal.NewConfig()

if len(args) > 0 && args[0] == "journal" {
cmd := exec.Command("open", config.RootPath)
runCmd(cmd)
return
}

openEntryCmd := exec.Command("open", config.EntryPath)
entryExists, _ := exists(config.EntryPath)

if entryExists {
runCmd(openEntryCmd)
} else {
createEntry(config.FolderPath, config.EntryPath)
runCmd(openEntryCmd)
}
}

func createEntry(folderPath string, entryPath string) {
err := os.MkdirAll(folderPath, 0700)
if err != nil {
log.Fatal("Could not create new folder", err)
}

_, err = os.Create(entryPath)
if err != nil {
log.Fatal("Could not create new entry", err)
}
}

func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err != nil {
return false, err
}

return true, nil
}

func runCmd(cmd *exec.Cmd) {
err := cmd.Run()
if err != nil {
log.Fatal("Could not run cmd: ", err)
}
}
43 changes: 43 additions & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package internal

import (
"fmt"
"log"
"os"
"path"
"time"
)

type Config struct {
FileExtension string
HomePath string
RootPath string
FolderPath string
EntryPath string
}

func NewConfig() Config {
fileExtension := ".md"
today := time.Now()
homePath := getHomeDir()
rootPath := path.Join(homePath, "journal")
folderPath := path.Join(rootPath, fmt.Sprintf("%d", today.Year()), fmt.Sprintf("%02d", today.Month()))
entryName := fmt.Sprintf("%02d%s", today.Day(), fileExtension)

return Config{
FileExtension: fileExtension,
HomePath: homePath,
RootPath: rootPath,
FolderPath: folderPath,
EntryPath: path.Join(folderPath, entryName),
}
}

func getHomeDir() string {
homePath, err := os.UserHomeDir()
if err != nil {
log.Fatal("Could not get user's home dir: ", err)
}

return homePath
}
66 changes: 0 additions & 66 deletions main.go

This file was deleted.

0 comments on commit d38fd1c

Please sign in to comment.