git.deanishe.net/deanishe/awgo.git@v0.15.0/workflow_paths.go (about) 1 // 2 // Copyright (c) 2018 Dean Jackson <deanishe@deanishe.net> 3 // 4 // MIT Licence. See http://opensource.org/licenses/MIT 5 // 6 // Created on 2018-02-09 7 // 8 9 package aw 10 11 import ( 12 "errors" 13 "fmt" 14 "log" 15 "os" 16 "os/exec" 17 "path/filepath" 18 "strings" 19 20 "github.com/deanishe/awgo/util" 21 ) 22 23 // Dir returns the path to the workflow's root directory. 24 func (wf *Workflow) Dir() string { 25 26 if wf.dir == "" { 27 wf.dir = findWorkflowRoot() 28 } 29 30 return wf.dir 31 } 32 33 // CacheDir returns the path to the workflow's cache directory. 34 func (wf *Workflow) CacheDir() string { 35 36 if wf.cacheDir == "" { 37 wf.cacheDir = wf.Config.Get(EnvVarCacheDir) 38 } 39 40 return wf.cacheDir 41 } 42 43 // OpenCache opens the workflow's cache directory in the default application (usually Finder). 44 func (wf *Workflow) OpenCache() error { 45 return exec.Command("open", wf.CacheDir()).Run() 46 } 47 48 // ClearCache deletes all files from the workflow's cache directory. 49 func (wf *Workflow) ClearCache() error { 50 return util.ClearDirectory(wf.CacheDir()) 51 } 52 53 // DataDir returns the path to the workflow's data directory. 54 func (wf *Workflow) DataDir() string { 55 56 if wf.dataDir == "" { 57 wf.dataDir = wf.Config.Get(EnvVarDataDir) 58 } 59 60 return wf.dataDir 61 } 62 63 // OpenData opens the workflow's data directory in the default application (usually Finder). 64 func (wf *Workflow) OpenData() error { 65 return exec.Command("open", wf.DataDir()).Run() 66 } 67 68 // ClearData deletes all files from the workflow's cache directory. 69 func (wf *Workflow) ClearData() error { 70 return util.ClearDirectory(wf.DataDir()) 71 } 72 73 // Reset deletes all workflow data (cache and data directories). 74 func (wf *Workflow) Reset() error { 75 errs := []error{} 76 if err := wf.ClearCache(); err != nil { 77 errs = append(errs, err) 78 } 79 if err := wf.ClearData(); err != nil { 80 errs = append(errs, err) 81 } 82 if len(errs) > 0 { 83 return errs[0] 84 } 85 return nil 86 } 87 88 // LogFile returns the path to the workflow's log file. 89 func (wf *Workflow) LogFile() string { 90 return filepath.Join(wf.CacheDir(), fmt.Sprintf("%s.log", wf.BundleID())) 91 } 92 93 // OpenLog opens the workflow's logfile in the default application (usually Console.app). 94 func (wf *Workflow) OpenLog() error { 95 if !util.PathExists(wf.LogFile()) { 96 log.Println("Creating log file...") 97 } 98 return exec.Command("open", wf.LogFile()).Run() 99 } 100 101 // OpenHelp opens the workflow's help URL (if set) in the default browser. 102 func (wf *Workflow) OpenHelp() error { 103 if wf.helpURL == "" { 104 return errors.New("Help URL is not set") 105 } 106 return exec.Command("open", wf.helpURL).Run() 107 } 108 109 // Try to find workflow root based on presence of info.plist. 110 func findWorkflowRoot() string { 111 112 var ( 113 dirs = []string{} // directories to look in for info.plist 114 seen = map[string]bool{} // avoid duplicates in dirs 115 ) 116 117 // Add path and all its parents to dirs & seen 118 queueTree := func(p string) { 119 120 p = filepath.Clean(p) 121 122 segs := strings.Split(p, "/") 123 124 for i := len(segs) - 1; i > 0; i-- { 125 126 p := strings.Join(segs[0:i], "/") 127 128 if p == "" { 129 p = "/" 130 } 131 132 if !seen[p] { 133 134 seen[p] = true 135 dirs = append(dirs, p) 136 } 137 } 138 } 139 140 // Working directory is the best place to start looking 141 wd, err := os.Getwd() 142 if err != nil { 143 panic(err) 144 } 145 146 // Add all paths from working directory upwards and from 147 // directory executable is in upwards. 148 queueTree(wd) 149 queueTree(filepath.Dir(os.Args[0])) 150 151 // Return path of first directory that contains an info.plist 152 for _, dir := range dirs { 153 154 p := filepath.Join(dir, "info.plist") 155 156 if _, err := os.Stat(p); err == nil { 157 return dir 158 } 159 } 160 161 log.Printf("[warning] info.plist not found. Guessed workflow directory: %s", wd) 162 // Fall back to working directory 163 return wd 164 }