Last active
March 14, 2022 01:28
-
-
Save gurjeet/3121b2e373e61873c5a6e96d72b6c1c6 to your computer and use it in GitHub Desktop.
GoLang Scripting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//usr/bin/env true; go run `dirname $0`/*.go "$@"; exit $? | |
package main | |
import ( | |
"errors" | |
"os" | |
) | |
// TODO: catch Panics and emit a clear message | |
func main() { os.Exit(mainWithExitCode()) } | |
func mainWithExitCode() int { | |
var e error | |
var r = "resource" | |
var command string | |
initLogging() | |
switch command = getArg(1); command { | |
case "create": | |
command = "creating" | |
case "destroy": | |
command = "destroying" | |
case "no-op": | |
default: | |
err("Unknown subcommand provided") | |
} | |
info(command + " " + r) | |
e = errors.New("Not implemented") | |
err(e) | |
return 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "os" | |
func isErr(e error) bool { return e != nil } | |
func getArg(n int) string { | |
if len(os.Args) >= n+1 { | |
return os.Args[n] | |
} | |
return "" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"os" | |
"strconv" | |
) | |
func initEnv() { | |
// Use our version of Kind binary to manage clusters | |
os.Setenv("PATH", "./kind/bin:"+os.Getenv("PATH")) | |
} | |
func initLogging() { | |
var name, val string | |
var e error | |
name = "DEBUG" | |
if val = os.Getenv(name); val != "" { | |
// Ignore error in parsing; we don't want to halt the app because of this | |
if enableDebug, e = strconv.ParseBool(val); isErr(e) { | |
enableDebug = enableDebugDefault | |
warn("There was an error parsing the value of ", name, "; expected a boolean, got ", val) | |
} | |
} | |
name = "EXIT_ON_ERROR" | |
if val = os.Getenv(name); val != "" { | |
// Ignore error in parsing; we don't want to halt the app because of this | |
if exitOnError, e = strconv.ParseBool(val); isErr(e) { | |
exitOnError = exitOnErrorDefault | |
warn("There was an error parsing the value of ", name, "; expected a boolean, got ", val) | |
} | |
} | |
} | |
func debug(msg ...interface{}) { | |
if enableDebug { | |
_log("DEBUG", msg...) | |
} | |
} | |
func info(msg ...interface{}) { | |
_log("INFO", msg...) | |
} | |
func warn(msg ...interface{}) { | |
_log("WARNING", msg...) | |
} | |
func err(msg ...interface{}) error { | |
var loggedMsg = _log("ERROR", msg...) | |
if exitOnError { | |
os.Exit(1) | |
} | |
return errors.New(loggedMsg) | |
} | |
func fatal(msg ...interface{}) { | |
_log("FATAL", msg...) | |
panic("Exiting process. Panic!!!") | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
// Supporting code follows; not to be used by code in other files | |
//////////////////////////////////////////////////////////////////////////////// | |
func _log(prefix string, message ...interface{}) string { | |
var msg = fmt.Sprint(append([]interface{}{prefix + ": "}, message...)...) | |
fmt.Println(msg) | |
return msg | |
} | |
const exitOnErrorDefault = true | |
const enableDebugDefault = true | |
// Similar to shell's `set -e` option | |
var exitOnError = exitOnErrorDefault | |
var enableDebug = enableDebugDefault |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment