github.com/goonzoid/gcli@v0.2.3-0.20150926213610-155587606ea1/skeleton/executable.go (about)

     1  package skeleton
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  )
     7  
     8  const (
     9  	// DefaultVersion is default appliaction version.
    10  	DefaultVersion = "0.1.0"
    11  
    12  	// DefaultDescription is default application description.
    13  	DefaultDescription = ""
    14  )
    15  
    16  // Executable store executable meta information.
    17  type Executable struct {
    18  	// Name is executable name.
    19  	Name string
    20  
    21  	// Owner is owner of the executable.
    22  	Owner string
    23  
    24  	// Commands are commands of the executable.
    25  	Commands []Command
    26  
    27  	// Flags are flags of the executable.
    28  	Flags []Flag
    29  
    30  	// Version is initial version.
    31  	Version string
    32  
    33  	// Description is description of the executable.
    34  	Description string
    35  
    36  	// FrameworkStr is framework name to use.
    37  	FrameworkStr string `toml:"Framework"`
    38  }
    39  
    40  // NewExecutable is constructor of Executable struct.
    41  func NewExecutable() *Executable {
    42  	return &Executable{
    43  		Version:     DefaultVersion,
    44  		Description: DefaultDescription,
    45  	}
    46  }
    47  
    48  // Validate validates Executalbe has required field or not.
    49  // If not returns, errors as slice.
    50  func (e *Executable) Validate() (errs []error) {
    51  
    52  	if e.Name == "" {
    53  		errs = append(errs, fmt.Errorf("`Name` cannot be blank"))
    54  	}
    55  
    56  	if e.Owner == "" {
    57  		errs = append(errs, fmt.Errorf("`Owner` cannot be blank"))
    58  	}
    59  
    60  	if len(e.Commands) == 0 && len(e.Flags) == 0 {
    61  		// can be blank ?
    62  	}
    63  
    64  	if len(e.Commands) > 0 {
    65  		for _, c := range e.Commands {
    66  			if c.Name == "" {
    67  				errs = append(errs, fmt.Errorf("`Command.Name` cannot be blank"))
    68  			}
    69  		}
    70  	}
    71  
    72  	if len(e.Flags) > 0 {
    73  		for _, f := range e.Flags {
    74  			if f.LongName == "" {
    75  				errs = append(errs, fmt.Errorf("`Flag.LongName` cannot be blank"))
    76  			}
    77  
    78  			if f.TypeString == "" {
    79  				errs = append(errs, fmt.Errorf("`Flag.TypeString` cannot be blank. Select from bool|int|string"))
    80  			}
    81  
    82  		}
    83  	}
    84  
    85  	if e.Version == "" {
    86  		// can be blank
    87  	}
    88  
    89  	if e.Description == "" {
    90  		// can be blank
    91  	}
    92  
    93  	if e.FrameworkStr == "" {
    94  		// can be blank
    95  	}
    96  
    97  	return errs
    98  }
    99  
   100  // Overwrite overwrites provided value with default value.
   101  func (e *Executable) Overwrite(key string, v interface{}) error {
   102  	// Check
   103  	switch v.(type) {
   104  	case string, []Command, []Flag:
   105  	default:
   106  		return fmt.Errorf("unexpected value: %#v", v)
   107  	}
   108  
   109  	rve := reflect.ValueOf(e)
   110  	rve.Elem().FieldByName(key).Set(reflect.ValueOf(v))
   111  
   112  	return nil
   113  }