From 023d1559d777c60dad3d6c07bd180381744c8938 Mon Sep 17 00:00:00 2001 From: k1574 Date: Thu, 16 Jun 2022 11:02:24 +0500 Subject: [PATCH] Init. --- go.mod | 3 +++ m/cmd/test/main.go | 25 +++++++++++++++++++++++++ m/multitool/main.go | 39 +++++++++++++++++++++++++++++++++++++++ readme | 4 ++++ 4 files changed, 71 insertions(+) create mode 100644 go.mod create mode 100644 m/cmd/test/main.go create mode 100644 m/multitool/main.go create mode 100644 readme 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". +