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