v0.27.1 packaging update, install and initial config
This commit is contained in:
1 parent
60abc97eb0
commit
4513a9e62b
15 files changed
+431
-110
No files matched your search
@@ -6,11 +6,15 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
shipped "github.com/lixenwraith/logwisp/config"
|
||||
"github.com/lixenwraith/logwisp/internal/config"
|
||||
|
||||
"github.com/lixenwraith/toml"
|
||||
@@ -37,6 +41,8 @@ var commands = []command{
|
||||
"Files are never overwritten; keys are written mode 0600.", tlsCommands},
|
||||
{"preset", "Show the pipeline a preset makes, as TOML",
|
||||
"lw --preset NAME,key=value runs one; flags here are its keys.", presetCommands()},
|
||||
{"config", "Place the annotated default configuration",
|
||||
"Files are never overwritten; lw --dump prints the effective configuration.", configCommands},
|
||||
}
|
||||
|
||||
// shorts are lw's one-letter flags, each its long flag in every command that
|
||||
@@ -287,3 +293,41 @@ func presetCommands() []subcommand {
|
||||
}
|
||||
return subs
|
||||
}
|
||||
|
||||
var configCommands = []subcommand{
|
||||
{"init", "[--out FILE]",
|
||||
"Write the annotated default configuration, to edit and run",
|
||||
nil, defineConfigInit},
|
||||
}
|
||||
|
||||
func defineConfigInit(flags *flag.FlagSet) func(stdout, stderr io.Writer) error {
|
||||
out := flags.String("out", "", "`file` to write (default: ~/.config/logwisp/logwisp.toml)")
|
||||
return func(_, stderr io.Writer) error {
|
||||
user, err := config.UserFile()
|
||||
path := cmp.Or(*out, user)
|
||||
if path == "" {
|
||||
return fmt.Errorf("no home directory for the default file (%v): name one with --out", err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writeNew(path, shipped.Sample, 0o644); errors.Is(err, fs.ErrExist) {
|
||||
if fi, err := os.Stat(path); err == nil && fi.IsDir() {
|
||||
return fmt.Errorf("%s is a directory: name the file, as in %s", path, filepath.Join(path, "logwisp.toml"))
|
||||
}
|
||||
return fmt.Errorf("%s exists; remove it to replace it", path)
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
// -c refuses a path that climbs out with ..; an absolute one loads anywhere
|
||||
if abs, err := filepath.Abs(path); err == nil {
|
||||
path = abs
|
||||
}
|
||||
if found, err := filepath.Abs(config.DefaultPath()); err == nil && found == path {
|
||||
fmt.Fprintf(stderr, "wrote %s; lw reads it without -c\n", path)
|
||||
} else {
|
||||
fmt.Fprintf(stderr, "wrote %s; run it with lw -c %s\n", path, path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
shipped "github.com/lixenwraith/logwisp/config"
|
||||
"github.com/lixenwraith/logwisp/internal/config"
|
||||
"github.com/lixenwraith/logwisp/internal/testutil"
|
||||
"github.com/lixenwraith/logwisp/internal/tlsx"
|
||||
@@ -261,6 +262,35 @@ func TestTLSCommandsIssueAVerifiableChain(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// lw config init writes the shipped configuration to the file lw reads without
|
||||
// -c, or to --out, says how lw finds it, and never replaces a file
|
||||
func TestConfigInitWritesTheShippedFileOnce(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv("LOGWISP_CONFIG_FILE", "")
|
||||
t.Setenv("LOGWISP_CONFIG_DIR", "")
|
||||
other := filepath.Join(t.TempDir(), "etc", "logwisp.toml")
|
||||
for _, c := range []struct {
|
||||
args []string
|
||||
hint string
|
||||
}{
|
||||
{[]string{"init"}, "lw reads it without -c"},
|
||||
{[]string{"init", "--out", other}, "lw -c " + other},
|
||||
} {
|
||||
if code, _, stderr := runCommand(t, "config", c.args...); code != 0 || !strings.Contains(stderr, c.hint) {
|
||||
t.Fatalf("lw config %v: exit %d: %s", c.args, code, stderr)
|
||||
}
|
||||
if code, _, stderr := runCommand(t, "config", c.args...); code != 1 || !strings.Contains(stderr, "exists") {
|
||||
t.Fatalf("lw config %v again: exit %d: %s", c.args, code, stderr)
|
||||
}
|
||||
}
|
||||
for _, path := range []string{filepath.Join(home, ".config", "logwisp", "logwisp.toml"), other} {
|
||||
if data, err := os.ReadFile(path); err != nil || !bytes.Equal(data, shipped.Sample) {
|
||||
t.Errorf("%s is not the shipped configuration: %v", path, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var update = flag.Bool("update", false, "rewrite the completion scripts: make completion")
|
||||
|
||||
// The installed completion scripts are what lw's tables generate
|
||||
|
||||
Reference in new issue
Block a user