Skip to content

Commit

Permalink
Add better parsing of config keys and add test for parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
simbados committed Feb 5, 2024
1 parent d9dab63 commit 869e50f
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 19 deletions.
16 changes: 16 additions & 0 deletions cmd/sb/sb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sb/internal/sandbox"
"sb/internal/types"
"sb/internal/util"
"strconv"
)

func main() {
Expand All @@ -20,6 +21,8 @@ func main() {
// set relevant paths
configAllPath(&context)

parseEnvs()

// set config parameter
setConfigParams(&context, input)
// parse config files
Expand All @@ -42,6 +45,19 @@ func main() {
}
}

func parseEnvs() {
for _, env := range types.AllEnvs {
val := os.Getenv(env)
if env == types.DEV_MODE && val != "" {
if val, err := strconv.ParseBool(val); err == nil {
types.Envs.DevModeEnabled = val
} else {
log.LogErr(fmt.Sprintf("Can not parse %v env variable please provide true or false", types.DEV_MODE))
}
}
}
}

func setConfigParams(context *types.Context, args []string) {
cliOptions, cliConfig, commands := parse.OptionsParsing(&context.Paths, args[1:])
if types.CliOptions.EditEnabled {
Expand Down
6 changes: 6 additions & 0 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func LogErr(args ...any) {
Logger.LogErr(args...)
}

func LogDev(args ...any) {
if types.Envs.DevModeEnabled {
fmt.Println(args...)
}
}

func LogHighlight(args ...any) {
Logger.LogHighlight(args...)
}
Expand Down
1 change: 0 additions & 1 deletion internal/parse/parseCliOptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func parseCliOptions(paths *types.Paths, option string) {
types.CliOptions.DebugEnabled = true
} else if currentOption == "--dry-run" || currentOption == "-dr" {
types.CliOptions.DryRunEnabled = true
types.CliOptions.DebugEnabled = true
} else if currentOption == "--create-exe" || currentOption == "-ce" {
types.CliOptions.CreateExeEnabled = true
} else if currentOption == "--help" || currentOption == "-h" {
Expand Down
50 changes: 36 additions & 14 deletions internal/parse/parseConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,21 @@ func ConfigFileParsing(context *types.Context) *types.SbConfig {
}

func parseRootBinaryConfig(paths *types.Paths, path string, commands []string) *types.SbConfig {
commands = append(commands, "__root-config__")
mapping := buildCommandMap(commands)
mapping["__root-config__"] = true
var configs []*types.SbConfig
configJson := util.ParseJson(path)
for key, val := range configJson {
for _, command := range commands {
if strings.Contains(command, key) {
if val == nil {
log.LogDebug(fmt.Sprintf("No config found for key: %v in path %v", key, path))
} else {
permissions, err := parseNextJsonLevel(val)
if err {
log.LogErr("Malformed root config json, please check your config at path: ", path)
}
configs = append(configs, parseConfigIntoStruct(paths, permissions, path))
log.PrettyJson(configs)
}
}
var command string
if strings.Contains(key, "*") {
command = strings.TrimSuffix(key, "*")
} else {
command = key
}
if exists := mapping[command]; exists {
configs = parseOptionsForCommand(paths, path, val, configs)
} else {
log.LogDev(fmt.Sprintf("No config found for key: %v in path %v", key, path))
}
}
if len(configs) == 0 {
Expand All @@ -99,6 +97,30 @@ func parseRootBinaryConfig(paths *types.Paths, path string, commands []string) *
return mergeConfig(configs...)
}

func buildCommandMap(commands []string) map[string]bool {
aggregate := ""
mapping := map[string]bool{}
for _, command := range commands {
if aggregate != "" {
aggregate = aggregate + " " + command
mapping[aggregate] = true
} else {
aggregate = command
}
mapping[command] = true
}
return mapping
}

func parseOptionsForCommand(paths *types.Paths, path string, val interface{}, configs []*types.SbConfig) []*types.SbConfig {
permissions, err := parseNextJsonLevel(val)
if err {
log.LogErr("Malformed root config json, please check your config at path: ", path)
}
configs = append(configs, parseConfigIntoStruct(paths, permissions, path))
return configs
}

func parseNextJsonLevel(config interface{}) (map[string]interface{}, bool) {
rootConf, ok := config.(map[string]interface{})
return rootConf, !ok
Expand Down
27 changes: 27 additions & 0 deletions internal/parse/parseRootBinaryConfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package parse

import (
"sb/internal/types"
"slices"
"strings"
"testing"
)

func TestRootConfig(t *testing.T) {
var commands = []string{"run", "some"}
paths := types.Paths{LocalConfigPath: "Users/test/sb", HomePath: "/Users/test", RootConfigPath: "Users/test", WorkingDir: "/Users/test/sb", BinPath: "/usr/bin", BinaryPath: "/usr/bin/ls"}
config := parseRootBinaryConfig(&paths, "./test.json", commands)
if len(config.Write) != 2 {
t.Errorf("parseRootBinaryConfig should have 2 entries for write but was %v", config.Write)
}
if !slices.ContainsFunc(config.Write, func(s string) bool {
return strings.Contains(s, "yes")
}) {
t.Errorf("parseRootBinaryConfig config entry should have 'yes' in path but was %v", config.Write)
}
if !slices.ContainsFunc(config.Write, func(s string) bool {
return strings.Contains(s, "okay")
}) {
t.Errorf("parseRootBinaryConfig config entry should have 'okay' in path but was %v", config.Write)
}
}
11 changes: 11 additions & 0 deletions internal/parse/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"__root__config": {
"read": ["whatsup"]
},
"run some": {
"write": ["~/yes"]
},
"run*": {
"write": ["[wd]/okay"]
}
}
10 changes: 6 additions & 4 deletions internal/types/cliOptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ type CliOptionsStr struct {
EditEnabled bool `json:"edit-enabled"`
}

var CliOptions = CliOptionsStr{
DebugEnabled: false,
DryRunEnabled: false,
CreateExeEnabled: false,
type EnvsStr struct {
DevModeEnabled bool `json:"dev_mode_enabled"`
}

var CliOptions = CliOptionsStr{}

var Envs = EnvsStr{}

var ValidCliOptions = map[string]string{
"--debug": "--debug",
"-d": "-d",
Expand Down
4 changes: 4 additions & 0 deletions internal/types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ package types

const ConfigRepo = "/.sb-config"
const LocalConfigPath = "/.sb-config"

const DEV_MODE = "DEV_MODE"

var AllEnvs = []string{DEV_MODE}

0 comments on commit 869e50f

Please sign in to comment.