forked from deanishe/awgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow_paths.go
145 lines (120 loc) · 3.37 KB
/
workflow_paths.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) 2018 Dean Jackson <[email protected]>
// MIT Licence - http://opensource.org/licenses/MIT
package aw
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/deanishe/awgo/util"
)
// Dir returns the path to the workflow's root directory.
func (wf *Workflow) Dir() string {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
if wf.dir == "" {
wf.dir = findWorkflowRoot(wd)
}
return wf.dir
}
// CacheDir returns the path to the workflow's cache directory.
func (wf *Workflow) CacheDir() string {
if wf.cacheDir == "" {
wf.cacheDir = wf.Config.Get(EnvVarCacheDir)
}
return wf.cacheDir
}
// OpenCache opens the workflow's cache directory in the default application (usually Finder).
func (wf *Workflow) OpenCache() error {
return wf.execFunc("open", wf.CacheDir())
}
// ClearCache deletes all files from the workflow's cache directory.
func (wf *Workflow) ClearCache() error {
return util.ClearDirectory(wf.CacheDir())
}
// DataDir returns the path to the workflow's data directory.
func (wf *Workflow) DataDir() string {
if wf.dataDir == "" {
wf.dataDir = wf.Config.Get(EnvVarDataDir)
}
return wf.dataDir
}
// OpenData opens the workflow's data directory in the default application (usually Finder).
func (wf *Workflow) OpenData() error {
return wf.execFunc("open", wf.DataDir())
}
// ClearData deletes all files from the workflow's data directory.
func (wf *Workflow) ClearData() error {
return util.ClearDirectory(wf.DataDir())
}
// Reset deletes all workflow data (cache and data directories).
func (wf *Workflow) Reset() error {
errs := []error{}
if err := wf.ClearCache(); err != nil {
errs = append(errs, err)
}
if err := wf.ClearData(); err != nil {
errs = append(errs, err)
}
if len(errs) > 0 {
return errs[0]
}
return nil
}
// LogFile returns the path to the workflow's log file.
func (wf *Workflow) LogFile() string {
return filepath.Join(wf.CacheDir(), fmt.Sprintf("%s.log", wf.BundleID()))
}
// OpenLog opens the workflow's logfile in the default application (usually Console.app).
func (wf *Workflow) OpenLog() error {
if !util.PathExists(wf.LogFile()) {
log.Println("Creating log file...")
}
return wf.execFunc("open", wf.LogFile())
}
// OpenHelp opens the workflow's help URL (if set) in the default browser.
func (wf *Workflow) OpenHelp() error {
if wf.helpURL == "" {
return errors.New("Help URL is not set")
}
return wf.execFunc("open", wf.helpURL)
}
// Try to find workflow root based on presence of info.plist.
func findWorkflowRoot(path string) string {
var (
dirs []string // directories to look in for info.plist
seen = map[string]bool{} // avoid duplicates in dirs
)
// Add path and all its parents to dirs & seen
queueTree := func(p string) {
p = filepath.Clean(p)
segs := strings.Split(p, "/")
for i := len(segs) - 1; i > 0; i-- {
p := strings.Join(segs[0:i], "/")
if p == "" {
p = "/"
}
if !seen[p] {
seen[p] = true
dirs = append(dirs, p)
}
}
}
// Add all paths from path upwards and from
// directory executable is in upwards.
queueTree(path)
queueTree(filepath.Dir(os.Args[0]))
// Return path of first directory that contains an info.plist
for _, dir := range dirs {
p := filepath.Join(dir, "info.plist")
if _, err := os.Stat(p); err == nil {
return dir
}
}
log.Printf("[warning] info.plist not found. Guessed: %s", path)
return path
}