Skip to content

Commit

Permalink
all show all opt
Browse files Browse the repository at this point in the history
Signed-off-by: Doug Davis <[email protected]>
  • Loading branch information
Doug Davis committed Jul 30, 2021
1 parent 9c82857 commit e53c5b5
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions helloworld/helloworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func Curl(url string) (string, error) {
var GlobalDebug = (os.Getenv("DEBUG") != "")
var envs = []string{}
var msg = ""
var skips = []string{
"KUBERNETES_",
"K_CONFIG",
"K_SERVICE",
}

func Debug(doit bool, format string, args ...interface{}) {
// If either is 'true' then print it
Expand All @@ -47,7 +52,7 @@ func Debug(doit bool, format string, args ...interface{}) {
}

// Just print an cool essage to the Writer that's passed in
func PrintMessage(w io.Writer) {
func PrintMessage(w io.Writer, showAll bool) {
fmt.Fprintf(w, "%s:\n", msg)
fmt.Fprintln(w, `. ___ __ ____ ____`)
fmt.Fprintln(w, `./ __)/ \( \( __)`)
Expand All @@ -62,6 +67,18 @@ func PrintMessage(w io.Writer) {
fmt.Fprintf(w, "Some Env Vars:\n")
fmt.Fprintf(w, "--------------\n")
for _, env := range envs {
if !showAll {
skipIt := false
for _, skip := range skips {
if strings.HasPrefix(env, skip) {
skipIt = true
break
}
}
if skipIt {
continue
}
}
fmt.Fprintf(w, "%s\n", env)
}
}
Expand All @@ -70,6 +87,7 @@ func PrintMessage(w io.Writer) {
func HandleHTTP(w http.ResponseWriter, r *http.Request) {
body := []byte{}
debug := false
showAll := (os.Getenv("SHOW") != "")

// If there's a body then read it in for later use
if r.Body != nil {
Expand All @@ -82,6 +100,11 @@ func HandleHTTP(w http.ResponseWriter, r *http.Request) {
debug = true
}

// Allow for people to enable 'showAll' on a per request basis
if _, ok := r.URL.Query()["show"]; ok {
showAll = true
}

Debug(debug, "%s:\n%s %s\nHeaders:\n%s\n\nBody:\n%s\n",
time.Now().String(), r.Method, r.URL, r.Header, string(body))

Expand Down Expand Up @@ -116,13 +139,18 @@ func HandleHTTP(w http.ResponseWriter, r *http.Request) {
if len(body) == 0 {
w.Header().Add("Content-Type", "text/plain")
// http://patorjk.com/software/taag/#p=display&f=Graceful&t=Code%0AEngine
PrintMessage(w)
PrintMessage(w, showAll)
} else {
fmt.Fprintf(w, string(body)+"\n")
}
}

func main() {
// Just for fun
if os.Getenv("SHOW") == "" {
os.Setenv("z", "Set env var 'SHOW' to see all variables")
}

// If env var CRASH is set then crash immediately.
// If its value is of the form HH:MM then crash at the specified time
// time. The time is based on time returned from: http://time.nist.gov:13
Expand Down Expand Up @@ -185,7 +213,7 @@ func main() {

fmt.Printf("Hello from helloworld! I'm a batch job! Index: %s\n\n",
jobIndex)
PrintMessage(os.Stdout)
PrintMessage(os.Stdout, os.Getenv("SHOW") == "")

} else {
// Debug the http handler for all requests
Expand Down

0 comments on commit e53c5b5

Please sign in to comment.