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