cat : first naive version
This commit is contained in:
parent
7e6f8dd36c
commit
83c52e3e70
3 changed files with 62 additions and 0 deletions
58
cat/cat.go
Normal file
58
cat/cat.go
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
package cat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/brtholomy/um/go/cmd"
|
||||||
|
"github.com/brtholomy/um/go/flags"
|
||||||
|
"github.com/brtholomy/um/go/pipe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
CMD = cmd.Cat
|
||||||
|
SUMMARY = "cat um files together using a filelist"
|
||||||
|
SEPARATOR = "\n---\n\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
type options struct {
|
||||||
|
Filelist flags.Arg
|
||||||
|
Help flags.Flag[bool]
|
||||||
|
}
|
||||||
|
|
||||||
|
func initOpts() options {
|
||||||
|
return options{
|
||||||
|
flags.Arg{"", "filelist. accepts from stdin if not provided"},
|
||||||
|
flags.Flag[bool]{"--help", "-h", false, "show help"},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func cat(files []string) (string, error) {
|
||||||
|
// strings.Builder ?
|
||||||
|
// can we estimate needed size? do an ls -S call?
|
||||||
|
ff := []string{}
|
||||||
|
for _, f := range files {
|
||||||
|
dat, err := os.ReadFile(f)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error opening file: %w", err)
|
||||||
|
}
|
||||||
|
ff = append(ff, string(dat))
|
||||||
|
}
|
||||||
|
return strings.Join(ff, SEPARATOR), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Cat(args []string) {
|
||||||
|
opts := initOpts()
|
||||||
|
flags.ParseArgs(CMD, SUMMARY, args, &opts)
|
||||||
|
files, err := pipe.FileListSplitMaybeStdin(opts.Filelist.Val)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("um %s: %s", CMD, err)
|
||||||
|
}
|
||||||
|
s, err := cat(files)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("um %s: %s", CMD, err)
|
||||||
|
}
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
@ -7,5 +7,6 @@ const (
|
||||||
Next Subcommand = "next"
|
Next Subcommand = "next"
|
||||||
Last Subcommand = "last"
|
Last Subcommand = "last"
|
||||||
Sort Subcommand = "sort"
|
Sort Subcommand = "sort"
|
||||||
|
Cat Subcommand = "cat"
|
||||||
Help Subcommand = "help"
|
Help Subcommand = "help"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
3
um.go
3
um.go
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/brtholomy/um/go/cat"
|
||||||
"github.com/brtholomy/um/go/cmd"
|
"github.com/brtholomy/um/go/cmd"
|
||||||
"github.com/brtholomy/um/go/last"
|
"github.com/brtholomy/um/go/last"
|
||||||
"github.com/brtholomy/um/go/next"
|
"github.com/brtholomy/um/go/next"
|
||||||
|
|
@ -44,6 +45,8 @@ func main() {
|
||||||
tag.Tag(args)
|
tag.Tag(args)
|
||||||
case cmd.Sort:
|
case cmd.Sort:
|
||||||
sort.Sort(args)
|
sort.Sort(args)
|
||||||
|
case cmd.Cat:
|
||||||
|
cat.Cat(args)
|
||||||
case cmd.Help:
|
case cmd.Help:
|
||||||
log.Fatalln(helpLong)
|
log.Fatalln(helpLong)
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue