From 809e4b33341520054e6a6f052bb74cc017d60465 Mon Sep 17 00:00:00 2001 From: Ochi Daiki Date: Sat, 17 Jun 2023 19:29:28 +0900 Subject: [PATCH] =?UTF-8?q?Impl=20"gocode"=20subcommand=20for=20outputting?= =?UTF-8?q?=20sample=20Go=20code=20using=20"gtree"=20=E2=80=A6=20(#175)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Impl "gocode" subcommand for outputting sample Go code using "gtree" package * nits * Goパッケージの依存関係リストからツリー生成するGoプログラムを出力 * update README * update README --- .gitignore | 2 + README.md | 20 ++++++-- cmd/gtree/go_code.go | 107 +++++++++++++++++++++++++++++++++++++++++++ cmd/gtree/main.go | 23 ++++++++++ 4 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 cmd/gtree/go_code.go diff --git a/.gitignore b/.gitignore index f065ce1..057dc5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ tmp.md +go_dependences.txt +tmp/ coverage.* gtreetest/ root/ diff --git a/README.md b/README.md index 73446b3..28d9cbe 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,7 @@ COMMANDS: mkdir, m Make directories(and files) from markdown. It is possible to dry run. Let's try 'gtree template | gtree mkdir -e .go -e .md -e Makefile'. template, t, tmpl Output markdown template. web, w, www Open "Tree Maker" in your browser. If it doesn't open, it will display the url. + gocode, gc, code Output a sample Go program calling "gtree" package. version, v Output gtree version. help, h Shows a list of commands or help for one command @@ -737,7 +738,8 @@ func preparePrimate() *gtree.Node { --- -- The program below converts the result of `find` into a tree. +#### The program below converts the result of `find` into a tree. + ```go package main @@ -817,8 +819,20 @@ func main() { ``` -- Convert `go list -deps ./...` to tree 👉 [link](https://github.com/ddddddO/gtree/blob/master/example/go-list_pipe_programmable-gtree/main.go) - - inspired by [nikolaydubina/go-recipes](https://github.com/nikolaydubina/go-recipes#readme) ! +- The above Go program can be output with the command below. + + ```console + $ gtree gocode + ``` + + +#### Convert `go list -deps ./...` to tree 👉 [link](https://github.com/ddddddO/gtree/blob/master/example/go-list_pipe_programmable-gtree/main.go) + +- The above Go program can be output with the command below. + ```console + $ gtree gocode --godeps-to-tree + ``` +- inspired by [nikolaydubina/go-recipes](https://github.com/nikolaydubina/go-recipes#readme) ! ### *MkdirProgrammably* func diff --git a/cmd/gtree/go_code.go b/cmd/gtree/go_code.go new file mode 100644 index 0000000..9a9d07c --- /dev/null +++ b/cmd/gtree/go_code.go @@ -0,0 +1,107 @@ +package main + +import ( + "fmt" + "strings" +) + +type gocode string + +func (g gocode) print() error { + _, err := fmt.Print(strings.Trim(string(g), "\n")) + return err +} + +func (g gocode) println() error { + if err := g.print(); err != nil { + return err + } + _, err := fmt.Println() + return err +} + +const findToTree gocode = ` +package main + +import ( + "bufio" + "fmt" + "os" + "strings" + + "github.com/ddddddO/gtree" +) + +// $ gtree gocode > find_to_tree.go && go mod init xxx 2>/dev/null && go mod tidy 2>/dev/null && find . -type d -o -type f -print | go run find_to_tree.go +func main() { + var ( + root *gtree.Node + node *gtree.Node + ) + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + line := scanner.Text() + splited := strings.Split(line, "/") + + for i, s := range splited { + if root == nil { + root = gtree.NewRoot(s) + node = root + continue + } + if i == 0 { + continue + } + + tmp := node.Add(s) + node = tmp + } + node = root + } + + if err := gtree.OutputProgrammably(os.Stdout, root); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} +` + +const goDependencesToTree gocode = ` +package main + +import ( + "bufio" + "fmt" + "os" + "strings" + + "github.com/ddddddO/gtree" +) + +// $ ls | grep go.mod && go list -deps ./path/to/go_dir > go_dependences.txt +// $ mkdir tmp && cd tmp && gtree gocode --godeps-to-tree > godeps_to_tree.go && go mod init xxx 2>/dev/null && go mod tidy 2>/dev/null && cat ../go_dependences.txt | go run godeps_to_tree.go +func main() { + var ( + root = gtree.NewRoot("[All Dependencies]") + node *gtree.Node + ) + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + line := scanner.Text() + splited := strings.Split(line, "/") + + for i, s := range splited { + if i == 0 { + node = root.Add(s) + continue + } + node = node.Add(s) + } + } + + if err := gtree.OutputProgrammably(os.Stdout, root); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} +` diff --git a/cmd/gtree/main.go b/cmd/gtree/main.go index 8d43074..c0af64f 100644 --- a/cmd/gtree/main.go +++ b/cmd/gtree/main.go @@ -92,6 +92,13 @@ func main() { }, } + gocodeFlags := []cli.Flag{ + &cli.BoolFlag{ + Name: "godeps-to-tree", + Usage: "Output Go program to convert Go package dependency list to tree.", + }, + } + app := &cli.App{ Name: "gtree", Usage: "This CLI generates directory trees and the directories itself using Markdown.", @@ -128,6 +135,14 @@ func main() { Before: notExistArgs, Action: actionWeb, }, + { + Name: "gocode", + Aliases: []string{"gc", "code"}, + Usage: "Output a sample Go program calling \"gtree\" package.", + Flags: gocodeFlags, + Before: notExistArgs, + Action: actionGoCode, + }, { Name: "version", Aliases: []string{"v"}, @@ -247,3 +262,11 @@ func actionWeb(c *cli.Context) error { return nil } + +func actionGoCode(c *cli.Context) error { + if c.Bool("godeps-to-tree") { + return goDependencesToTree.println() + } + + return findToTree.println() +}