github.com/maurerlabs/github-actions/toolkit@v0.0.0-20191127192943-9d064d66f7de/core.go (about) 1 package toolkit 2 3 import ( 4 "fmt" 5 "os" 6 "runtime" 7 "strings" 8 "time" 9 ) 10 11 type InputOptions struct { 12 Required bool 13 Fallback string 14 } 15 16 func command(val string, args ...interface{}) { 17 fmt.Println(fmt.Sprintf(val, args...)) 18 } 19 20 func ExportVariable(key, val string) { 21 key = strings.TrimSpace(key) 22 23 os.Setenv(key, val) 24 command("::set-env name=%s::%s", key, val) 25 } 26 27 func SetSecret(val string) { 28 command("::add-mask::%s", val) 29 } 30 31 func AddPath(val string) { 32 os.Setenv("PATH", fmt.Sprintf("%s%c%s", val, os.PathListSeparator, os.Getenv("PATH"))) 33 command("::add-path::%s", val) 34 } 35 36 func GetInput(key string, options ...*InputOptions) string { 37 key = strings.ReplaceAll(strings.TrimSpace(key), " ", "_") 38 envVar := "INPUT_" + strings.ToUpper(key) 39 40 if val, ok := os.LookupEnv(envVar); ok { 41 return val 42 } 43 44 if len(options) > 0 && options[0].Required { 45 panic(fmt.Sprintf("Missing input value: %s", key)) 46 } 47 48 if len(options) > 0 && options[0].Fallback != "" { 49 return options[0].Fallback 50 } 51 52 return "" 53 } 54 55 func SetOutput(key, val string) { 56 command("::set-output name=%s::%s", key, val) 57 } 58 59 func SetFailed(msg string) { 60 Error(msg) 61 os.Exit(1) 62 } 63 64 func Info(msg string) { 65 command("%s", msg) 66 } 67 68 func Debug(msg string) { 69 if _, file, line, ok := runtime.Caller(1); ok { 70 command("::debug file=%s,line=%d::%s", file, line, msg) 71 } else { 72 command("::debug ::%s", msg) 73 } 74 } 75 76 func Warning(msg string) { 77 if _, file, line, ok := runtime.Caller(1); ok { 78 command("::warning file=%s,line=%d::%s", file, line, msg) 79 } else { 80 command("::warning ::%s", msg) 81 } 82 } 83 84 func Error(msg string) { 85 if _, file, line, ok := runtime.Caller(1); ok { 86 command("::error file=%s,line=%d::%s", file, line, msg) 87 } else { 88 command("::error ::%s", msg) 89 } 90 } 91 92 func Pause() func() { 93 id := time.Now().UnixNano() 94 command("::stop-commands::%d", id) 95 96 return func() { 97 command("::%d::", id) 98 } 99 } 100 101 func StartGroup(key string) { 102 command("::group ::%s", key) 103 } 104 105 func EndGroup() { 106 command("::endgroup") 107 } 108 109 func SaveState(key, val string) { 110 command("::save-state name=%s::%s", strings.TrimSpace(key), val) 111 } 112 113 func GetState(key string, options ...*InputOptions) string { 114 envVar := "STATE_" + strings.TrimSpace(key) 115 116 if val, ok := os.LookupEnv(envVar); ok { 117 return val 118 } 119 120 if len(options) > 0 && options[0].Required { 121 panic(fmt.Sprintf("Missing state value: %s", key)) 122 } 123 124 if len(options) > 0 && options[0].Fallback != "" { 125 return options[0].Fallback 126 } 127 128 return "" 129 }