Skip to content

Commit

Permalink
Merge pull request #20 from tcnksm/bash-init
Browse files Browse the repository at this point in the history
Generate bash script skeleton (experiment)
  • Loading branch information
tcnksm committed Jun 29, 2015
2 parents 73b8907 + 6262b55 commit cba8714
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
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

0 comments on commit cba8714

Please sign in to comment.