Skip to content

Commit

Permalink
Add vigilant mode
Browse files Browse the repository at this point in the history
  • Loading branch information
simbados committed Feb 11, 2024
1 parent 2e5c55c commit 129e1e3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
- [ ] Add support for removing config files with sb
- [ ] Possibility to add overall config json file to apply to all commands (discuss if good idea?)
- [ ] Add TLDR; for README
- [ ] Vigilant mode, ask at the end to proceed with command and config
- [x] Vigilant mode, ask at the end to proceed with command and config
- [ ] Option for only applying root or local config ```-c local -c root```
- [ ] More testing for critical parts of the tool

Expand Down
25 changes: 22 additions & 3 deletions cmd/sb/sb.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,32 @@ func main() {
}

// build sandbox profile
profile := sandbox.BuildSandboxProfile(&context)
minifiedProfile, profile := sandbox.BuildSandboxProfile(&context)

log.LogDebug(log.PrettyJson(&context))

if !types.CliOptions.DryRunEnabled {
// Run the sandbox
args := append(append(append(append(append(append([]string{}, "sandbox-exec"), "-p"), profile), context.Config.BinaryName), context.Config.Commands...))
args := append(append(append(append(append(append([]string{}, "sandbox-exec"), "-p"), minifiedProfile), context.Config.BinaryName), context.Config.Commands...))
log.LogHighlight("Running sandbox exec with following command")
log.LogHighlight(strings.Join(args[3:], " "))
if types.CliOptions.VigilantModeEnabled {
log.LogInfoLn("The sandbox profile:")
log.LogInfoLn(profile)
log.LogHighlight("Do you want to run the command")
log.LogHighlight(strings.Join(args[3:], " "))
log.LogHighlightSl("(Y)es/(n)o ")
var answer string
_, err := fmt.Scanln(&answer)
if err != nil {
log.LogErr("Can not read your input")
}
if answer != "y" {
log.LogInfoLn("Exiting program without running binary")
os.Exit(0)
}
} else {
log.LogHighlight(strings.Join(args[3:], " "))
}
osHelper.Run(args)
}
}
Expand Down Expand Up @@ -85,6 +102,8 @@ E.g. sb -s npm
`)
}
util.ShowConfig(context, commands[0])
} else if currentOption == "--vigilant" || currentOption == "-vi" {
types.CliOptions.VigilantModeEnabled = true
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type LoggerType interface {
LogInfoLn(args ...any)
LogInfoSl(args ...any)
LogHighlight(args ...any)
LogHighlightSl(args ...any)
PrettyJson(toPretty any) string
}

Expand All @@ -34,6 +35,12 @@ func (il ImplLogger) LogHighlight(args ...any) {
fmt.Print(ColorReset)
}

func (il ImplLogger) LogHighlightSl(args ...any) {
fmt.Print(ColorGreen)
fmt.Print(args...)
fmt.Print(ColorReset)
}

func (il ImplLogger) LogWarn(args ...any) {
fmt.Print(ColorOrange)
fmt.Println(args...)
Expand Down Expand Up @@ -84,6 +91,10 @@ func LogHighlight(args ...any) {
Logger.LogHighlight(args...)
}

func LogHighlightSl(args ...any) {
Logger.LogHighlightSl(args...)
}

func LogWarn(args ...any) {
Logger.LogWarn(args...)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
)

func BuildSandboxProfile(context *types.Context) string {
func BuildSandboxProfile(context *types.Context) (string, string) {
profile := "; Generated by cli tool sb\n"
profile += readBaseProfile(&context.Paths) + "\n"
profile += fmt.Sprintf(`
Expand All @@ -31,7 +31,7 @@ func BuildSandboxProfile(context *types.Context) string {
profile = netOut(context.Config.SbConfig, profile)
}
log.LogDebug(profile)
return minifyProfile(profile)
return minifyProfile(profile), profile
}

func minifyProfile(profile string) string {
Expand Down
14 changes: 8 additions & 6 deletions internal/types/cliOptions.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package types

type CliOptionsStr struct {
DebugEnabled bool `json:"debug-enabled"`
DryRunEnabled bool `json:"dry-run-enabled"`
CreateExeEnabled bool `json:"create-exe-enabled"`
HelpEnabled bool `json:"help"`
VersionEnabled bool `json:"version-enabled"`
EditEnabled bool `json:"edit-enabled"`
DebugEnabled bool `json:"debug-enabled"`
DryRunEnabled bool `json:"dry-run-enabled"`
CreateExeEnabled bool `json:"create-exe-enabled"`
HelpEnabled bool `json:"help"`
VersionEnabled bool `json:"version-enabled"`
EditEnabled bool `json:"edit-enabled"`
VigilantModeEnabled bool `json:"vigilant_mode_enabled"`
}

type EnvsStr struct {
Expand All @@ -33,4 +34,5 @@ var ValidCliOptions = map[string]string{
"--edit": "--edit",
"-e": "-e",
"-s": "--show",
"-vi": "--vigilant",
}
3 changes: 2 additions & 1 deletion internal/util/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The cli options are the following:
--dry-run -dr Do not execute the sandbox with the wanted binary will set debug and print to true so that you have all the information what sb would do
--help -h Print this help section
--version -v Show which version of sb is installed
--show -s Show location of all config files for this binary and the content of the file that would be applied`)
--show -s Show location of all config files for this binary and the content of the file that would be applied
--vigilant -vi Print Profile and ask before running command`)
os.Exit(0)
}

0 comments on commit 129e1e3

Please sign in to comment.