Skip to content

Commit

Permalink
Merge branch 'main' into pending-changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaen committed Jul 16, 2024
2 parents 3b04cb5 + 4e844a7 commit bf077f9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
30 changes: 23 additions & 7 deletions klog/app/cli/bookmarks.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cli

import (
"strings"

"github.com/jotaen/klog/klog/app"
"github.com/jotaen/klog/klog/app/cli/util"
"strings"
)

type Bookmarks struct {
Expand All @@ -24,7 +25,7 @@ type Bookmarks struct {
func (opt *Bookmarks) Help() string {
return `
Bookmarks allow you to interact with often-used files via an alias, independent of your current working directory.
You can think of a bookmark as some sort of klog-specific symlink, that’s always available when you invoke klog, and that resolves to the designated file.
You can think of a bookmark as some sort of klog-specific symlink, that’s always available when you invoke klog, and that resolves to the designated target file.
Use the subcommands below to set up and manage your bookmarks.
A bookmark name is denoted by the prefix '@'. For example, if you have a bookmark named '@work', that points to a .klg file, you can use klog like this:
Expand All @@ -38,6 +39,8 @@ This is useful in case you only have one main file at a time, and allows you to
klog total
klog start --summary 'Started new project'
When creating a bookmark, you can also create the respective target file by using the '--create' flag.
`
}

Expand Down Expand Up @@ -89,9 +92,10 @@ func (opt *BookmarksInfo) Run(ctx app.Context) error {
}

type BookmarksSet struct {
File string `arg:"" type:"string" predictor:"file" help:".klg source file"`
Name string `arg:"" name:"bookmark" type:"string" optional:"1" help:"The name of the bookmark."`
Force bool `name:"force" help:"Force to set, even if target file does not exist or is invalid"`
File string `arg:"" type:"string" predictor:"file" help:".klg target file"`
Name string `arg:"" name:"bookmark" type:"string" optional:"1" help:"The name of the bookmark."`
Create bool `name:"create" short:"c" help:"Create the target file"`
Force bool `name:"force" help:"Force to set, even if target file does not exist or is invalid"`
util.QuietArgs
}

Expand All @@ -100,6 +104,14 @@ func (opt *BookmarksSet) Run(ctx app.Context) error {
if err != nil {
return err
}

if opt.Create {
cErr := app.CreateEmptyFile(file)
if cErr != nil {
return cErr
}
}

if !opt.Force {
_, rErr := ctx.ReadInputs(app.FileOrBookmarkName(file.Path()))
if rErr != nil {
Expand Down Expand Up @@ -128,10 +140,14 @@ func (opt *BookmarksSet) Run(ctx app.Context) error {
}
if !opt.Quiet {
if didBookmarkAlreadyExist {
ctx.Print("Changed bookmark:\n")
ctx.Print("Changed bookmark")
} else {
ctx.Print("Created new bookmark:\n")
ctx.Print("Created new bookmark")
}
if opt.Create {
ctx.Print(" and created target file")
}
ctx.Print(":\n")
}
ctx.Print(bookmark.Name().ValuePretty() + " -> " + bookmark.Target().Path() + "\n")
return nil
Expand Down
25 changes: 25 additions & 0 deletions klog/app/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,31 @@ func WriteToFile(target File, contents string) Error {
return nil
}

// CreateEmptyFile creates a new file on disk.
// It returns an error if the file already exists, or if the file cannot be
// created.
func CreateEmptyFile(file File) Error {
if _, sErr := os.Stat(file.Path()); !os.IsNotExist(sErr) {
return NewErrorWithCode(
GENERAL_ERROR,
"Cannot create file",
"There is already a file at that location",
sErr,
)
}
// Note: `os.Create` would truncate the file if it already exists.
_, cErr := os.Create(file.Path())
if cErr != nil {
return NewErrorWithCode(
GENERAL_ERROR,
"Cannot create file",
"Please check the file name and permissions",
cErr,
)
}
return nil
}

// ReadStdin reads the entire input from stdin and returns it as string.
// It returns an error if stdin cannot be accessed, or if reading from it fails.
func ReadStdin() (string, Error) {
Expand Down
17 changes: 16 additions & 1 deletion klog/app/main/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestPrintAppErrors(t *testing.T) {
assert.True(t, strings.Contains(out[2], "There is already an open range in this record"), out)
}

func TestBookmarkFile(t *testing.T) {
func TestConfigureAndUseBookmark(t *testing.T) {
klog := &Env{
files: map[string]string{
"test.klg": "2020-01-01\nSome stuff\n\t1h7m\n",
Expand All @@ -71,6 +71,21 @@ func TestBookmarkFile(t *testing.T) {
assert.True(t, strings.Contains(out[3], "1h7m"), out)
}

func TestCreateBookmarkTargetFileOnDemand(t *testing.T) {
klog := &Env{}
out := klog.run(
[]string{"bookmarks", "set", "--create", "test.klg", "tst"},
[]string{"bookmarks", "set", "--create", "test.klg", "tst"},
)
// Out 0 like: `Created new bookmark`, `@tst -> /tmp/.../test.klg`
assert.True(t, strings.Contains(out[0], "Created new bookmark and created target file:"), out)
assert.True(t, strings.Contains(out[0], "@tst"), out)
assert.True(t, strings.Contains(out[0], "test.klg"), out)
// Out 1 like: `Error: Cannot create file`, `There is already a file at that location`
assert.True(t, strings.Contains(out[1], "Error: Cannot create file"), out)
assert.True(t, strings.Contains(out[1], "There is already a file at that location"), out)
}

func TestWriteToFile(t *testing.T) {
klog := &Env{
files: map[string]string{
Expand Down

0 comments on commit bf077f9

Please sign in to comment.