From 560fbff5df05a36b3dba04a597796108b6f21397 Mon Sep 17 00:00:00 2001 From: Jim Teeuwen Date: Tue, 29 Oct 2013 16:17:14 +0100 Subject: [PATCH] Main directory becomes the library. go-bindata subdir holds the command code. --- go-bindata/config.go | 106 ++++++++++++++++++++++++++++++++++++++++++ go-bindata/main.go | 14 ++++++ go-bindata/version.go | 31 ++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 go-bindata/config.go create mode 100644 go-bindata/main.go create mode 100644 go-bindata/version.go diff --git a/go-bindata/config.go b/go-bindata/config.go new file mode 100644 index 0000000..d5cdaf5 --- /dev/null +++ b/go-bindata/config.go @@ -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] []\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 +} diff --git a/go-bindata/main.go b/go-bindata/main.go new file mode 100644 index 0000000..1ebfade --- /dev/null +++ b/go-bindata/main.go @@ -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) +} diff --git a/go-bindata/version.go b/go-bindata/version.go new file mode 100644 index 0000000..25e75cd --- /dev/null +++ b/go-bindata/version.go @@ -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()) +}