Skip to content

Commit

Permalink
Impl "web" subcommand for opening TreeMaker (#174)
Browse files Browse the repository at this point in the history
* impl "web" subcommand for opening TreeMaker

* fix windows

* wsl用にブラウザで開きたいため

* update help

* update readme

* nits
  • Loading branch information
ddddddO authored Jun 17, 2023
1 parent be4755c commit 1048e2f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ Also, once loaded, you can enjoy offline!<br>

![](web_example.gif)

You can open it in your browser with
```console
$ gtree web
```

[source code](cmd/gtree-wasm/)


Expand Down Expand Up @@ -111,7 +116,7 @@ gtree
```console
$ gtree --help
NAME:
gtree - This CLI outputs tree or makes directories from markdown.
gtree - This CLI generates directory trees and the directories itself using Markdown.

USAGE:
gtree [global options] command [command options] [arguments...]
Expand All @@ -120,11 +125,12 @@ COMMANDS:
output, o, out Output tree from markdown. Let's try 'gtree template | gtree output'. Output format is tree or yaml or toml or json. Default tree.
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.
version, v Output gtree version.
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--help, -h show help (default: false)
--help, -h show help
```

- The symbols that can be used in Markdown are `*`, `-`, `+`, and `#`.
Expand Down
28 changes: 27 additions & 1 deletion cmd/gtree/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,16 @@ func main() {
},
}

webFlags := []cli.Flag{
&cli.BoolFlag{
Name: "wsl",
Usage: "Specify this option if you are using WSL.",
},
}

app := &cli.App{
Name: "gtree",
Usage: "This CLI outputs tree or makes directories from markdown.",
Usage: "This CLI generates directory trees and the directories itself using Markdown.",
Commands: []*cli.Command{
{
Name: "output",
Expand All @@ -113,6 +120,14 @@ func main() {
Before: notExistArgs,
Action: actionTemplate,
},
{
Name: "web",
Aliases: []string{"w", "www"},
Usage: "Open \"Tree Maker\" in your browser. If it doesn't open, it will display the url.",
Flags: webFlags,
Before: notExistArgs,
Action: actionWeb,
},
{
Name: "version",
Aliases: []string{"v"},
Expand Down Expand Up @@ -221,3 +236,14 @@ func actionTemplate(c *cli.Context) error {

return directory.println()
}

const treeMakerURL = "https://ddddddo.github.io/gtree/"

func actionWeb(c *cli.Context) error {
if err := openWeb(treeMakerURL, c.Bool("wsl")); err != nil {
fmt.Println(treeMakerURL)
return nil
}

return nil
}
49 changes: 49 additions & 0 deletions cmd/gtree/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"errors"
"os/exec"
"runtime"
)

var openFuncs = map[string]func(string) error{
"windows": openWebByWindows,
"linux": openWebByLinux,
"wsl": openWebByWSL,
"darwin": openWebByMac,
}

func openWeb(url string, isWSL bool) error {
runtimeOS := runtime.GOOS
if isWSL {
runtimeOS = "wsl"
}

openFunc, ok := openFuncs[runtimeOS]
if !ok {
return errors.New("That OS is not yet supported....")
}
return openFunc(url)
}

func openWebByWindows(url string) error {
_, err := exec.Command("cmd.exe", "/c", "start", url).CombinedOutput()
return err
}

func openWebByLinux(url string) error {
// TODO: インタラクティブモードで開かれた場合どうするか。errはnil
// 戻り値に返ってきてるっぽい
_, err := exec.Command("xdg-open", url).CombinedOutput()
return err
}

func openWebByWSL(url string) error {
_, err := exec.Command("cmd.exe", "/c", "start", url).CombinedOutput()
return err
}

func openWebByMac(url string) error {
_, err := exec.Command("open", url).CombinedOutput()
return err
}

0 comments on commit 1048e2f

Please sign in to comment.