Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
174 lines (116 sloc) 4.39 KB
My golang CLI application for writing CLI application
GopherCon 2016 LT
11 Jul 2016
Taichi Nakashima
@deeeet
* About me
.image img/deeeet.png 200 _
- Taichi Nakashima
- Tokyo, Japan 🇯🇵
- *@deeeet* (Twitter) / *@tcnksm* (GitHub)
# My name is Taichi Nakashima. I'm from Tokyo Japan
# This is my twitter & github account
* TL;DR
# In this talk, I will
Introduce my golang CLI applications that help developing golang CLI application
* My golang CLI applications
# I will share these tools
- [[https://github.com/tcnksm/gcli][gcli]]
- [[https://github.com/tcnksm/ghr][ghr]]
* gcli
# First one is gcli
# This name comes from
G(enerate)CLI
Generate a skeleton (codes and its directory structure) project you need to start building CLI application right out of the box.
You can choose a famous CLI framework like [[https://github.com/urfave/cli][codegangsta/cli]] (now `urfave/cli`), [[https://github.com/mitchellh/cli][mitchellh/cli]] and so on. You can also generate without framework, same struct and methond go command has.
*Why?*
# When you write CLI application in Golang,
# you may notice everytime you're writing a lot of boilerplate codes.
# For example, subcommand interface definition is always same, just name is different, right ?
#
# I want to avoid writing common code everytime,
# and focus on function part.
#
# That's why I wrote this tool.
To focus on function part of application instead of writing the chunk of boilerplate codes (especially subcommand interface definition)
* gcli
# Let me show a simple demo
# In this demo, I will generate
_DEMO_ - generating a `todo` CLI application which has `add`, `list` and `delete` subcommand with `codegangsta/cli` package.
.image img/gcli.gif 400 _
# As you can see it generates go codes with test and directory structure,
# It also generates README.md and .gitignore file and so on.
#
# It's go build able from beginning
# Let me do that.
#
# Now there is a binary and you can run it
# Let's show the help messages.
#
# It shows it has add and list, delete command
#
# Now you can start to write actual TODO application function part
# cd $GOPATH/src/github.com/tcnksm/
# gcli new -c add -c list -c delete todo
# cd todo
# go build
# ./todo -h
* gcli
You can also generate a project from `.toml` file.
# sample.toml
Name = "todo"
Owner = "tcnksm"
Version = "0.1.0"
Description = "Manage TODO tasks from command line"
[[Commands]]
Name = "add"
Synopsis = "Add new task"
Help = "[Usage] todo [option] add TASK"
...
And run the following command,
$ gcli apply sample.toml
* gcli
# You can do more things, please vist to my github account
See more on [[https://github.com/tcnksm/gcli][https://github.com/tcnksm/gcli]]
* ghr
G(it)H(ub)R(elease)
Upload multiple artifacts to GitHub Release page in parallel with *one* command
*Why?*
# When you release your CLI application to user, How do you do that ?
# I think you will cross-compile it for multiple platform like linux, windows or darwin
# and upload them to GitHub Release page, bintray.com, S3 or something like that.
# Because it's one of the biggest because of golang, right?
#
# Personally, I prefer to use Github Release page,
# so I made ghr to simplify cross-compile binaries release procedure.
To simplify cross-compiled binaries release procedure
* ghr
# Let me show another demo for this, I will
_DEMO_ - uploading cross-compiled binaries
.image img/ghr.gif 400 _
# I have a very simple application, it just display "Hello,GHR"
# and I already cross-compile it for darwin and linux and windows.
#
# You can upload them by
# ghr with its release version, directory where binearies are
# cat main.go
# gox -os="darwin linux windows" -output "out/{{.Dir}}_{{.OS}}_{{.Arch}}"
# ls out/
# ghr v1.0.0 out/
# After it's done, this kind of release page is created and
# binaries are ready to download
* ghr
You can run it not only on your local PC but on CI service like Travis or Werker.
e.g., on werker you can run `go test`. If it's passed, you can cross-copmpile and release binearies
# to your GitHub release page by ghr.
# wercker.yml
deploy:
steps:
- tcnksm/ghr:
token: $GITHUB_TOKEN
input: dist
* ghr
See more on [[https://github.com/tcnksm/ghr][https://github.com/tcnksm/ghr]]
* Conclution
I hope these tools help you to write golang CLI application.
If you are interested, please visit [[https://github.com/tcnksm][github.com/tcnksm]]