Skip to content

Commit

Permalink
Impl "gocode" subcommand for outputting sample Go code using "gtree" … (
Browse files Browse the repository at this point in the history
#175)

* Impl "gocode" subcommand for outputting sample Go code using "gtree" package

* nits

* Goパッケージの依存関係リストからツリー生成するGoプログラムを出力

* update README

* update README
  • Loading branch information
ddddddO authored Jun 17, 2023
1 parent 1048e2f commit 809e4b3
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
tmp.md
go_dependences.txt
tmp/
coverage.*
gtreetest/
root/
Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
107 changes: 107 additions & 0 deletions cmd/gtree/go_code.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
`
23 changes: 23 additions & 0 deletions cmd/gtree/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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()
}

0 comments on commit 809e4b3

Please sign in to comment.