Main directory becomes the library. go-bindata subdir holds

the command code.
pull/4/head
Jim Teeuwen 2013-10-29 16:17:14 +01:00
parent 6187b52beb
commit 560fbff5df
3 changed files with 151 additions and 0 deletions

106
go-bindata/config.go Normal file
View File

@ -0,0 +1,106 @@
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0/
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)
// Config defines command line options.
type Config struct {
Input string // Input directory with assets.
Output string // Output directory for generated code.
Tags []string // Build tags to include in output files.
}
// NewConfig create s anew, filled configuration instance
// by reading and parsing command line options.
//
// This function exits the program with an error, if
// any of the command line options are incorrect.
func NewConfig() *Config {
var version bool
var tagstr string
c := new(Config)
flag.Usage = func() {
fmt.Printf("Usage: %s [options] <input> [<output>]\n\n", os.Args[0])
flag.PrintDefaults()
}
flag.StringVar(&tagstr, "tags", "", "Comma-separated list of build tags to include.")
flag.BoolVar(&version, "version", false, "Displays version information.")
flag.Parse()
if version {
fmt.Printf("%s\n", Version())
os.Exit(0)
}
// Make sure we have in/output paths.
if flag.NArg() == 0 {
fmt.Fprintf(os.Stderr, "Missing asset directory.\n")
os.Exit(1)
}
// Test validity of input path.
c.Input = filepath.Clean(flag.Arg(0))
stat, err := os.Lstat(c.Input)
if err != nil {
fmt.Fprintf(os.Stderr, "Input path: %v.\n", err)
os.Exit(1)
}
if !stat.IsDir() {
fmt.Fprintf(os.Stderr, "Input path is not a directory.\n")
os.Exit(1)
}
// Find and test validity of output path.
if flag.NArg() > 1 {
c.Output = filepath.Clean(flag.Arg(1))
stat, err := os.Lstat(c.Output)
if err != nil {
if !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Output path: %v.\n", err)
os.Exit(1)
}
// Create output directory
err = os.MkdirAll(c.Output, 0744)
if err != nil {
fmt.Fprintf(os.Stderr, "Create Output directory: %v.\n", err)
os.Exit(1)
}
} else if !stat.IsDir() {
fmt.Fprintf(os.Stderr, "Output path is not a directory.\n")
os.Exit(1)
}
} else {
// If no output path is specified, use the current directory.
c.Output, err = os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to determine current working directory: %v\n", err)
os.Exit(1)
}
}
// Process build tags.
if len(tagstr) > 0 {
c.Tags = strings.Split(tagstr, ",")
} else {
c.Tags = append(c.Tags, "debug")
}
return c
}

14
go-bindata/main.go Normal file
View File

@ -0,0 +1,14 @@
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0/
package main
import (
"fmt"
)
func main() {
cfg := NewConfig()
fmt.Println(cfg)
}

31
go-bindata/version.go Normal file
View File

@ -0,0 +1,31 @@
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0/
package main
import (
"fmt"
"runtime"
)
const (
AppName = "bindata"
AppVersionMajor = 3
AppVersionMinor = 1
)
// revision part of the program version.
// This will be set automatically at build time like so:
//
// go build -ldflags "-X main.AppVersionRev `date -u +%s`"
var AppVersionRev string
func Version() string {
if len(AppVersionRev) == 0 {
AppVersionRev = "0"
}
return fmt.Sprintf("%s %d.%d.%s (Go runtime %s).\nCopyright (c) 2010-2013, Jim Teeuwen.",
AppName, AppVersionMajor, AppVersionMinor, AppVersionRev, runtime.Version())
}