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