osext.go 873 B

123456789101112131415161718192021222324252627282930313233
  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. // Extensions to the standard "os" package.
  5. package osext // import "github.com/kardianos/osext"
  6. import "path/filepath"
  7. var cx, ce = executableClean()
  8. func executableClean() (string, error) {
  9. p, err := executable()
  10. return filepath.Clean(p), err
  11. }
  12. // Executable returns an absolute path that can be used to
  13. // re-invoke the current program.
  14. // It may not be valid after the current program exits.
  15. func Executable() (string, error) {
  16. return cx, ce
  17. }
  18. // Returns same path as Executable, returns just the folder
  19. // path. Excludes the executable name and any trailing slash.
  20. func ExecutableFolder() (string, error) {
  21. p, err := Executable()
  22. if err != nil {
  23. return "", err
  24. }
  25. return filepath.Dir(p), nil
  26. }