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 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-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 h1:9IJVeXxRDTPlp12DQ8M76ydoyDCUx6J4up5UM2DwIXQ=
github.com/surdeus/gomtool v0.0.0-20230324073313-c382bc403f8b/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo= 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/mk"
"github.com/surdeus/goblin/src/tool/mkdir" "github.com/surdeus/goblin/src/tool/mkdir"
"github.com/surdeus/goblin/src/tool/noext" "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/paths"
"github.com/surdeus/goblin/src/tool/quote" "github.com/surdeus/goblin/src/tool/quote"
"github.com/surdeus/goblin/src/tool/read" "github.com/surdeus/goblin/src/tool/read"
@ -57,8 +56,7 @@ func main() {
"ftest": mtool.Tool{ftest.Run, "filter files by specified features", ""}, "ftest": mtool.Tool{ftest.Run, "filter files by specified features", ""},
"range": mtool.Tool{grange.Run, "too lazy", ""}, "range": mtool.Tool{grange.Run, "too lazy", ""},
"in": mtool.Tool{in.Run, "filter strings from stdin that aren not in arguments", ""}, "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", ""}, "which": mtool.Tool{useprog.Run, "print the name or the path 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", ""}, "mk": mtool.Tool{mk.Run, "file dependency system, simpler make", ""},
"awk": mtool.Tool{awk.Run, "simple scripting language for working with string templates", ""}, "awk": mtool.Tool{awk.Run, "simple scripting language for working with string templates", ""},
"paths": mtool.Tool{ "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/interp"
"github.com/surdeus/goblin/src/tool/awk/lexer" "github.com/surdeus/goblin/src/tool/awk/lexer"
"github.com/surdeus/goblin/src/tool/awk/parser" "github.com/surdeus/goblin/src/tool/awk/parser"
"github.com/surdeus/gomtool/src/mtool"
) )
const ( 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 // Parse command line arguments manually rather than using the
// "flag" package, so we can support flags with no space between // "flag" package, so we can support flags with no space between
// flag and argument, like '-F:' (allowed by POSIX) // flag and argument, like '-F:' (allowed by POSIX)
@ -83,7 +85,8 @@ func Run(args []string) {
outputMode := "" outputMode := ""
header := false header := false
argv0 := args[0] argv0 := flags.UtilName()
args := flags.Args()
var i int var i int
for i = 1; i < len(args); i++ { for i = 1; i < len(args); i++ {

View file

@ -5,7 +5,7 @@ import (
"io" "io"
"bufio" "bufio"
"fmt" "fmt"
"flag" "github.com/surdeus/gomtool/src/mtool"
) )
var ( var (
@ -76,23 +76,14 @@ func checkFile(p string) bool {
return true return true
} }
func Run(args []string) { func Run(flagSet *mtool.Flags) {
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)
}
flagSet.BoolVar(&flags[fileFlag], "f", false, "is file") flagSet.BoolVar(&flags[fileFlag], "f", false, "is file")
flagSet.BoolVar(&flags[dirFlag], "d", false, "is directory") flagSet.BoolVar(&flags[dirFlag], "d", false, "is directory")
flagSet.BoolVar(&flags[writFlag], "w", false, "is writable") flagSet.BoolVar(&flags[writFlag], "w", false, "is writable")
flagSet.BoolVar(&flags[readFlag], "r", false, "is readable") flagSet.BoolVar(&flags[readFlag], "r", false, "is readable")
flagSet.Parse(args) flagSet.Parse()
args = flagSet.Args() args := flagSet.Args()
if len(args) != 0 { if len(args) != 0 {
flagSet.Usage() flagSet.Usage()

View file

@ -1,14 +1,13 @@
package grange package grange
/* Concatenate files in "stdout". */ /* Concatenate files in "stdout". */
import( import(
"os"
"flag"
"fmt" "fmt"
"strconv" "strconv"
"github.com/surdeus/gomtool/src/mtool"
) )
var( var(
flagSet *flag.FlagSet flagSet *mtool.Flags
args []string args []string
blockSize int blockSize int
rangeType string rangeType string
@ -106,17 +105,11 @@ func ByteRange() {
func RuneRange() { func RuneRange() {
} }
func Run(arg []string) { func Run(flags *mtool.Flags) {
arg0 := arg[0] flags.StringVar(&rangeType, "t", "int", "range type")
args = arg[1:] flags.Parse()
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)
args = flagSet.Args() args = flagSet.Args()
flagSet = flags
rangeTypeMap[rangeType]() rangeTypeMap[rangeType]()
} }

View file

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

View file

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

View file

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

View file

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

View file

@ -1,26 +1,16 @@
package useprog package useprog
import ( import (
"os"
"os/exec" "os/exec"
"fmt" "fmt"
"flag" "github.com/surdeus/gomtool/src/mtool"
) )
func Run(args []string) { func Run(flagSet *mtool.Flags) {
arg0 := args[0] flagSet.Parse()
args = args[1:] args := flagSet.Args()
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)
}
flagSet.Parse(args) if len(args) < 1 {
args = flagSet.Args()
if len(args) == 0 {
flagSet.Usage() flagSet.Usage()
} }

View file

@ -3,20 +3,13 @@ package whoami
import( import(
"os" "os"
"os/user" "os/user"
"flag"
"fmt" "fmt"
"log" "log"
"github.com/surdeus/gomtool/src/mtool"
) )
func Run(args []string) { func Run(flagSet *mtool.Flags) {
arg0 := args[0] flagSet.Parse()
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)
if len(flagSet.Args())>0 { if len(flagSet.Args())>0 {
flagSet.Usage() flagSet.Usage()