minio/cmd/minio-cli/minio-cli.go

158 lines
3.4 KiB
Go
Raw Normal View History

2014-12-18 05:18:02 -05:00
package main
import (
"flag"
"log"
"os"
"path"
"strings"
"text/template"
"time"
"github.com/minio-io/minio/pkgs/utils"
)
type source struct {
Name string
TempLate template.Template
}
const (
// Relative path from GOPATH default
TEMPLATEREPO = "/src/github.com/minio-io/minio/templates/"
)
type option struct {
Name string
Definename string
Functionname string
}
type command struct {
2014-12-18 05:18:02 -05:00
Name string
Usage string
Month string
Year int
Options []option
}
func (f source) get(commandName string, definition command) error {
wr, err := os.Create(path.Join(commandName, f.Name))
2014-12-18 05:18:02 -05:00
if err != nil {
return err
}
defer wr.Close()
return f.TempLate.Execute(wr, definition)
2014-12-18 05:18:02 -05:00
}
func initCommand(commandname, usage string, inputOptions []string) command {
2014-12-18 05:18:02 -05:00
year, month, _ := time.Now().Date()
return command{
Name: commandname,
2014-12-18 05:18:02 -05:00
Usage: usage,
Month: month.String(),
Year: year,
Options: initOptions(inputOptions),
}
}
func initOptions(inputOptions []string) []option {
var options []option
if inputOptions[0] == "" {
return options
}
for _, name := range inputOptions {
option := option{
Name: name,
Definename: utils.FirstUpper(name),
Functionname: "do" + utils.FirstUpper(name),
}
options = append(options, option)
}
return options
}
func main() {
var flOptions, flUsage, flTemplatePath string
flag.StringVar(&flOptions, "options", "", "Comma-separated list of options to build")
flag.StringVar(&flUsage, "usage", "", "A one liner explains the purpose of the cli being built")
flag.StringVar(&flTemplatePath, "templatepath", "", "Non standard templates path")
flag.Parse()
inputOptions := strings.Split(flOptions, ",")
commandname := flag.Arg(0)
2014-12-18 05:18:02 -05:00
if commandname == "" {
log.Fatal("command name must not be blank\n")
2014-12-18 05:18:02 -05:00
}
if inputOptions[0] == "" {
log.Fatal("-options option1 should be specified with command name")
2014-12-18 05:18:02 -05:00
}
gopath := os.Getenv("GOPATH")
var mainTemplatePath, optionsTemplatePath, readmeTemplatePath string
if flTemplatePath == "" {
mainTemplatePath = path.Join(gopath, TEMPLATEREPO, "main.tmpl")
optionsTemplatePath = path.Join(gopath, TEMPLATEREPO, "options.tmpl")
readmeTemplatePath = path.Join(gopath, TEMPLATEREPO, "README.tmpl")
} else {
mainTemplatePath = path.Join(flTemplatePath, "main.tmpl")
optionsTemplatePath = path.Join(flTemplatePath, "options.tmpl")
readmeTemplatePath = path.Join(flTemplatePath, "README.tmpl")
}
if _, err := os.Stat(mainTemplatePath); err != nil {
log.Fatal(err)
}
if _, err := os.Stat(optionsTemplatePath); err != nil {
log.Fatal(err)
}
if _, err := os.Stat(readmeTemplatePath); err != nil {
log.Fatal(err)
}
var mainTemplate = template.Must(template.ParseFiles(mainTemplatePath))
var optionsTemplate = template.Must(template.ParseFiles(optionsTemplatePath))
var readmeTemplate = template.Must(template.ParseFiles(readmeTemplatePath))
err := os.Mkdir(commandname, 0755)
2014-12-18 05:18:02 -05:00
utils.Assert(err)
command := initCommand(commandname, flUsage, inputOptions)
2014-12-18 05:18:02 -05:00
optionsGo := source{
Name: commandname + "-options.go",
2014-12-18 05:18:02 -05:00
TempLate: *optionsTemplate,
}
readmeMd := source{
Name: commandname + ".md",
2014-12-18 05:18:02 -05:00
TempLate: *readmeTemplate,
}
mainGo := source{
Name: commandname + ".go",
2014-12-18 05:18:02 -05:00
TempLate: *mainTemplate,
}
err = readmeMd.get(commandname, command)
2014-12-18 05:18:02 -05:00
utils.Assert(err)
mainGo.get(commandname, command)
2014-12-18 05:18:02 -05:00
utils.Assert(err)
optionsGo.get(commandname, command)
2014-12-18 05:18:02 -05:00
err = GoFormat(commandname)
2014-12-18 05:18:02 -05:00
utils.Assert(err)
}