Skip to content

Commit

Permalink
Better names for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Sep 17, 2017
1 parent d98b9a7 commit 5c08ba9
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion background.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ func getPid(jobName string) (int, error) {

// Path to PID file for job.
func pidFile(jobName string) string {
dir := util.EnsureExists(filepath.Join(awCacheDir(), "jobs"))
dir := util.MustExist(filepath.Join(awCacheDir(), "jobs"))
return filepath.Join(dir, jobName+".pid")
}
2 changes: 1 addition & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Cache struct {
// Directory dir is created if it doesn't exist. The function will panic
// if directory can't be created.
func NewCache(dir string) *Cache {
util.EnsureExists(dir)
util.MustExist(dir)
return &Cache{dir}
}

Expand Down
2 changes: 1 addition & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
func WithTempDir(fn func(dir string)) {
root := os.TempDir()
p := filepath.Join(root, fmt.Sprintf("awgo-%d.%d", os.Getpid(), time.Now().Nanosecond()))
util.EnsureExists(p)
util.MustExist(p)
defer os.RemoveAll(p)
fn(p)
}
Expand Down
2 changes: 1 addition & 1 deletion feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (fb *Feedback) NewItem(title string) *Item {
func (fb *Feedback) NewFileItem(path string) *Item {
t := filepath.Base(path)
it := fb.NewItem(t)
it.Subtitle(util.ShortenPath(path)).
it.Subtitle(util.PrettyPath(path)).
Arg(path).
Valid(true).
UID(path).
Expand Down
4 changes: 2 additions & 2 deletions update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (u *Updater) CheckDue() bool {
return true
}
elapsed := time.Now().Sub(u.LastCheck)
log.Printf("%s since last check for update", util.ReadableDuration(elapsed))
log.Printf("%s since last check for update", util.HumanDuration(elapsed))
return elapsed.Nanoseconds() > u.updateInterval.Nanoseconds()
}

Expand Down Expand Up @@ -215,7 +215,7 @@ func (u *Updater) Install() error {

// cachePath returns a filepath within AwGo's update cache directory.
func (u *Updater) cachePath(filename string) string {
dp := util.EnsureExists(filepath.Join(u.cacheDir, "update"))
dp := util.MustExist(filepath.Join(u.cacheDir, "update"))
return filepath.Join(dp, filename)
}

Expand Down
16 changes: 8 additions & 8 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ func FindWorkflowRoot() (string, error) {
return "", fmt.Errorf("info.plist not found")
}

// EnsureExists takes and returns a directory path, creating the directory
// MustExist takes and returns a directory path, creating the directory
// if necessary. Any created directories have permission set to 700.
func EnsureExists(dirpath string) string {
// Panics if the directory cannot be created.
func MustExist(dirpath string) string {
err := os.MkdirAll(dirpath, 0700)
if err != nil {
panic(fmt.Sprintf("Couldn't create directory `%s` : %v", dirpath, err))
Expand Down Expand Up @@ -88,8 +89,8 @@ func FindFileUpwards(filename string, startdir string) (string, error) {
return "", err
}

// ShortenPath replaces $HOME with ~ in path
func ShortenPath(path string) string {
// PrettyPath replaces $HOME with ~ in path
func PrettyPath(path string) string {
return strings.Replace(path, os.Getenv("HOME"), "~", -1)
}

Expand Down Expand Up @@ -132,9 +133,8 @@ func Pad(str, pad string, n int) string {
}
}

// ReadableDuration returns a sensibly-formatted string for
// non-benchmarking purposes.
func ReadableDuration(d time.Duration) string {
// HumanDuration returns a sensibly-formatted string for non-benchmarking purposes.
func HumanDuration(d time.Duration) string {
if d.Hours() >= 72 { // 3 days
return fmt.Sprintf("%dd", int(d.Hours()/24))
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func ClearDirectory(p string) error {
return nil
}
err := os.RemoveAll(p)
EnsureExists(p)
MustExist(p)
if err == nil {
log.Printf("deleted contents of `%s`", p)
}
Expand Down
20 changes: 10 additions & 10 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,16 @@ func ExamplePad() {
}

func ExampleReadableDuration() {
fmt.Println(ReadableDuration(time.Hour * 96))
fmt.Println(ReadableDuration(time.Hour * 48))
fmt.Println(ReadableDuration(time.Hour * 12))
fmt.Println(ReadableDuration(time.Minute * 130))
fmt.Println(ReadableDuration(time.Minute * 90))
fmt.Println(ReadableDuration(time.Second * 315))
fmt.Println(ReadableDuration(time.Second * 70))
fmt.Println(ReadableDuration(time.Second * 5))
fmt.Println(ReadableDuration(time.Millisecond * 320))
fmt.Println(ReadableDuration(time.Millisecond * 50))
fmt.Println(HumanDuration(time.Hour * 96))
fmt.Println(HumanDuration(time.Hour * 48))
fmt.Println(HumanDuration(time.Hour * 12))
fmt.Println(HumanDuration(time.Minute * 130))
fmt.Println(HumanDuration(time.Minute * 90))
fmt.Println(HumanDuration(time.Second * 315))
fmt.Println(HumanDuration(time.Second * 70))
fmt.Println(HumanDuration(time.Second * 5))
fmt.Println(HumanDuration(time.Millisecond * 320))
fmt.Println(HumanDuration(time.Millisecond * 50))
// Output: 4d
// 48h
// 12h0m
Expand Down
12 changes: 6 additions & 6 deletions workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,13 @@ func (wf *Workflow) CacheDir() string {
"$HOME/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Data/%s",
wf.BundleID()))
}
return util.EnsureExists(wf.cacheDir)
return util.MustExist(wf.cacheDir)
}

// OpenCache opens the workflow's cache directory in the default application (usually Finder).
func OpenCache() error { return wf.OpenCache() }
func (wf *Workflow) OpenCache() error {
util.EnsureExists(wf.DataDir())
util.MustExist(wf.DataDir())
cmd := exec.Command("open", wf.CacheDir())
return cmd.Run()
}
Expand All @@ -441,7 +441,7 @@ func (wf *Workflow) DataDir() string {
"$HOME/Library/Application Support/Alfred 3/Workflow Data/%s",
wf.BundleID()))
}
return util.EnsureExists(wf.dataDir)
return util.MustExist(wf.dataDir)
}

// OpenData opens the workflow's data directory in the default application (usually Finder).
Expand Down Expand Up @@ -728,13 +728,13 @@ func (wf *Workflow) outputErrorMsg(msg string) {
// awDataDir is the directory for AwGo's own data.
func awDataDir() string { return wf.awDataDir() }
func (wf *Workflow) awDataDir() string {
return util.EnsureExists(filepath.Join(wf.DataDir(), "_aw"))
return util.MustExist(filepath.Join(wf.DataDir(), "_aw"))
}

// awCacheDir is the directory for AwGo's own cache.
func awCacheDir() string { return wf.awCacheDir() }
func (wf *Workflow) awCacheDir() string {
return util.EnsureExists(filepath.Join(wf.CacheDir(), "_aw"))
return util.MustExist(filepath.Join(wf.CacheDir(), "_aw"))
}

// --------------------------------------------------------------------
Expand All @@ -743,7 +743,7 @@ func (wf *Workflow) awCacheDir() string {
// finishLog outputs the workflow duration
func finishLog(fatal bool) {
elapsed := time.Now().Sub(startTime)
s := util.Pad(fmt.Sprintf(" %s ", util.ReadableDuration(elapsed)), "-", 50)
s := util.Pad(fmt.Sprintf(" %s ", util.HumanDuration(elapsed)), "-", 50)
if fatal {
log.Fatalln(s)
} else {
Expand Down

0 comments on commit 5c08ba9

Please sign in to comment.