github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/util/path.go (about)

     1  package util
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  )
     7  
     8  var (
     9  	_ExeDir  string
    10  	_ExeName string
    11  )
    12  
    13  // WorkDir 程序执行目录
    14  func WorkDir() string {
    15  	if _ExeDir != "" {
    16  		return _ExeDir
    17  	}
    18  	p, err := os.Executable()
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  	_ExeDir = filepath.ToSlash(filepath.Dir(p))
    23  	return _ExeDir
    24  }
    25  
    26  // SetExeDir 手动指定执行目录
    27  func SetExeDir(dir string) {
    28  	_ExeDir = dir
    29  }
    30  
    31  func ExeName() string {
    32  	if _ExeName != "" {
    33  		return _ExeName
    34  	}
    35  	executable, _ := os.Executable()
    36  	ext := filepath.Ext(executable)
    37  	_ExeName = filepath.Base(executable[:len(executable)-len(ext)])
    38  	return _ExeName
    39  }