commit 023d1559d777c60dad3d6c07bd180381744c8938 Author: k1574 Date: Thu Jun 16 11:02:24 2022 +0500 Init. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3b3b760 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/k1574/gomtool + +go 1.18 diff --git a/m/cmd/test/main.go b/m/cmd/test/main.go new file mode 100644 index 0000000..b6b439b --- /dev/null +++ b/m/cmd/test/main.go @@ -0,0 +1,25 @@ +package main + +import( + "github.com/k1574/gomtool/m/multitool" + "strconv" + "fmt" +) + +var( + tools = multitool.Tools{ + "echo" : func(args []string) { + fmt.Println(args) + }, + "sum" : func(args []string) { + one, _ := strconv.Atoi(args[1]) + two, _ := strconv.Atoi(args[2]) + fmt.Println(one + two) + }, + } +) + +func main() { + multitool.Main("test", tools) +} + diff --git a/m/multitool/main.go b/m/multitool/main.go new file mode 100644 index 0000000..21d5f21 --- /dev/null +++ b/m/multitool/main.go @@ -0,0 +1,39 @@ +package multitool + +import( + "fmt" + "os" + "path" +) + + +type Tools map[string] func(args []string) + +func Main(name string, m Tools) { + var( + utilName string + args []string + ) + + if binBase := path.Base(os.Args[0]) ; binBase != name { + utilName = binBase + args = os.Args[:] + } else { + if len(os.Args)<2 { + for k, _ := range m { + fmt.Printf("%s\n", k) + } + os.Exit(0) + } + utilName = os.Args[1] + args = os.Args[1:] + } + + if _, ok := m[utilName] ; !ok { + fmt.Printf("%s: No such uitl as '%s'.\n", os.Args[0], utilName ) + os.Exit(1) + } + + m[utilName](args) +} + diff --git a/readme b/readme new file mode 100644 index 0000000..499d113 --- /dev/null +++ b/readme @@ -0,0 +1,4 @@ +# gomtool + +Go multitool package. Suits the task of multiple programs in one like in "goblin". +