github.com/TaceyWong/ctools@v0.1.1-0.20201221084458-4d7a8190b2ac/pkg/osext/osext.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Extensions to the standard "os" package.
     6  package osext // import "github.com/kardianos/osext"
     7  
     8  import "path/filepath"
     9  
    10  var cx, ce = executableClean()
    11  
    12  func executableClean() (string, error) {
    13  	p, err := executable()
    14  	return filepath.Clean(p), err
    15  }
    16  
    17  // Executable returns an absolute path that can be used to
    18  // re-invoke the current program.
    19  // It may not be valid after the current program exits.
    20  func Executable() (string, error) {
    21  	return cx, ce
    22  }
    23  
    24  // Returns same path as Executable, returns just the folder
    25  // path. Excludes the executable name and any trailing slash.
    26  func ExecutableFolder() (string, error) {
    27  	p, err := Executable()
    28  	if err != nil {
    29  		return "", err
    30  	}
    31  
    32  	return filepath.Dir(p), nil
    33  }