github.com/ChicK00o/awgo@v0.29.4/util/examples_test.go (about) 1 // Copyright (c) 2018 Dean Jackson <deanishe@deanishe.net> 2 // MIT Licence - http://opensource.org/licenses/MIT 3 4 package util 5 6 import ( 7 "fmt" 8 "io/ioutil" 9 "os" 10 "strings" 11 "time" 12 ) 13 14 // Shorten paths by replacing user's home directory with ~ 15 func ExamplePrettyPath() { 16 paths := []string{ 17 "", 18 "$HOME", 19 "$HOME/", 20 "$HOME/Documents", 21 "/Applications", 22 } 23 24 for _, s := range paths { 25 // Expand $HOME 26 p := os.ExpandEnv(s) 27 28 fmt.Println(PrettyPath(p)) 29 } 30 // Output: 31 // 32 // ~ 33 // ~/ 34 // ~/Documents 35 // /Applications 36 } 37 38 func ExamplePadLeft() { 39 fmt.Println(PadLeft("wow", "-", 5)) 40 // Output: --wow 41 } 42 43 func ExamplePadRight() { 44 fmt.Println(PadRight("wow", "-", 5)) 45 // Output: wow-- 46 } 47 48 func ExamplePad() { 49 fmt.Println(Pad("wow", "-", 10)) 50 // Output: ---wow---- 51 } 52 53 func ExamplePathExists() { 54 name := "my-test-file.txt" 55 56 // Non-existent file 57 fmt.Println(PathExists(name)) 58 59 // Create the file 60 if err := ioutil.WriteFile(name, []byte("test"), 0600); err != nil { 61 panic(err) 62 } 63 64 // Now it exists 65 fmt.Println(PathExists(name)) 66 // Output: 67 // false 68 // true 69 70 if err := os.Remove(name); err != nil { 71 panic(err) 72 } 73 } 74 75 // QuoteAS wraps the string in quotes and escapes quotes within the string. 76 func ExampleQuoteAS() { 77 values := []string{ 78 "", 79 "simple", 80 "with spaces", 81 `has "quotes" within`, 82 `"within quotes"`, 83 `"`, 84 } 85 86 // Quote values for insertion into AppleScript 87 for _, s := range values { 88 fmt.Println(QuoteAS(s)) 89 } 90 // Output: 91 // "" 92 // "simple" 93 // "with spaces" 94 // "has " & quote & "quotes" & quote & " within" 95 // quote & "within quotes" & quote 96 // quote 97 } 98 99 func ExampleRunAS() { 100 // Some test words 101 data := []string{ 102 "Hello, AppleScript!", 103 `"Just Do It!"`, 104 `He said, "I'm fine!" then died :(`, 105 `"`, 106 } 107 108 for _, input := range data { 109 // Simple script to return input 110 // QuoteAS adds quotation marks, so don't add any more 111 quoted := QuoteAS(input) 112 script := "return " + quoted 113 114 // Run script and collect result 115 output, err := RunAS(script) 116 if err != nil { 117 // handle error 118 } 119 120 fmt.Printf("> %s\n", input) 121 fmt.Printf("< %s\n", output) 122 } 123 124 // Output: 125 // > Hello, AppleScript! 126 // < Hello, AppleScript! 127 // > "Just Do It!" 128 // < "Just Do It!" 129 // > He said, "I'm fine!" then died :( 130 // < He said, "I'm fine!" then died :( 131 // > " 132 // < " 133 } 134 135 // You can pass additional arguments to your scripts. 136 func ExampleRunJS_arguments() { 137 // Some test values 138 argv := []string{"angular", "react", "vue"} 139 140 script := `function run(argv) { return argv.join('\n') }` 141 output, err := RunJS(script, argv...) 142 if err != nil { 143 // handle error 144 } 145 146 fmt.Println(output) 147 148 // Output: 149 // angular 150 // react 151 // vue 152 } 153 154 // Run calls any executable file. It does *not* use $PATH to find commands. 155 func ExampleRun() { 156 // Create a simple test script 157 filename := "test-script" 158 script := `#!/bin/bash 159 echo -n Happy Hour 160 ` 161 162 // Make sure script is executable! 163 if err := ioutil.WriteFile(filename, []byte(script), 0700); err != nil { 164 panic(err) 165 } 166 167 // Note: we're running "test-script", but Run looks for "./test-script", 168 // not a command "test-script" on your $PATH. 169 out, err := Run(filename) 170 if err != nil { 171 panic(err) 172 } 173 174 fmt.Println(string(out)) 175 176 // Output: 177 // Happy Hour 178 179 if err := os.Remove(filename); err != nil { 180 panic(err) 181 } 182 } 183 184 // You can pass arguments to the program/script you run. 185 func ExampleRun_arguments() { 186 // Run an executable with arguments 187 out, err := Run("/bin/bash", "-c", "echo -n Stringfellow Hawke") 188 if err != nil { 189 panic(err) 190 } 191 192 fmt.Println(string(out)) 193 194 // Output: 195 // Stringfellow Hawke 196 } 197 198 // Run recognises certain kinds of script files and knows which 199 // interpreter to run them with. 200 func ExampleRun_scripts() { 201 // Test scripts that output $1. 202 // Run will run them based on their file extension. 203 scripts := []struct { 204 name, code string 205 }{ 206 {"test-file.py", "import sys; print(sys.argv[1])"}, 207 {"test-file.txt", "ignored"}, // invalid 208 {"test-file.sh", `echo "$1"`}, 209 {"test-file.scpt", "on run(argv)\nreturn first item of argv\nend run"}, 210 {"test-file.doc", "irrelevant"}, // invalid 211 } 212 213 // Create test scripts. Note: they aren't executable. 214 for _, script := range scripts { 215 if err := ioutil.WriteFile(script.name, []byte(script.code), 0600); err != nil { 216 panic(err) 217 } 218 } 219 220 // Run scripts 221 for _, script := range scripts { 222 // Run runs file based on file extension 223 // Pass script's own name as $1 224 data, err := Run(script.name, script.name) 225 if err != nil { 226 // We're expecting 2 unknown types 227 if err == ErrUnknownFileType { 228 fmt.Printf("[err] %s: %s\n", err, script.name) 229 continue 230 } 231 232 // Oops :( 233 panic(err) 234 } 235 236 // Script's own name 237 str := strings.TrimSpace(string(data)) 238 fmt.Println(str) 239 } 240 241 // Output: 242 // test-file.py 243 // [err] unknown filetype: test-file.txt 244 // test-file.sh 245 // test-file.scpt 246 // [err] unknown filetype: test-file.doc 247 248 for _, script := range scripts { 249 if err := os.Remove(script.name); err != nil { 250 panic(err) 251 } 252 } 253 } 254 255 // Timed logs the execution duration of a function with a message. 256 // Call with defer and time.Now(). 257 func ExampleTimed() { 258 doThing := func() { 259 // 260 defer Timed(time.Now(), "long-running thing") 261 fmt.Printf("doing long-running thing ...") 262 // simulate work 263 time.Sleep(time.Second) 264 } 265 266 // Call function. NOTE: the output from deferred functions is not 267 // captured. 268 doThing() 269 // Output: 270 // doing long-running thing ... 271 }