This commit is contained in:
Andrey Parhomenko 2023-03-24 16:54:51 +03:00
parent 29b71aa353
commit a2ad07dc5e
22 changed files with 122 additions and 195 deletions

2
go.mod
View file

@ -2,4 +2,4 @@ module github.com/surdeus/goblin
go 1.18
require github.com/surdeus/gomtool v0.0.0-20230216120523-b00a31dd9887 // indirect
require github.com/surdeus/gomtool v0.0.0-20230324073313-c382bc403f8b // indirect

6
go.sum
View file

@ -8,3 +8,9 @@ github.com/surdeus/gomtool v0.0.0-20221123071619-d67de6873631 h1:dSnG4J2jusYRVnR
github.com/surdeus/gomtool v0.0.0-20221123071619-d67de6873631/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo=
github.com/surdeus/gomtool v0.0.0-20230216120523-b00a31dd9887 h1:lKe6TayY2qB50RqPYrHBlHPeJPevNViM+UQn8/2jBxc=
github.com/surdeus/gomtool v0.0.0-20230216120523-b00a31dd9887/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo=
github.com/surdeus/gomtool v0.0.0-20230324065513-d8d05cb8077d h1:COfpUMABa+RrsD05bOnXDx9FX+oaJ+dnKiLpqOODxl8=
github.com/surdeus/gomtool v0.0.0-20230324065513-d8d05cb8077d/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo=
github.com/surdeus/gomtool v0.0.0-20230324070550-2fb327b6a6a6 h1:aOXHUmhQS/mCo8r72dERsxAnYrlk/hP61BAyZ79MkDs=
github.com/surdeus/gomtool v0.0.0-20230324070550-2fb327b6a6a6/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo=
github.com/surdeus/gomtool v0.0.0-20230324073313-c382bc403f8b h1:9IJVeXxRDTPlp12DQ8M76ydoyDCUx6J4up5UM2DwIXQ=
github.com/surdeus/gomtool v0.0.0-20230324073313-c382bc403f8b/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo=

View file

@ -30,48 +30,51 @@ import (
"github.com/surdeus/goblin/src/tool/wc"
"github.com/surdeus/goblin/src/tool/whoami"
"github.com/surdeus/goblin/src/tool/yes"
mtool "github.com/surdeus/gomtool/src/multitool"
"github.com/surdeus/gomtool/src/mtool"
)
func main() {
tools := mtool.Tools{
"basename": mtool.Tool{basename.Run, "get base name of file path"},
"cat": mtool.Tool{cat.Run, "print file data to the standard output"},
"mkdir": mtool.Tool{mkdir.Run, "make new directory"},
"echo": mtool.Tool{echo.Run, "print strings to the standard output"},
"true": mtool.Tool{gtrue.Run, "exit with true status"},
"false": mtool.Tool{gfalse.Run, "exit with false status"},
"sort": mtool.Tool{sort.Run, "sort strings inputed from standard input"},
"tac": mtool.Tool{tac.Run, "print strings from standard input in reversed order"},
"ls": mtool.Tool{ls.Run, "list directory content"},
"yes": mtool.Tool{yes.Run, "print string infinite/exact amount times"},
"date": mtool.Tool{date.Run, "print current date"},
"uniq": mtool.Tool{uniq.Run, "filter repeated strings"},
"quote": mtool.Tool{quote.Run, "quote words containing space characters"},
"urlprs": mtool.Tool{urlprs.Run, "parse URLs"},
"noext": mtool.Tool{noext.Run, "print file path without extension"},
"mergelbl": mtool.Tool{mergelbl.Run, "merge line by line"},
"ec": mtool.Tool{ec.Run, "render escape sequences"},
"read": mtool.Tool{read.Run, "read lines and exit"},
"wc": mtool.Tool{wc.Run, "count words, bytes, runes etc"},
"ftest": mtool.Tool{ftest.Run, "filter files by specified features"},
"range": mtool.Tool{grange.Run, "too lazy"},
"in": mtool.Tool{in.Run, "filter strings from stdin that aren not in arguments"},
"useprog": mtool.Tool{useprog.Run, "print the name of the first existing program in arg list"},
"path": mtool.Tool{path.Run, "print cross platform path based on cmd arguments"},
"mk": mtool.Tool{mk.Run, "file dependency system, simpler make"},
"awk": mtool.Tool{awk.Run, "simple scripting language for working with string templates"},
"basename": mtool.Tool{basename.Run, "get base name of file path", ""},
"cat": mtool.Tool{cat.Run, "print file data to the standard output", ""},
"mkdir": mtool.Tool{mkdir.Run, "make new directory", ""},
"echo": mtool.Tool{echo.Run, "print strings to the standard output", ""},
"true": mtool.Tool{gtrue.Run, "exit with true status", ""},
"false": mtool.Tool{gfalse.Run, "exit with false status", ""},
"sort": mtool.Tool{sort.Run, "sort strings inputed from standard input", ""},
"tac": mtool.Tool{tac.Run, "print strings from standard input in reversed order", ""},
"ls": mtool.Tool{ls.Run, "list directory content", ""},
"yes": mtool.Tool{yes.Run, "print string infinite/exact amount times", ""},
"date": mtool.Tool{date.Run, "print current date", ""},
"uniq": mtool.Tool{uniq.Run, "filter repeated strings", ""},
"quote": mtool.Tool{quote.Run, "quote words containing space characters", ""},
"urlprs": mtool.Tool{urlprs.Run, "parse URLs", ""},
"noext": mtool.Tool{noext.Run, "print file path without extension", ""},
"mergelbl": mtool.Tool{mergelbl.Run, "merge line by line", ""},
"ec": mtool.Tool{ec.Run, "render escape sequences", ""},
"read": mtool.Tool{read.Run, "read lines and exit", ""},
"wc": mtool.Tool{wc.Run, "count words, bytes, runes etc", ""},
"ftest": mtool.Tool{ftest.Run, "filter files by specified features", ""},
"range": mtool.Tool{grange.Run, "too lazy", ""},
"in": mtool.Tool{in.Run, "filter strings from stdin that aren not in arguments", ""},
"useprog": mtool.Tool{useprog.Run, "print the name of the first existing program in arg list", ""},
"path": mtool.Tool{path.Run, "print cross platform path based on cmd arguments", ""},
"mk": mtool.Tool{mk.Run, "file dependency system, simpler make", ""},
"awk": mtool.Tool{awk.Run, "simple scripting language for working with string templates", ""},
"paths": mtool.Tool{
paths.Run,
"convert UNIX slash separated paths into the OS compatible ones",
"",
},
"whoami": mtool.Tool{
whoami.Run,
"print current user name",
"",
},
"ln": mtool.Tool{
ln.Run,
"link files",
"",
},
}

View file

@ -3,9 +3,8 @@ package basename
import(
"fmt"
"flag"
"os"
"path"
"github.com/surdeus/gomtool/src/mtool"
)
var(
@ -18,16 +17,9 @@ func Base(p string) string {
return path.Base(p)
}
func Run(argv []string) {
arg0 = argv[0]
args = argv[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [files]\n", arg0, arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
func Run(flags *mtool.Flags) {
flags.Parse()
args := flags.Args()
lasti := len(args) - 1
for i, v := range args {

View file

@ -3,8 +3,8 @@ package cat
import(
"os"
"io"
"flag"
"fmt"
"github.com/surdeus/gomtool/src/mtool"
)
var(
@ -37,22 +37,15 @@ func fcat(f *os.File) error {
return nil
}
func Run(args []string) {
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.IntVar(&blockSize, "bs", 512, "block size")
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [options] [files]\n", arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
func Run(flags *mtool.Flags) {
flags.IntVar(&blockSize, "bs", 512, "block size")
flags.Parse()
args := flags.Args()
if len(args)>0 {
for _, p := range args {
e := Cat(p)
if e != nil {
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
fmt.Fprintf(os.Stderr, "%s.\n", e)
}
}
} else {

View file

@ -1,25 +1,17 @@
package date
import(
"os"
"flag"
"fmt"
"time"
"github.com/surdeus/gomtool/src/mtool"
)
func Run(args []string) {
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s: %s\n", arg0, arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
func Run(flagSet *mtool.Flags) {
flagSet.Parse()
args := flagSet.Args()
if len(flagSet.Args())>0 {
if len(args) > 0 {
flagSet.Usage()
os.Exit(1)
}
date := time.Now()

View file

@ -6,6 +6,7 @@ import(
"os"
"bufio"
"log"
"github.com/surdeus/gomtool/src/mtool"
)
const(
@ -31,7 +32,7 @@ handleEscChar(rd *bufio.Reader) error {
}
func
Run(args []string) {
Run(flags *mtool.Flags) {
rd := bufio.NewReader(os.Stdin)
for {
r, _, e := rd.ReadRune()

View file

@ -3,7 +3,7 @@ package echo
import (
"fmt"
"flag"
"github.com/surdeus/gomtool/src/mtool"
)
var(
@ -11,13 +11,15 @@ var(
eol = "\n"
)
func Run(args []string) {
func Run(flagSet *mtool.Flags) {
var nflag bool
flagSet := flag.NewFlagSet(args[0], flag.ExitOnError)
flagSet.BoolVar(&nflag, "n", false, "Do not print new line character.")
flagSet.StringVar(&del, "d", " ", "Delimiter of arguments")
flagSet.Parse(args[1:])
args = flagSet.Args()
flagSet.Parse()
args := flagSet.Args()
l := len(args) - 1
for i, s := range args {
fmt.Print(s)

View file

@ -1,4 +1,10 @@
package gfalse
func Run(args []string) {
import(
"os"
"github.com/surdeus/gomtool/src/mtool"
)
func Run(flags *mtool.Flags) {
os.Exit(1)
}

View file

@ -1,5 +1,5 @@
package gtrue
func Run(args []string) {
}
import "github.com/surdeus/gomtool/src/mtool"
func Run(flags *mtool.Flags) {}

View file

@ -2,17 +2,15 @@ package ls
import(
"os"
"fmt"
"flag"
"strings"
"regexp"
"path"
"github.com/surdeus/gomtool/src/mtool"
)
var(
listHidden bool
args []string
arg0 string
flagSet *flag.FlagSet
)
var slashRegexp = regexp.MustCompile("/+")
@ -126,30 +124,25 @@ ls(p string, fold int) error {
return nil
}
func Run(argv []string) {
arg0 = argv[0]
args = argv[1:]
flagSet = flag.NewFlagSet(arg0, flag.ExitOnError)
func Run(flagSet *mtool.Flags) {
var foldLvl int
flagSet.IntVar(&foldLvl, "r", 1, "List recursively with choosing deepness, can't be negative or zero.")
flagSet.BoolVar(&listHidden, "a", false, "List hidden files.")
flagSet.Parse(args)
flagSet.Parse()
args = flagSet.Args()
if foldLvl<0 {
flagSet.Usage()
os.Exit(1)
}
if foldLvl==0 && len(args)==0 {
flagSet.Usage()
os.Exit(1)
}
if len(args) == 0 {
foldLvl -= 1
if l, e := ReadDir(".") ; e != nil {
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
fmt.Fprintf(os.Stderr, "%s.\n", e)
} else {
for _, f := range l {
if !shouldList(f.Name()) {
@ -162,8 +155,7 @@ func Run(argv []string) {
e := ls(f.Name(), foldLvl)
if e!=nil {
fmt.Fprintf(os.Stderr,
"%s: %s\n",
arg0, e)
"%s\n", e)
}
}
}
@ -172,7 +164,7 @@ func Run(argv []string) {
for _, p := range args {
e := ls(p, foldLvl)
if e != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", arg0, e)
fmt.Fprintf(os.Stderr, "%s\n", e)
}
}

View file

@ -4,11 +4,11 @@ import(
"os"
"fmt"
"bufio"
"flag"
"log"
"github.com/surdeus/gomtool/src/mtool"
)
func Run(args []string) {
func Run(flagSet *mtool.Flags) {
var(
e error
buf string
@ -16,15 +16,8 @@ func Run(args []string) {
rsep := '\n'
wsep := "\n"
//del := ""
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s <file1> <file2> .. [fileN]\n", arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
flagSet.Parse()
args := flagSet.Args()
files := make([]*os.File, len(args))
for i, v := range args {

View file

@ -1,29 +1,22 @@
package mkdir
import (
"flag"
"fmt"
"os"
"github.com/surdeus/goblin/src/pathx"
"github.com/surdeus/gomtool/src/mtool"
)
func Run(args []string) {
arg0 := args[0]
args = args[1:]
func Run(flagSet *mtool.Flags) {
var (
parentFlag bool
modeArg int
)
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.BoolVar(&parentFlag, "p", false, "No error if existing, make parent as needed.")
flagSet.IntVar(&modeArg, "m", 0766, "Set file `mode`.")
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] [files]\n", arg0, arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
flagSet.Parse()
args := flagSet.Args()
if len(args) == 0 {
flagSet.Usage()
os.Exit(1)
@ -38,7 +31,7 @@ func Run(args []string) {
e = os.Mkdir(pth, mode)
}
if e != nil {
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
fmt.Fprintf(os.Stderr, "%s\n", e)
}
}
}

View file

@ -3,8 +3,7 @@ package noext
import(
"fmt"
"flag"
"os"
"github.com/surdeus/gomtool/src/mtool"
)
var(
@ -25,15 +24,9 @@ func NoExt(p string) string {
return p[:i]
}
func Run(argv []string) {
arg0 = argv[0]
args = argv[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [files]\n", arg0, arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
func Run(flagSet *mtool.Flags) {
flagSet.Parse()
args := flagSet.Args()
if len(args) < 1 { flagSet.Usage() }
fmt.Printf("%s", NoExt(args[0]))
}

View file

@ -4,10 +4,10 @@ package quote
import(
"os"
"io"
"flag"
"fmt"
"unicode"
"bufio"
"github.com/surdeus/gomtool/src/mtool"
)
func HasWhiteSpace(s string) bool {
@ -19,16 +19,8 @@ func HasWhiteSpace(s string) bool {
return false
}
func Run(args []string) {
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s: %s\n", arg0, arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
func Run(flagSet *mtool.Flags) {
flagSet.Parse()
r := bufio.NewReader(os.Stdin)
for{

View file

@ -3,26 +3,18 @@ package read
import(
"os"
"bufio"
"flag"
"fmt"
"github.com/surdeus/gomtool/src/mtool"
)
var(
nLines int
)
func Run(args []string) {
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
func Run(flagSet *mtool.Flags) {
flagSet.IntVar(&nLines, "n", 1, "amount of lines")
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [options] [files]\n", arg0)
flagSet.PrintDefaults()
os.Exit(1)
}
flagSet.Parse(args)
args = flagSet.Args()
flagSet.Parse()
//args := flagSet.Args()
if nLines <= 0 {
flagSet.Usage()

View file

@ -4,14 +4,11 @@ import (
"fmt"
"os"
"sort"
"flag"
"github.com/surdeus/goblin/src/input"
"github.com/surdeus/gomtool/src/mtool"
)
func Run(args []string) {
flagSet := flag.NewFlagSet(args[0], flag.ExitOnError)
flagSet.Parse(args[1:])
func Run(flagSet *mtool.Flags) {
lines, _ := input.ReadAllLines(os.Stdin)
sort.Strings(lines)
for _, l := range lines {

View file

@ -3,9 +3,9 @@ package tac
import(
"os"
"io"
"flag"
"fmt"
"bufio"
"github.com/surdeus/gomtool/src/mtool"
)
func reverse(a []string) chan string {
@ -47,21 +47,14 @@ func ftac(f *os.File) error {
return nil
}
func Run(args []string) {
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.Usage = func () {
fmt.Fprintf(os.Stderr, "Usage of %s: %s [files]\n", arg0, arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
func Run(flagSet *mtool.Flags) {
flagSet.Parse()
args := flagSet.Args()
if len(args)>0 {
for _, p := range args {
e := tac(p)
if e != nil {
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
fmt.Fprintf(os.Stderr, "%s.\n", e)
}
}
} else {

View file

@ -4,25 +4,18 @@ package uniq
import(
"os"
"fmt"
"flag"
"bufio"
"io"
"github.com/surdeus/gomtool/src/mtool"
)
func Run(args []string) {
func Run(flagSet *mtool.Flags) {
var(
Uflag bool
)
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.BoolVar(&Uflag, "U", false, "Print every line just one time.")
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] [string]\n", arg0, arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
flagSet.Parse()
//args := flagSet.Args()
r := bufio.NewReader(os.Stdin)
u := make(map[string]int)

View file

@ -5,6 +5,7 @@ import (
"fmt"
"net"
"net/url"
"github.com/surdeus/gomtool/src/mtool"
)
var nilStr = "_"
@ -16,10 +17,13 @@ func printNil(s string) {
fmt.Println(nilStr)
}
}
func Run(args []string) {
func Run(flags *mtool.Flags) {
nilStr = "_"
s := args[1]
flags.Parse()
args := flags.Args()
if len(args) < 1 { flags.Usage() }
s := args[0]
u, err := url.Parse(s)
if err != nil {

View file

@ -6,8 +6,8 @@ import (
"bufio"
"fmt"
"unicode"
"flag"
"strconv"
"github.com/surdeus/gomtool/src/mtool"
)
var (
@ -46,21 +46,17 @@ func finish() {
os.Exit(0)
}
func Run(args []string) {
func Run(flagSet *mtool.Flags) {
var (
r rune
siz int
)
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.BoolVar(&flags[charsFlag], "c", false, "print amount of chars(bytes)")
flagSet.BoolVar(&flags[runesFlag], "r", false, "print amount of runes in UTF stream")
flagSet.BoolVar(&flags[wordsFlag], "w", false, "print amount of words")
flagSet.BoolVar(&flags[linesFlag], "l", false, "print amount of lines")
flagSet.Parse(args)
flagSet.Parse()
for i, v := range flags {
if v {

View file

@ -4,9 +4,9 @@ package yes
import(
"os"
"fmt"
"flag"
"strings"
"github.com/surdeus/goblin/src/input"
"github.com/surdeus/gomtool/src/mtool"
)
var(
nArg int
@ -24,24 +24,18 @@ func yes(s string){
}
}
func Run(args []string) {
func Run(flagSet *mtool.Flags) {
var(
stdinFlag bool
nFlag bool
s string
)
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.BoolVar(&stdinFlag, "s", false, "Read string from stdin.")
flagSet.BoolVar(&nFlag, "n", false, "Do not add net line character.")
flagSet.IntVar(&nArg, "N", -1, "Repeat input N times. Negative value means infinite cycle.")
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] [string]\n", arg0, arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
flagSet.Parse()
args := flagSet.Args()
if stdinFlag {
in, _ := input.ReadAllRaw(os.Stdin)