Skip to content

Commit

Permalink
new: new -caplets-path argument to specify an alternative caplets bas…
Browse files Browse the repository at this point in the history
…e path (closes #850)
  • Loading branch information
evilsocket committed Mar 23, 2021
1 parent 161124a commit d63122b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
25 changes: 19 additions & 6 deletions caplets/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
InstallArchive = "https://github.com/bettercap/caplets/archive/master.zip"
)

func getInstallBase() string {
func getDefaultInstallBase() string {
if runtime.GOOS == "windows" {
return filepath.Join(os.Getenv("ALLUSERSPROFILE"), "bettercap")
}
Expand All @@ -28,20 +28,26 @@ func getUserHomeDir() string {
}

var (
InstallBase = getInstallBase()
InstallBase = ""
InstallPathArchive = ""
InstallPath = ""
ArchivePath = ""
LoadPaths = []string(nil)
)

func Setup(base string) error {
InstallBase = base
InstallPathArchive = filepath.Join(InstallBase, "caplets-master")
InstallPath = filepath.Join(InstallBase, "caplets")
ArchivePath = filepath.Join(os.TempDir(), "caplets.zip")
InstallPath = filepath.Join(InstallBase, "caplets")
ArchivePath = filepath.Join(os.TempDir(), "caplets.zip")

LoadPaths = []string{
"./",
"./caplets/",
InstallPath,
filepath.Join(getUserHomeDir(), "caplets"),
}
)

func init() {
for _, path := range str.SplitBy(str.Trim(os.Getenv(EnvVarName)), string(os.PathListSeparator)) {
if path = str.Trim(path); len(path) > 0 {
LoadPaths = append(LoadPaths, path)
Expand All @@ -51,4 +57,11 @@ func init() {
for i, path := range LoadPaths {
LoadPaths[i], _ = filepath.Abs(path)
}

return nil
}

func init() {
// init with defaults
Setup(getDefaultInstallBase())
}
2 changes: 2 additions & 0 deletions core/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Options struct {
Commands *string
CpuProfile *string
MemProfile *string
CapletsPath *string
}

func ParseOptions() (Options, error) {
Expand All @@ -33,6 +34,7 @@ func ParseOptions() (Options, error) {
Commands: flag.String("eval", "", "Run one or more commands separated by ; in the interactive session, used to set variables via command line."),
CpuProfile: flag.String("cpu-profile", "", "Write cpu profile `file`."),
MemProfile: flag.String("mem-profile", "", "Write memory profile to `file`."),
CapletsPath: flag.String("caplets-path", "", "Specify an alternative base path for caplets."),
}

flag.Parse()
Expand Down
6 changes: 6 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ func (s *Session) Start() error {
return s.Modules[i].Name() < s.Modules[j].Name()
})

if *s.Options.CapletsPath != "" {
if err = caplets.Setup(*s.Options.CapletsPath); err != nil {
return err
}
}

if s.Interface, err = network.FindInterface(*s.Options.InterfaceName); err != nil {
return err
}
Expand Down

0 comments on commit d63122b

Please sign in to comment.