go-pkg/lsm2/cli/main.go

121 lines
2.1 KiB
Go
Raw Normal View History

2024-11-02 09:00:17 -06:00
package main
import (
"errors"
"fmt"
"io"
"iter"
"os"
"github.com/docopt/docopt-go"
"go.sour.is/pkg/lsm2"
)
var usage = `
2024-11-03 14:19:09 -07:00
Usage:
lsm2 create <archive> <files>...
lsm2 append <archive> <files>...
lsm2 read <archive> <index>`
2024-11-02 09:00:17 -06:00
type args struct {
Create bool
2024-11-03 14:19:09 -07:00
Append bool
Read bool
2024-11-02 09:00:17 -06:00
Archive string `docopt:"<archive>"`
Files []string `docopt:"<files>"`
2024-11-03 14:19:09 -07:00
Index int64 `docopt:"<index>"`
2024-11-02 09:00:17 -06:00
}
func main() {
opts, err := docopt.ParseDoc(usage)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(opts, must(opts.Bool("create")))
console := console{os.Stdin, os.Stdout, os.Stderr}
args := args{}
err = opts.Bind(&args)
fmt.Println(err)
run(console, args)
}
type console struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
func (c console) Write(b []byte) (int, error) {
return c.Stdout.Write(b)
}
2024-11-03 14:19:09 -07:00
func run(console console,a args) error {
fmt.Fprintln(console, "lsm")
2024-11-02 09:00:17 -06:00
switch {
2024-11-03 14:19:09 -07:00
case a.Create:
f, err := os.OpenFile(a.Archive, os.O_CREATE|os.O_WRONLY, 0644)
2024-11-02 09:00:17 -06:00
if err != nil {
return err
}
2024-11-03 14:19:09 -07:00
defer f.Close()
2024-11-02 09:00:17 -06:00
2024-11-03 14:19:09 -07:00
return lsm2.WriteLogFile(f, fileReaders(a.Files))
case a.Append:
f, err := os.OpenFile(a.Archive, os.O_RDWR, 0644)
if err != nil {
return err
}
defer f.Close()
2024-11-02 09:00:17 -06:00
2024-11-03 14:19:09 -07:00
return lsm2.AppendLogFile(f, fileReaders(a.Files))
case a.Read:
fmt.Fprintln(console, "reading", a.Archive)
f, err := os.Open(a.Archive)
if err != nil {
return err
}
defer f.Close()
lg, err := lsm2.ReadLogFile(f)
if err != nil {
return err
}
for i, rd := range lg.Iter() {
fmt.Fprintf(console, "=========================\n%d:\n", i)
io.Copy(console, rd)
fmt.Fprintln(console, "=========================")
}
if lg.Err != nil {
return lg.Err
}
return nil
2024-11-02 09:00:17 -06:00
default:
return errors.New("unknown command")
}
}
2024-11-03 14:19:09 -07:00
func fileReaders(names []string) iter.Seq[io.Reader] {
return iter.Seq[io.Reader](func(yield func(io.Reader) bool) {
for _, name := range names {
f, err := os.Open(name)
if err != nil {
continue
}
if !yield(f) {
f.Close()
return
}
f.Close()
}
})
}
2024-11-02 09:00:17 -06:00
func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}