v0.27.2 minor update: long lines, switches, tls
Package / arch (push) Canceled after 0s
Package / user (push) Canceled after 0s
Package / deb (push) Canceled after 0s
Package / freebsd (push) Canceled after 0s
Package / image (push) Canceled after 0s
Test / test (push) Canceled after 0s
Test / e2e (push) Canceled after 0s
Package / arch (push) Canceled after 0s
Package / user (push) Canceled after 0s
Package / deb (push) Canceled after 0s
Package / freebsd (push) Canceled after 0s
Package / image (push) Canceled after 0s
Test / test (push) Canceled after 0s
Test / e2e (push) Canceled after 0s
This commit is contained in:
1 parent
4513a9e62b
commit
a1648ce17b
16 files changed
+159
-34
No files matched your search
@@ -135,7 +135,6 @@ func checkConfig(cfg *config.Config) int {
|
||||
// dumpConfig prints the effective configuration, flags and environment
|
||||
// resolved into it, as a file lw -c reads back.
|
||||
func dumpConfig(cfg *config.Config, w io.Writer) int {
|
||||
cfg.Dump = false
|
||||
// Snapshot gave each console sink the top-level color: keep it inherited
|
||||
for _, p := range cfg.Pipelines {
|
||||
for _, s := range p.PluginSinks {
|
||||
|
||||
+17
-1
@@ -56,6 +56,10 @@ var shorts = map[string]struct {
|
||||
"u": {"user", false},
|
||||
}
|
||||
|
||||
// switches are lw's own options that take no value and are no setting, so no
|
||||
// file or environment variable turns one on
|
||||
var switches = []string{"check", "dump", "version"}
|
||||
|
||||
// usageError is a command-line mistake: exit status 2 rather than 1
|
||||
type usageError string
|
||||
|
||||
@@ -168,11 +172,12 @@ func checkArgs(fs *flag.FlagSet, required []string) error {
|
||||
}
|
||||
|
||||
// invocation is a parsed command line: a command with its arguments, a help
|
||||
// request, or what config.Load reads.
|
||||
// request, or the switches given and what config.Load reads.
|
||||
type invocation struct {
|
||||
command *command
|
||||
args []string
|
||||
help bool
|
||||
on map[string]bool
|
||||
load config.Args
|
||||
}
|
||||
|
||||
@@ -225,6 +230,14 @@ func parseCommandLine(argv []string) (inv invocation, err error) {
|
||||
err = cmp.Or(err, fmt.Errorf("%s requires a configuration file path", typed))
|
||||
}
|
||||
inv.load.File = value
|
||||
case strings.HasPrefix(name, "--") && slices.Contains(switches, name[2:]):
|
||||
if inline {
|
||||
err = cmp.Or(err, fmt.Errorf("%s takes no value", typed))
|
||||
}
|
||||
if inv.on == nil {
|
||||
inv.on = map[string]bool{}
|
||||
}
|
||||
inv.on[name[2:]] = true
|
||||
case arg == "--color":
|
||||
// Bare, it means always: config takes no bare flag for a string key
|
||||
inv.load.Overrides = append(inv.load.Overrides, "--color="+cmp.Or(next(&i), "always"))
|
||||
@@ -232,6 +245,9 @@ func parseCommandLine(argv []string) (inv invocation, err error) {
|
||||
if !inline {
|
||||
value = next(&i)
|
||||
}
|
||||
if value == "" {
|
||||
err = cmp.Or(err, fmt.Errorf("%s requires a value; one that starts with '-' follows '='", typed))
|
||||
}
|
||||
inv.load.Specs = append(inv.load.Specs, config.Spec{Flag: name[2:], Value: value})
|
||||
case len(name) > 1 && name[0] == '-' && unicode.IsLetter(rune(name[1])):
|
||||
// lw's only single-dash options are the shorts
|
||||
|
||||
+8
-6
@@ -28,8 +28,8 @@ func TestCommandLineGrammar(t *testing.T) {
|
||||
}{
|
||||
{[]string{"-c", "a.toml", "--config=b.toml", "--quiet"}, invocation{load: config.Args{File: "b.toml", Overrides: []string{"--quiet"}}}},
|
||||
{[]string{"-c=a.toml", "--config", "b.toml"}, invocation{load: config.Args{File: "b.toml"}}},
|
||||
{[]string{"--source=null", "--sink", "http,port=1", "--filter", "--sink"},
|
||||
invocation{load: config.Args{Specs: []config.Spec{{Flag: "source", Value: "null"}, {Flag: "sink", Value: "http,port=1"}, {Flag: "filter"}, {Flag: "sink"}}}}},
|
||||
{[]string{"--source=null", "--sink", "http,port=1"},
|
||||
invocation{load: config.Args{Specs: []config.Spec{{Flag: "source", Value: "null"}, {Flag: "sink", Value: "http,port=1"}}}}},
|
||||
{[]string{"--preset", "tail,path=x", "--pipeline", "b"}, invocation{load: config.Args{Specs: []config.Spec{{Flag: "preset", Value: "tail,path=x"}, {Flag: "pipeline", Value: "b"}}}}},
|
||||
{[]string{"--color", "--color", "never", "--color=auto"}, invocation{load: config.Args{Overrides: []string{"--color=always", "--color=never", "--color=auto"}}}},
|
||||
{[]string{"--quiet", "--"}, invocation{load: config.Args{Overrides: []string{"--quiet"}}}},
|
||||
@@ -37,9 +37,10 @@ func TestCommandLineGrammar(t *testing.T) {
|
||||
{[]string{"-c", "-h"}, invocation{help: true}},
|
||||
{[]string{"help"}, invocation{help: true}},
|
||||
{[]string{"tls", "ca", "-h"}, invocation{command: &commands[1], args: []string{"ca", "-h"}}},
|
||||
{[]string{"-q", "-t", "-V", "-p", "tail,path=x", "-c=a.toml", "--logging.level", "debug", "--logging.file.retention_hours=-1", "--dump=false"},
|
||||
invocation{load: config.Args{File: "a.toml", Specs: []config.Spec{{Flag: "preset", Value: "tail,path=x"}},
|
||||
Overrides: []string{"--quiet", "--check", "--version", "--logging.level=debug", "--logging.file.retention_hours=-1", "--dump=false"}}}},
|
||||
{[]string{"-q", "-t", "-V", "-p", "tail,path=x", "-c=a.toml", "--logging.level", "debug", "--logging.file.retention_hours=-1", "--dump"},
|
||||
invocation{on: map[string]bool{"check": true, "version": true, "dump": true},
|
||||
load: config.Args{File: "a.toml", Specs: []config.Spec{{Flag: "preset", Value: "tail,path=x"}},
|
||||
Overrides: []string{"--quiet", "--logging.level=debug", "--logging.file.retention_hours=-1"}}}},
|
||||
} {
|
||||
got, err := parseCommandLine(c.argv)
|
||||
if err != nil || !reflect.DeepEqual(got, c.want) {
|
||||
@@ -47,7 +48,8 @@ func TestCommandLineGrammar(t *testing.T) {
|
||||
}
|
||||
}
|
||||
for _, argv := range [][]string{{"-c"}, {"--config"}, {"--config="}, {"-c="}, {"-c", "--quiet"},
|
||||
{"-v"}, {"-qt"}, {"-u", "x"}, {"-config", "x"}, {"--logging.level"}, {"--logging.file.retention_hours", "-1"}} {
|
||||
{"-v"}, {"-qt"}, {"-u", "x"}, {"-config", "x"}, {"--logging.level"}, {"--logging.file.retention_hours", "-1"},
|
||||
{"--check=false"}, {"--version=true"}, {"--source"}, {"--sink=", "x"}, {"-p"}, {"--filter", "--sink", "x"}} {
|
||||
if _, err := parseCommandLine(argv); err == nil {
|
||||
t.Errorf("%q accepted", argv)
|
||||
}
|
||||
|
||||
@@ -83,6 +83,9 @@ func options() map[string]string {
|
||||
for long := range config.Settings() {
|
||||
options[long] = ""
|
||||
}
|
||||
for _, long := range switches {
|
||||
options[long] = ""
|
||||
}
|
||||
for letter, short := range shorts {
|
||||
if short.top {
|
||||
options[short.long] = letter
|
||||
@@ -99,7 +102,7 @@ func completionModel() (top []compFlag, cmds []compCommand, presets string) {
|
||||
f := compFlag{long: long, short: shortOf[long], value: "text", repeat: slices.Contains(specFlags, long)}
|
||||
if v, ok := valueOf[long]; ok {
|
||||
f.value = v
|
||||
} else if settings[long] {
|
||||
} else if settings[long] || slices.Contains(switches, long) {
|
||||
f.value = ""
|
||||
}
|
||||
top = append(top, f)
|
||||
|
||||
+5
-6
@@ -30,6 +30,9 @@ func main() {
|
||||
case inv.help:
|
||||
printHelp(os.Stdout)
|
||||
os.Exit(0)
|
||||
case inv.on["version"]:
|
||||
fmt.Println(version.String())
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// --- 1. Initial setup ---
|
||||
@@ -54,14 +57,10 @@ func main() {
|
||||
|
||||
InitOutputHandler(cfg.Quiet)
|
||||
|
||||
if cfg.ShowVersion {
|
||||
fmt.Println(version.String())
|
||||
os.Exit(0)
|
||||
}
|
||||
if cfg.Check {
|
||||
if inv.on["check"] {
|
||||
os.Exit(checkConfig(cfg))
|
||||
}
|
||||
if cfg.Dump {
|
||||
if inv.on["dump"] {
|
||||
os.Exit(dumpConfig(cfg, os.Stdout))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user