-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ go.work | |
|
||
.DS_Store | ||
install.sh | ||
bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |