Use the new mtool implementation.

This commit is contained in:
Andrey Parhomenko 2023-03-24 19:47:31 +03:00
parent a2ad07dc5e
commit 6777a136e6
12 changed files with 44 additions and 91 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-20230324073313-c382bc403f8b // indirect
require github.com/surdeus/gomtool v0.0.0-20230324163514-3199e25c3890 // indirect

2
go.sum
View file

@ -14,3 +14,5 @@ github.com/surdeus/gomtool v0.0.0-20230324070550-2fb327b6a6a6 h1:aOXHUmhQS/mCo8r
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=
github.com/surdeus/gomtool v0.0.0-20230324163514-3199e25c3890 h1:mT4VDgCb2AgPfq2k4GfXI8O3I2sIjHJUaJwBvx95AYM=
github.com/surdeus/gomtool v0.0.0-20230324163514-3199e25c3890/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo=

View file

@ -18,7 +18,6 @@ import (
"github.com/surdeus/goblin/src/tool/mk"
"github.com/surdeus/goblin/src/tool/mkdir"
"github.com/surdeus/goblin/src/tool/noext"
"github.com/surdeus/goblin/src/tool/path"
"github.com/surdeus/goblin/src/tool/paths"
"github.com/surdeus/goblin/src/tool/quote"
"github.com/surdeus/goblin/src/tool/read"
@ -57,8 +56,7 @@ func main() {
"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", ""},
"which": mtool.Tool{useprog.Run, "print the name or the path of the first existing program in arg list", ""},
"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{

View file

@ -41,6 +41,8 @@ import (
"github.com/surdeus/goblin/src/tool/awk/interp"
"github.com/surdeus/goblin/src/tool/awk/lexer"
"github.com/surdeus/goblin/src/tool/awk/parser"
"github.com/surdeus/gomtool/src/mtool"
)
const (
@ -67,7 +69,7 @@ Additional GoAWK arguments:
`
)
func Run(args []string) {
func Run(flags *mtool.Flags) {
// Parse command line arguments manually rather than using the
// "flag" package, so we can support flags with no space between
// flag and argument, like '-F:' (allowed by POSIX)
@ -83,7 +85,8 @@ func Run(args []string) {
outputMode := ""
header := false
argv0 := args[0]
argv0 := flags.UtilName()
args := flags.Args()
var i int
for i = 1; i < len(args); i++ {

View file

@ -5,7 +5,7 @@ import (
"io"
"bufio"
"fmt"
"flag"
"github.com/surdeus/gomtool/src/mtool"
)
var (
@ -76,23 +76,14 @@ func checkFile(p string) bool {
return true
}
func Run(args []string) {
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s <options>\n", arg0)
flagSet.PrintDefaults()
os.Exit(1)
}
func Run(flagSet *mtool.Flags) {
flagSet.BoolVar(&flags[fileFlag], "f", false, "is file")
flagSet.BoolVar(&flags[dirFlag], "d", false, "is directory")
flagSet.BoolVar(&flags[writFlag], "w", false, "is writable")
flagSet.BoolVar(&flags[readFlag], "r", false, "is readable")
flagSet.Parse(args)
args = flagSet.Args()
flagSet.Parse()
args := flagSet.Args()
if len(args) != 0 {
flagSet.Usage()

View file

@ -1,14 +1,13 @@
package grange
/* Concatenate files in "stdout". */
import(
"os"
"flag"
"fmt"
"strconv"
"github.com/surdeus/gomtool/src/mtool"
)
var(
flagSet *flag.FlagSet
flagSet *mtool.Flags
args []string
blockSize int
rangeType string
@ -106,17 +105,11 @@ func ByteRange() {
func RuneRange() {
}
func Run(arg []string) {
arg0 := arg[0]
args = arg[1:]
flagSet = flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.StringVar(&rangeType, "t", "int", "range type")
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [options] [files]\n", arg0)
flagSet.PrintDefaults()
os.Exit(1)
}
flagSet.Parse(args)
func Run(flags *mtool.Flags) {
flags.StringVar(&rangeType, "t", "int", "range type")
flags.Parse()
args = flagSet.Args()
flagSet = flags
rangeTypeMap[rangeType]()
}

View file

@ -5,27 +5,18 @@ import (
"io"
"bufio"
"fmt"
"flag"
"github.com/surdeus/gomtool/src/mtool"
)
func Run(args []string) {
func Run(flagSet *mtool.Flags) {
var (
print bool
not bool
)
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.BoolVar(&print, "p", false, "print matching lines")
flagSet.BoolVar(&not, "n", false, "find not matching lines")
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s <str1> [str2, ...str3]\n", arg0)
flagSet.PrintDefaults()
os.Exit(1)
}
flagSet.Parse(args)
args = flagSet.Args()
flagSet.Parse()
args := flagSet.Args()
if len(args) == 0 {
//flagSet.Usage()

View file

@ -1,18 +1,17 @@
package ln
import (
"flag"
"fmt"
"os"
"github.com/surdeus/gomtool/src/mtool"
)
func Run(args []string) {
func Run(flagSet *mtool.Flags) {
var lflag bool
flagSet := flag.NewFlagSet(args[0], flag.ExitOnError)
flagSet.BoolVar(&lflag, "s", false, "make a symbolic link, not a hard one")
flagSet.Parse(args[1:])
args = flagSet.Args()
flagSet.Parse()
args := flagSet.Args()
if len(args) != 2 {
flagSet.Usage()

View file

@ -2,7 +2,6 @@ package mk
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"os"
@ -11,6 +10,7 @@ import (
"sync"
"github.com/surdeus/goblin/src/pathx"
"github.com/surdeus/gomtool/src/mtool"
)
// True if messages should be printed without fancy colors.
@ -317,24 +317,19 @@ func mkPrintRecipe(target string, recipe string, quiet bool) {
mkMsgMutex.Unlock()
}
func Run(args []string) {
func Run(flags *mtool.Flags) {
var mkfilepath string
var interactive bool
var dryrun bool
var shallowrebuild bool
var quiet bool
arg0 := args[0]
args = args[1:]
if mkincdir := os.Getenv("MKINCDIR"); mkincdir == "" {
homeDir, _ := os.UserHomeDir()
homeDir = pathx.FromReal(homeDir).String()
os.Setenv("MKINCDIR", homeDir+"/app/goblin/mk/inc")
}
flags := flag.NewFlagSet(arg0, flag.ExitOnError)
flags.StringVar(&mkfilepath, "f", "mkfile", "use the given file as mkfile")
flags.BoolVar(&dryrun, "n", false, "print commands without actually executing")
flags.BoolVar(&shallowrebuild, "r", false, "force building of just targets")
@ -342,7 +337,7 @@ func Run(args []string) {
flags.IntVar(&subprocsAllowed, "p", 4, "maximum number of jobs to execute in parallel")
flags.BoolVar(&interactive, "i", false, "prompt before executing rules")
flags.BoolVar(&quiet, "q", false, "don't print recipes before executing them")
flags.Parse(args)
flags.Parse()
mkfile, err := os.Open(pathx.From(mkfilepath).Real())
if err != nil {

View file

@ -3,7 +3,6 @@ package paths
import (
"bufio"
"errors"
"flag"
"fmt"
"log"
"os"
@ -11,6 +10,7 @@ import (
"strings"
"github.com/surdeus/goblin/src/pathx"
"github.com/surdeus/gomtool/src/mtool"
)
var (
@ -50,17 +50,15 @@ func handlePath(p string) {
fmt.Println(toPrint)
}
func Run(args []string) {
arg0 := args[0]
args = args[1:]
flags := flag.NewFlagSet(arg0, flag.ExitOnError)
func Run(flags *mtool.Flags) {
flags.StringVar(&part, "p", "all", "part of path you want to print")
flags.BoolVar(&r, "r", false, "print real OS dependent paths")
flags.BoolVar(&fromReal, "fr", false, "take input paths as real ones")
flags.BoolVar(&ec, "ec", false, "escape characters (mostly for '\\' char in Git bash")
flags.Parse(args)
args = flags.Args()
flags.Parse()
args := flags.Args()
lhandler, ok := handlers[part]
if !ok {
log.Fatal(noPartErr)

View file

@ -1,26 +1,16 @@
package useprog
import (
"os"
"os/exec"
"fmt"
"flag"
"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: %s <prog1> [prog2, ...prog3]\n", arg0)
flagSet.PrintDefaults()
os.Exit(1)
}
func Run(flagSet *mtool.Flags) {
flagSet.Parse()
args := flagSet.Args()
flagSet.Parse(args)
args = flagSet.Args()
if len(args) == 0 {
if len(args) < 1 {
flagSet.Usage()
}

View file

@ -3,20 +3,13 @@ package whoami
import(
"os"
"os/user"
"flag"
"fmt"
"log"
"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()
if len(flagSet.Args())>0 {
flagSet.Usage()