env.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package caplets
  2. import (
  3. "os"
  4. "path/filepath"
  5. "runtime"
  6. "github.com/evilsocket/islazy/str"
  7. "github.com/mitchellh/go-homedir"
  8. )
  9. const (
  10. EnvVarName = "CAPSPATH"
  11. Suffix = ".cap"
  12. InstallArchive = "https://github.com/bettercap/caplets/archive/master.zip"
  13. )
  14. func getDefaultInstallBase() string {
  15. if runtime.GOOS == "windows" {
  16. return filepath.Join(os.Getenv("ALLUSERSPROFILE"), "bettercap")
  17. }
  18. return "/usr/local/share/bettercap/"
  19. }
  20. func getUserHomeDir() string {
  21. usr, _ := homedir.Dir()
  22. return usr
  23. }
  24. var (
  25. InstallBase = ""
  26. InstallPathArchive = ""
  27. InstallPath = ""
  28. ArchivePath = ""
  29. LoadPaths = []string(nil)
  30. )
  31. func Setup(base string) error {
  32. InstallBase = base
  33. InstallPathArchive = filepath.Join(InstallBase, "caplets-master")
  34. InstallPath = filepath.Join(InstallBase, "caplets")
  35. ArchivePath = filepath.Join(os.TempDir(), "caplets.zip")
  36. LoadPaths = []string{
  37. "./",
  38. "./caplets/",
  39. InstallPath,
  40. filepath.Join(getUserHomeDir(), "caplets"),
  41. }
  42. for _, path := range str.SplitBy(str.Trim(os.Getenv(EnvVarName)), string(os.PathListSeparator)) {
  43. if path = str.Trim(path); len(path) > 0 {
  44. LoadPaths = append(LoadPaths, path)
  45. }
  46. }
  47. for i, path := range LoadPaths {
  48. LoadPaths[i], _ = filepath.Abs(path)
  49. }
  50. return nil
  51. }
  52. func init() {
  53. // init with defaults
  54. Setup(getDefaultInstallBase())
  55. }