Blame SOURCES/golist-bootstrap-cli-no-vendor.patch

ecdb77
diff --git a/cmd/golist/golist.go b/cmd/golist/golist.go
ecdb77
index ee028ae..e9c038d 100644
ecdb77
--- a/cmd/golist/golist.go
ecdb77
+++ b/cmd/golist/golist.go
ecdb77
@@ -11,9 +11,8 @@ import (
ecdb77
 	"strings"
ecdb77
 	"text/template"
ecdb77
 
ecdb77
-	"github.com/urfave/cli"
ecdb77
-
ecdb77
 	"pagure.io/golist/pkg/util"
ecdb77
+	"pagure.io/golist/pkg/cli"
ecdb77
 )
ecdb77
 
ecdb77
 var (
ecdb77
@@ -100,7 +99,6 @@ func main() {
ecdb77
 	}
ecdb77
 
ecdb77
 	app.Action = func(c *cli.Context) error {
ecdb77
-
ecdb77
 		if len(c.StringSlice("package-path")) == 0 {
ecdb77
 			return fmt.Errorf("--package-path is not set")
ecdb77
 		}
ecdb77
diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go
ecdb77
new file mode 100644
ecdb77
index 0000000..ec91056
ecdb77
--- /dev/null
ecdb77
+++ b/pkg/cli/cli.go
ecdb77
@@ -0,0 +1,293 @@
ecdb77
+package cli
ecdb77
+
ecdb77
+/* golist uses a very small portion of functionality
ecdb77
+   from github.com/urfave/cli. This module provides
ecdb77
+   minimal substitute API implementations for only the
ecdb77
+   core functionality used by golist, for the purpose of
ecdb77
+   bootstrapping golist without additional dependencies.
ecdb77
+*/
ecdb77
+
ecdb77
+import (
ecdb77
+	"strings"
ecdb77
+	"fmt"
ecdb77
+	"os"
ecdb77
+)
ecdb77
+
ecdb77
+type String string
ecdb77
+type StringSlice []string
ecdb77
+type Bool bool
ecdb77
+
ecdb77
+type Flag interface {
ecdb77
+	name() string
ecdb77
+	usage() string
ecdb77
+}
ecdb77
+
ecdb77
+type BoolFlag struct {
ecdb77
+	Name string
ecdb77
+	Usage string
ecdb77
+	Value bool
ecdb77
+}
ecdb77
+
ecdb77
+type StringFlag struct {
ecdb77
+	Name string
ecdb77
+	Usage string
ecdb77
+	Value string
ecdb77
+}
ecdb77
+
ecdb77
+type StringSliceFlag struct {
ecdb77
+	Name string
ecdb77
+	Usage string
ecdb77
+	Value *StringSlice
ecdb77
+}
ecdb77
+
ecdb77
+type App struct {
ecdb77
+	Name string
ecdb77
+	Usage string
ecdb77
+	Version string
ecdb77
+	Flags []Flag
ecdb77
+	Action func(*Context) error
ecdb77
+
ecdb77
+}
ecdb77
+
ecdb77
+func NewApp() App {
ecdb77
+	var a App
ecdb77
+	return a
ecdb77
+}
ecdb77
+
ecdb77
+func (a *App) Run(osArgs []string) error {
ecdb77
+	c, err := newContext(a.Flags, osArgs)
ecdb77
+	if err != nil {
ecdb77
+		return err
ecdb77
+	}
ecdb77
+	if c.Bool("help") {
ecdb77
+		a.PrintHelp()
ecdb77
+		os.Exit(0)
ecdb77
+	}
ecdb77
+	return a.Action(c)
ecdb77
+}
ecdb77
+
ecdb77
+func (a *App) PrintHelp() {
ecdb77
+	maxNameLen := 0
ecdb77
+	for _, flag := range a.Flags {
ecdb77
+		length := len(flag.name())
ecdb77
+		if length > maxNameLen {
ecdb77
+			maxNameLen = length
ecdb77
+		}
ecdb77
+	}
ecdb77
+	fmtSpec := "%-" + fmt.Sprintf("%v", maxNameLen + 6) + "s\t%s\n"
ecdb77
+	fmt.Printf("%s - %s\n", a.Name, a.Usage)
ecdb77
+	fmt.Printf("Options:\n")
ecdb77
+	for _, flag := range a.Flags {
ecdb77
+		flagNameSlice := append([]string{canonicalName(flag)}, alternateNames(flag)...)
ecdb77
+		for i, _ := range flagNameSlice {
ecdb77
+			if len(flagNameSlice[i]) > 1 {
ecdb77
+				flagNameSlice[i] = fmt.Sprintf("--%s", flagNameSlice[i])
ecdb77
+			} else {
ecdb77
+				flagNameSlice[i] = fmt.Sprintf("-%s", flagNameSlice[i])
ecdb77
+			}
ecdb77
+		}
ecdb77
+		flagNameStr := strings.Join(flagNameSlice, ", ")
ecdb77
+		switch flag.(type) {
ecdb77
+			case StringFlag:
ecdb77
+				flagNameStr = fmt.Sprintf("  %s value", flagNameStr)
ecdb77
+			case StringSliceFlag:
ecdb77
+				flagNameStr = fmt.Sprintf("  %s value", flagNameStr)
ecdb77
+			case BoolFlag:
ecdb77
+				flagNameStr = fmt.Sprintf("  %s", flagNameStr)
ecdb77
+		}
ecdb77
+		fmt.Printf(fmtSpec, flagNameStr, flag.usage())
ecdb77
+	}
ecdb77
+}
ecdb77
+
ecdb77
+
ecdb77
+type Context struct {
ecdb77
+	flagValues map[string]interface{}
ecdb77
+	flagDecls map[string]Flag
ecdb77
+	altFlags map[string]string
ecdb77
+	positionalArgs []string
ecdb77
+}
ecdb77
+
ecdb77
+func (c *Context) Bool(flag string) bool {
ecdb77
+	iface, ok := c.flagDecls[flag]
ecdb77
+	if !ok {
ecdb77
+		panic("undefined flag" + flag)
ecdb77
+	}
ecdb77
+	switch iface.(type) {
ecdb77
+		case BoolFlag:
ecdb77
+			break
ecdb77
+		default:
ecdb77
+			panic("flag type mismatch - expected BoolFlag, got: " + flag)
ecdb77
+	}
ecdb77
+	val, ok := c.flagValues[flag]
ecdb77
+	if !ok {
ecdb77
+		return iface.(BoolFlag).Value
ecdb77
+	}
ecdb77
+	return val.(bool)
ecdb77
+}
ecdb77
+
ecdb77
+func (c *Context) String(flag string) string {
ecdb77
+	iface, ok := c.flagDecls[flag]
ecdb77
+	if !ok {
ecdb77
+		panic("undefined flag" + flag)
ecdb77
+	}
ecdb77
+	switch iface.(type) {
ecdb77
+		case StringFlag:
ecdb77
+			break
ecdb77
+		default:
ecdb77
+			panic("flag type mismatch - expected StringFlag, got: " + flag)
ecdb77
+	}
ecdb77
+	val, ok:= c.flagValues[flag]
ecdb77
+	if !ok {
ecdb77
+		return iface.(StringFlag).Value
ecdb77
+	}
ecdb77
+	return val.(string)
ecdb77
+}
ecdb77
+
ecdb77
+func (c *Context) StringSlice(flag string) []string {
ecdb77
+	iface, ok := c.flagDecls[flag];
ecdb77
+	if !ok {
ecdb77
+		panic("undefined flag" + flag)
ecdb77
+	}
ecdb77
+	switch iface.(type) {
ecdb77
+		case StringSliceFlag:
ecdb77
+			break
ecdb77
+		default:
ecdb77
+			panic("flag type mismatch - expected StringSliceFlag, got: " + flag)
ecdb77
+	}
ecdb77
+	val, ok := c.flagValues[flag]
ecdb77
+	if !ok {
ecdb77
+		val = iface.(StringSliceFlag).Value
ecdb77
+		if val != nil {
ecdb77
+			return []string{}
ecdb77
+		}
ecdb77
+	}
ecdb77
+	return val.([]string)
ecdb77
+}
ecdb77
+
ecdb77
+// Create a hash mapping from flag names to declarations
ecdb77
+// and alt names to flag names.
ecdb77
+func (c *Context) setupFlags(flagDecls []Flag) error {
ecdb77
+	helpFlag := BoolFlag {
ecdb77
+		Name: "help, h",
ecdb77
+		Usage: "Show help message",
ecdb77
+	}
ecdb77
+	flagDecls = append(flagDecls, helpFlag)
ecdb77
+	for _, flag := range flagDecls {
ecdb77
+		flagName := canonicalName(flag)
ecdb77
+		if _, ok:= c.flagDecls[flagName]; ok {
ecdb77
+			return fmt.Errorf("cannot redeclare flag: %s", flagName)
ecdb77
+		}
ecdb77
+		c.flagDecls[flagName] = flag
ecdb77
+		altFlagNames := alternateNames(flag)
ecdb77
+		for _, altName := range altFlagNames {
ecdb77
+			c.altFlags[altName] = flagName
ecdb77
+		}
ecdb77
+	}
ecdb77
+	return nil
ecdb77
+}
ecdb77
+
ecdb77
+func (c *Context) parseArgs(osArgs []string) error {
ecdb77
+	// process command line arguments as a stream of tokens.
ecdb77
+	// operations consume the first token in the stream until
ecdb77
+	// the stream is empty.
ecdb77
+	argStream := osArgs
ecdb77
+	for len(argStream) > 0 {
ecdb77
+		arg := argStream[0]
ecdb77
+		if ! isFlag(arg) {
ecdb77
+			argStream = argStream[1:]
ecdb77
+			c.positionalArgs = append(c.positionalArgs, arg)
ecdb77
+
ecdb77
+		} else {
ecdb77
+			arg = trimFlag(arg)
ecdb77
+			if _, ok:= c.altFlags[arg]; ok {
ecdb77
+				arg = c.altFlags[arg]
ecdb77
+			}
ecdb77
+			iface, ok := c.flagDecls[arg]
ecdb77
+			if !ok {
ecdb77
+				return fmt.Errorf("unexpected argument: %v", arg)
ecdb77
+			}
ecdb77
+			switch flag := iface.(type) {
ecdb77
+				case StringFlag:
ecdb77
+					argStream = argStream[1:]
ecdb77
+					if len(argStream) == 0 {
ecdb77
+						return fmt.Errorf("expected value for argument: %v", arg)
ecdb77
+					}
ecdb77
+					if isFlag(argStream[0]) {
ecdb77
+						return fmt.Errorf("unexpected flag: %v", arg)
ecdb77
+					}
ecdb77
+					c.flagValues[arg] = argStream[0]
ecdb77
+				case StringSliceFlag:
ecdb77
+					argStream = argStream[1:]
ecdb77
+					if len (argStream) == 0 {
ecdb77
+						return fmt.Errorf("expected value for argument: %v", arg)
ecdb77
+					}
ecdb77
+					if isFlag(argStream[0]) {
ecdb77
+						return fmt.Errorf("unexpected flag: %v", arg)
ecdb77
+					}
ecdb77
+					c.flagValues[arg] = make([]string, 0)
ecdb77
+					c.flagValues[arg] = append(c.flagValues[arg].([]string), argStream[0])
ecdb77
+					argStream = argStream[1:]
ecdb77
+					for len(argStream) > 0 && ! isFlag(argStream[0]) {
ecdb77
+						c.flagValues[arg] = append(c.flagValues[arg].([]string), argStream[0])
ecdb77
+						argStream = argStream[1:]
ecdb77
+					}
ecdb77
+				case BoolFlag:
ecdb77
+					argStream = argStream[1:]
ecdb77
+					c.flagValues[canonicalName(flag)] = true
ecdb77
+				default:
ecdb77
+					return fmt.Errorf("unexpected flag: %v", arg)
ecdb77
+			}
ecdb77
+		}
ecdb77
+	}
ecdb77
+	return nil
ecdb77
+}
ecdb77
+
ecdb77
+func newContext(flags []Flag, osArgs []string) (*Context, error) {
ecdb77
+	var c Context
ecdb77
+	c.flagValues = make(map[string]interface{})
ecdb77
+	c.flagDecls = make(map[string]Flag)
ecdb77
+	c.altFlags = make(map[string]string)
ecdb77
+	c.altFlags = make(map[string]string)
ecdb77
+	if err := c.setupFlags(flags); err != nil {
ecdb77
+		return nil, err
ecdb77
+	}
ecdb77
+	if err := c.parseArgs(osArgs); err != nil {
ecdb77
+		return nil, err
ecdb77
+	}
ecdb77
+	return &c, nil
ecdb77
+}
ecdb77
+
ecdb77
+func (f StringFlag) name() string {return f.Name}
ecdb77
+func (f StringFlag) usage() string {return f.Usage}
ecdb77
+func (f BoolFlag) name() string {return f.Name}
ecdb77
+func (f BoolFlag) usage() string {return f.Usage}
ecdb77
+func (f StringSliceFlag) name() string {return f.Name}
ecdb77
+func (f StringSliceFlag) usage() string {return f.Usage}
ecdb77
+
ecdb77
+// takes a Flag with a comma delimited string
ecdb77
+// of flag names and returns the first one
ecdb77
+func canonicalName(flag Flag) string {
ecdb77
+	flagNames := strings.Split(flag.name(), ",")
ecdb77
+	return strings.TrimSpace(flagNames[0])
ecdb77
+}
ecdb77
+
ecdb77
+// takes a Flag with a comma delimited string
ecdb77
+// of flag names and returns them as a string slice
ecdb77
+// with the canonical (first) flag ommitted
ecdb77
+func alternateNames(flag Flag) []string {
ecdb77
+	flagNames := strings.Split(flag.name(), ",")
ecdb77
+	altNames := flagNames[1:]
ecdb77
+	for i, _ := range altNames {
ecdb77
+		altNames[i] = strings.TrimSpace(altNames[i])
ecdb77
+	}
ecdb77
+	return altNames
ecdb77
+}
ecdb77
+
ecdb77
+func isFlag(arg string) bool {
ecdb77
+  return strings.HasPrefix(arg, "-")
ecdb77
+}
ecdb77
+
ecdb77
+func trimFlag(arg string) string {
ecdb77
+  return strings.Trim(strings.Trim(arg, "-"), "-")
ecdb77
+}
ecdb77
+