浏览代码

utils: Added SelfPath(), which figures out the current (absolute) path of the running binary

Andrea Luzzardi 12 年之前
父节点
当前提交
e6adfa2bc6
共有 1 个文件被更改,包括 15 次插入0 次删除
  1. 15 0
      utils.go

+ 15 - 0
utils.go

@@ -4,7 +4,9 @@ import (
 	"bytes"
 	"container/list"
 	"io"
+	"os"
 	"os/exec"
+	"path/filepath"
 	"sync"
 )
 
@@ -34,6 +36,19 @@ func Tar(path string) (io.Reader, error) {
 	return output, nil
 }
 
+// Figure out the absolute path of our own binary
+func SelfPath() string {
+	path, err := exec.LookPath(os.Args[0])
+	if err != nil {
+		panic(err)
+	}
+	path, err = filepath.Abs(path)
+	if err != nil {
+		panic(err)
+	}
+	return path
+}
+
 type nopWriteCloser struct {
 	io.Writer
 }