Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate bash script skeleton (experiment) #20

Merged
merged 4 commits into from
Jun 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This is road map of `gcli`, what I'm doing for next release and planing for futu
- `godoc`
- More document for flag
- Global option for command pattern
- **Done**: Generate bash script
- **Done**: More verbose outputs in `skeleton` package
- **Done** `go vet` tests for artifacts
- **Done**: `list` command
Expand Down
3 changes: 3 additions & 0 deletions command/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func (c *ListCommand) Run(args []string) int {
header := []string{"Name", "Command", "URL"}
table.SetHeader(header)
for _, f := range skeleton.Frameworks {
if f.Hide {
continue
}
var cmd string
if len(f.CommandTemplates) > 0 {
cmd = "*"
Expand Down
2 changes: 1 addition & 1 deletion command/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (c *NewCommand) Run(args []string) int {
case artifact := <-artifactCh:
c.UI.Output(fmt.Sprintf(" Created %s", artifact))
case err := <-errCh:
c.UI.Error("Failed to generate %s: " + err.Error())
c.UI.Error(fmt.Sprintf("Failed to generate %s: %s", output, err.Error()))

// If some file are created before error happend
// Should be cleanuped
Expand Down
16 changes: 16 additions & 0 deletions skeleton/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type Framework struct {

// CommandTemplate
CommandTemplates []Template

// If Hide is true, `list` command doesn't show
// this framework
Hide bool
}

// CommonTemplates is collection of templates which are used all frameworks.
Expand Down Expand Up @@ -83,6 +87,18 @@ The goal is to enable developers to write fast and distributable command line ap
},
},

{
Name: "bash",
URL: "",
Description: `
`,
BaseTemplates: []Template{
{"resource/tmpl/bash/main.sh.tmpl", "{{ .Name }}.sh"},
},
CommandTemplates: []Template{},
Hide: true,
},

{
Name: "flag",
AltNames: []string{},
Expand Down
101 changes: 101 additions & 0 deletions skeleton/resource/tmpl/bash/main.sh.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/bin/bash
#
# {{ .Name }}
#
# Copyright (c) {{ date }} {{.Owner}}
#

readonly PROGNAME=$(basename $0)
readonly COMMAND=$1

usage() {
cat <<EOF
Name:
${PROGNAME}

Usage:
${PROGNAME} command [arguments...]

Commands:
{{range .Commands}}{{.Name}} {{ .Synopsis }}
{{end}}help show help
version print ${PROGNAME} version

EOF
}

version() {
echo "${PROGNAME} version {{ .Version }}"
}

log() {
echo "$(date "+%Y/%m/%m %H:%M:%S") $@"
}

debug() {
is_not_empty $DEBUG && log $@
}

info() {
echo -e "\033[34m$@\033[m" # blue
}

warn() {
echo -e "\033[33m$@\033[m" # yellow
}

error() {
echo -e "\033[31m$@\033[m" # red
}

is_empty() {
local var=$1

[[ -z $var ]]
}

is_not_empty() {
local var=$1

[[ -n $var ]]
}

is_file() {
local file=$1

[[ -f $file ]]
}

is_dir() {
local dir=$1

[[ -d $dir ]]
}

{{range .Commands}}
{{ .Name }}() {
debug $FUNCNAME $@
}
{{end}}

case "${COMMAND}" in

{{range .Commands}}{{ .Name }})
{{.Name}}
;;
{{end}}
version|v|-v|--version)
version
;;

help|usage|h|-h|--help)
usage
;;

*)
error "[Error] Invalid command '${COMMAND}'"
echo "Run '${PROGNAME} help' for usage."
exit 1
esac

exit 0