2016-11-03 17:16:01 -05:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"flag"
|
2020-10-16 00:06:27 -05:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-11-03 17:16:01 -05:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2019-05-03 07:49:14 -05:00
|
|
|
"syscall"
|
2016-11-03 17:16:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Context is a type that is passed through to
|
|
|
|
// each Handler action in a cli application. Context
|
|
|
|
// can be used to retrieve context-specific Args and
|
|
|
|
// parsed command-line options.
|
|
|
|
type Context struct {
|
|
|
|
App *App
|
|
|
|
Command Command
|
2019-05-03 07:49:14 -05:00
|
|
|
shellComplete bool
|
2016-11-03 17:16:01 -05:00
|
|
|
flagSet *flag.FlagSet
|
|
|
|
setFlags map[string]bool
|
|
|
|
parentContext *Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext creates a new context. For use in when invoking an App or Command action.
|
|
|
|
func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
|
2019-05-03 07:49:14 -05:00
|
|
|
c := &Context{App: app, flagSet: set, parentContext: parentCtx}
|
|
|
|
|
|
|
|
if parentCtx != nil {
|
|
|
|
c.shellComplete = parentCtx.shellComplete
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
2016-11-03 17:16:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NumFlags returns the number of flags set
|
|
|
|
func (c *Context) NumFlags() int {
|
|
|
|
return c.flagSet.NFlag()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets a context flag to a value.
|
|
|
|
func (c *Context) Set(name, value string) error {
|
2019-05-03 07:49:14 -05:00
|
|
|
c.setFlags = nil
|
2016-11-03 17:16:01 -05:00
|
|
|
return c.flagSet.Set(name, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GlobalSet sets a context flag to a value on the global flagset
|
|
|
|
func (c *Context) GlobalSet(name, value string) error {
|
2019-05-03 07:49:14 -05:00
|
|
|
globalContext(c).setFlags = nil
|
2016-11-03 17:16:01 -05:00
|
|
|
return globalContext(c).flagSet.Set(name, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSet determines if the flag was actually set
|
|
|
|
func (c *Context) IsSet(name string) bool {
|
|
|
|
if c.setFlags == nil {
|
|
|
|
c.setFlags = make(map[string]bool)
|
|
|
|
|
|
|
|
c.flagSet.Visit(func(f *flag.Flag) {
|
|
|
|
c.setFlags[f.Name] = true
|
|
|
|
})
|
|
|
|
|
|
|
|
c.flagSet.VisitAll(func(f *flag.Flag) {
|
|
|
|
if _, ok := c.setFlags[f.Name]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.setFlags[f.Name] = false
|
|
|
|
})
|
|
|
|
|
|
|
|
// XXX hack to support IsSet for flags with EnvVar
|
|
|
|
//
|
|
|
|
// There isn't an easy way to do this with the current implementation since
|
|
|
|
// whether a flag was set via an environment variable is very difficult to
|
|
|
|
// determine here. Instead, we intend to introduce a backwards incompatible
|
|
|
|
// change in version 2 to add `IsSet` to the Flag interface to push the
|
|
|
|
// responsibility closer to where the information required to determine
|
|
|
|
// whether a flag is set by non-standard means such as environment
|
2020-10-16 00:06:27 -05:00
|
|
|
// variables is available.
|
2016-11-03 17:16:01 -05:00
|
|
|
//
|
|
|
|
// See https://github.com/urfave/cli/issues/294 for additional discussion
|
|
|
|
flags := c.Command.Flags
|
|
|
|
if c.Command.Name == "" { // cannot == Command{} since it contains slice types
|
|
|
|
if c.App != nil {
|
|
|
|
flags = c.App.Flags
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, f := range flags {
|
|
|
|
eachName(f.GetName(), func(name string) {
|
|
|
|
if isSet, ok := c.setFlags[name]; isSet || !ok {
|
2020-10-16 00:06:27 -05:00
|
|
|
// Check if a flag is set
|
|
|
|
if isSet {
|
|
|
|
// If the flag is set, also set its other aliases
|
|
|
|
eachName(f.GetName(), func(name string) {
|
|
|
|
c.setFlags[name] = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-03 17:16:01 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
val := reflect.ValueOf(f)
|
|
|
|
if val.Kind() == reflect.Ptr {
|
|
|
|
val = val.Elem()
|
|
|
|
}
|
|
|
|
|
2020-10-16 00:06:27 -05:00
|
|
|
filePathValue := val.FieldByName("FilePath")
|
|
|
|
if filePathValue.IsValid() {
|
|
|
|
eachName(filePathValue.String(), func(filePath string) {
|
|
|
|
if _, err := os.Stat(filePath); err == nil {
|
|
|
|
c.setFlags[name] = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2016-11-03 17:16:01 -05:00
|
|
|
}
|
|
|
|
|
2020-10-16 00:06:27 -05:00
|
|
|
envVarValue := val.FieldByName("EnvVar")
|
|
|
|
if envVarValue.IsValid() {
|
|
|
|
eachName(envVarValue.String(), func(envVar string) {
|
|
|
|
envVar = strings.TrimSpace(envVar)
|
|
|
|
if _, ok := syscall.Getenv(envVar); ok {
|
|
|
|
c.setFlags[name] = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-11-03 17:16:01 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.setFlags[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
// GlobalIsSet determines if the global flag was actually set
|
|
|
|
func (c *Context) GlobalIsSet(name string) bool {
|
|
|
|
ctx := c
|
|
|
|
if ctx.parentContext != nil {
|
|
|
|
ctx = ctx.parentContext
|
|
|
|
}
|
|
|
|
|
|
|
|
for ; ctx != nil; ctx = ctx.parentContext {
|
|
|
|
if ctx.IsSet(name) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlagNames returns a slice of flag names used in this context.
|
|
|
|
func (c *Context) FlagNames() (names []string) {
|
2020-10-16 00:06:27 -05:00
|
|
|
for _, f := range c.Command.Flags {
|
|
|
|
name := strings.Split(f.GetName(), ",")[0]
|
2016-11-03 17:16:01 -05:00
|
|
|
if name == "help" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GlobalFlagNames returns a slice of global flag names used by the app.
|
|
|
|
func (c *Context) GlobalFlagNames() (names []string) {
|
2020-10-16 00:06:27 -05:00
|
|
|
for _, f := range c.App.Flags {
|
|
|
|
name := strings.Split(f.GetName(), ",")[0]
|
2016-11-03 17:16:01 -05:00
|
|
|
if name == "help" || name == "version" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent returns the parent context, if any
|
|
|
|
func (c *Context) Parent() *Context {
|
|
|
|
return c.parentContext
|
|
|
|
}
|
|
|
|
|
2019-05-03 07:49:14 -05:00
|
|
|
// value returns the value of the flag coressponding to `name`
|
|
|
|
func (c *Context) value(name string) interface{} {
|
|
|
|
return c.flagSet.Lookup(name).Value.(flag.Getter).Get()
|
|
|
|
}
|
|
|
|
|
2016-11-03 17:16:01 -05:00
|
|
|
// Args contains apps console arguments
|
|
|
|
type Args []string
|
|
|
|
|
|
|
|
// Args returns the command line arguments associated with the context.
|
|
|
|
func (c *Context) Args() Args {
|
|
|
|
args := Args(c.flagSet.Args())
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
// NArg returns the number of the command line arguments.
|
|
|
|
func (c *Context) NArg() int {
|
|
|
|
return len(c.Args())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the nth argument, or else a blank string
|
|
|
|
func (a Args) Get(n int) string {
|
|
|
|
if len(a) > n {
|
|
|
|
return a[n]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// First returns the first argument, or else a blank string
|
|
|
|
func (a Args) First() string {
|
|
|
|
return a.Get(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tail returns the rest of the arguments (not the first one)
|
|
|
|
// or else an empty string slice
|
|
|
|
func (a Args) Tail() []string {
|
|
|
|
if len(a) >= 2 {
|
|
|
|
return []string(a)[1:]
|
|
|
|
}
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present checks if there are any arguments present
|
|
|
|
func (a Args) Present() bool {
|
|
|
|
return len(a) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap swaps arguments at the given indexes
|
|
|
|
func (a Args) Swap(from, to int) error {
|
|
|
|
if from >= len(a) || to >= len(a) {
|
|
|
|
return errors.New("index out of range")
|
|
|
|
}
|
|
|
|
a[from], a[to] = a[to], a[from]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func globalContext(ctx *Context) *Context {
|
|
|
|
if ctx == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if ctx.parentContext == nil {
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
ctx = ctx.parentContext
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupGlobalFlagSet(name string, ctx *Context) *flag.FlagSet {
|
|
|
|
if ctx.parentContext != nil {
|
|
|
|
ctx = ctx.parentContext
|
|
|
|
}
|
|
|
|
for ; ctx != nil; ctx = ctx.parentContext {
|
|
|
|
if f := ctx.flagSet.Lookup(name); f != nil {
|
|
|
|
return ctx.flagSet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) {
|
|
|
|
switch ff.Value.(type) {
|
|
|
|
case *StringSlice:
|
|
|
|
default:
|
2020-10-16 00:06:27 -05:00
|
|
|
_ = set.Set(name, ff.Value.String())
|
2016-11-03 17:16:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
|
|
|
|
visited := make(map[string]bool)
|
|
|
|
set.Visit(func(f *flag.Flag) {
|
|
|
|
visited[f.Name] = true
|
|
|
|
})
|
|
|
|
for _, f := range flags {
|
|
|
|
parts := strings.Split(f.GetName(), ",")
|
|
|
|
if len(parts) == 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var ff *flag.Flag
|
|
|
|
for _, name := range parts {
|
|
|
|
name = strings.Trim(name, " ")
|
|
|
|
if visited[name] {
|
|
|
|
if ff != nil {
|
|
|
|
return errors.New("Cannot use two forms of the same flag: " + name + " " + ff.Name)
|
|
|
|
}
|
|
|
|
ff = set.Lookup(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ff == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, name := range parts {
|
|
|
|
name = strings.Trim(name, " ")
|
|
|
|
if !visited[name] {
|
|
|
|
copyFlag(name, ff, set)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-16 00:06:27 -05:00
|
|
|
|
|
|
|
type requiredFlagsErr interface {
|
|
|
|
error
|
|
|
|
getMissingFlags() []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type errRequiredFlags struct {
|
|
|
|
missingFlags []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *errRequiredFlags) Error() string {
|
|
|
|
numberOfMissingFlags := len(e.missingFlags)
|
|
|
|
if numberOfMissingFlags == 1 {
|
|
|
|
return fmt.Sprintf("Required flag %q not set", e.missingFlags[0])
|
|
|
|
}
|
|
|
|
joinedMissingFlags := strings.Join(e.missingFlags, ", ")
|
|
|
|
return fmt.Sprintf("Required flags %q not set", joinedMissingFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *errRequiredFlags) getMissingFlags() []string {
|
|
|
|
return e.missingFlags
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkRequiredFlags(flags []Flag, context *Context) requiredFlagsErr {
|
|
|
|
var missingFlags []string
|
|
|
|
for _, f := range flags {
|
|
|
|
if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() {
|
|
|
|
var flagPresent bool
|
|
|
|
var flagName string
|
|
|
|
for _, key := range strings.Split(f.GetName(), ",") {
|
2020-11-06 13:41:42 -05:00
|
|
|
key = strings.TrimSpace(key)
|
2020-10-16 00:06:27 -05:00
|
|
|
if len(key) > 1 {
|
|
|
|
flagName = key
|
|
|
|
}
|
|
|
|
|
2020-11-06 13:41:42 -05:00
|
|
|
if context.IsSet(key) {
|
2020-10-16 00:06:27 -05:00
|
|
|
flagPresent = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !flagPresent && flagName != "" {
|
|
|
|
missingFlags = append(missingFlags, flagName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(missingFlags) != 0 {
|
|
|
|
return &errRequiredFlags{missingFlags: missingFlags}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|