...

Source file src/golang.conradwood.net/go-easyops/cmdline/compile_path.go

Documentation: golang.conradwood.net/go-easyops/cmdline

     1  package cmdline
     2  
     3  import (
     4  	"runtime"
     5  	"strings"
     6  )
     7  
     8  // returns the sourceode path from which main() was compiled into this binary
     9  func SourceCodePath() string {
    10  	pc := make([]uintptr, 128)
    11  	num := runtime.Callers(0, pc)
    12  	if num == 0 {
    13  		return "[no caller]"
    14  	}
    15  	pc = pc[:num] // pass only valid pcs to runtime.CallersFrames
    16  	frames := runtime.CallersFrames(pc)
    17  	res := "[unidentifiable caller]"
    18  	more := true
    19  	var frame runtime.Frame
    20  	for {
    21  		// Check whether there are more frames to process after this one.
    22  		if !more {
    23  			break
    24  		}
    25  		frame, more = frames.Next()
    26  		// Process this frame.
    27  		//
    28  		// To keep this example's output stable
    29  		// even if there are changes in the testing package,
    30  		// stop unwinding when we leave package runtime.
    31  		if strings.Contains(frame.File, "runtime/") {
    32  			continue
    33  		}
    34  		if frame.Function != "main.main" {
    35  			continue
    36  		}
    37  		//		fmt.Printf("Frame function: %s in %s\n", frame.Function, frame.File)
    38  		if strings.Contains(frame.Function, "go-easyops") {
    39  			continue
    40  		}
    41  		if strings.Contains(frame.File, "/opt/yacloud") { //internal go stuff
    42  			continue
    43  		}
    44  		res = frame.File
    45  		break
    46  		//		fmt.Printf("- more:%v | %s\n", more, frame.Function)
    47  
    48  	}
    49  	return res
    50  }
    51  

View as plain text