-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support passing config options as CLI arguments (#15)
- Loading branch information
Showing
12 changed files
with
349 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
var arrayFields = []string{ | ||
"urls", | ||
"follow", | ||
"allowedDomains", | ||
"blockedDomains", | ||
"allowedURLs", | ||
"blockedURLs", | ||
"proxies", | ||
} | ||
|
||
func parseConfigArgs(args []string) (map[string]any, error) { | ||
updates := map[string]any{} | ||
|
||
flag := "" | ||
for _, arg := range normalizeArgs(args) { | ||
if flag == "" && !isFlag(arg) { | ||
return nil, fmt.Errorf("expected flag, got %q instead", arg) | ||
} | ||
|
||
if flag != "" && isFlag(arg) { | ||
updates[flag[2:]] = true | ||
flag = "" | ||
continue | ||
} | ||
|
||
if flag != "" { | ||
if v, ok := updates[flag[2:]]; ok { | ||
if vv, ok := v.([]any); ok { | ||
updates[flag[2:]] = append(vv, parseArg(arg)) | ||
} else { | ||
updates[flag[2:]] = []any{v, parseArg(arg)} | ||
} | ||
} else { | ||
if slices.Contains(arrayFields, flag[2:]) { | ||
updates[flag[2:]] = []any{parseArg(arg)} | ||
} else { | ||
updates[flag[2:]] = parseArg(arg) | ||
} | ||
} | ||
flag = "" | ||
continue | ||
} | ||
|
||
flag = arg | ||
} | ||
|
||
if flag != "" { | ||
updates[flag[2:]] = true | ||
flag = "" | ||
} | ||
|
||
return updates, nil | ||
} | ||
|
||
func normalizeArgs(args []string) []string { | ||
var norm []string | ||
|
||
for _, arg := range args { | ||
if !strings.HasPrefix(arg, "--") { | ||
norm = append(norm, arg) | ||
} else { | ||
norm = append(norm, strings.SplitN(arg, "=", 2)...) | ||
} | ||
} | ||
|
||
return norm | ||
} | ||
|
||
func parseArg(arg string) any { | ||
if arg == "true" { | ||
return true | ||
} | ||
if arg == "false" { | ||
return false | ||
} | ||
if num, err := strconv.Atoi(arg); err == nil { | ||
return num | ||
} | ||
return arg | ||
} | ||
|
||
func isFlag(arg string) bool { | ||
return strings.HasPrefix(arg, "--") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package cmd | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseConfigUpdates(t *testing.T) { | ||
tests := []struct { | ||
flags string | ||
err bool | ||
updates map[string]any | ||
}{ | ||
{ | ||
flags: `--foo bar`, | ||
updates: map[string]any{"foo": "bar"}, | ||
}, | ||
{ | ||
flags: `--foo=bar`, | ||
updates: map[string]any{"foo": "bar"}, | ||
}, | ||
{ | ||
flags: `--foo`, | ||
updates: map[string]any{"foo": true}, | ||
}, | ||
{ | ||
flags: `--foo false`, | ||
updates: map[string]any{"foo": false}, | ||
}, | ||
{ | ||
flags: `--foo a --foo b`, | ||
updates: map[string]any{"foo": []any{"a", "b"}}, | ||
}, | ||
{ | ||
flags: `--foo a --foo=b`, | ||
updates: map[string]any{"foo": []any{"a", "b"}}, | ||
}, | ||
{ | ||
flags: `--foo 69`, | ||
updates: map[string]any{"foo": 69}, | ||
}, | ||
{ | ||
flags: `--foo.bar a`, | ||
updates: map[string]any{"foo.bar": "a"}, | ||
}, | ||
{ | ||
flags: `foo`, | ||
err: true, | ||
}, | ||
{ | ||
flags: `--foo a b`, | ||
err: true, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.flags, func(t *testing.T) { | ||
args, err := parseConfigArgs(strings.Fields(test.flags)) | ||
|
||
if test.err { | ||
require.Error(t, err) | ||
require.Empty(t, args) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, test.updates, args) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.