github.com/haagen/force@v0.19.6-0.20140911230915-22addd930b34/field.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  var cmdField = &Command{
     9  	Run:   runField,
    10  	Usage: "field",
    11  	Short: "Manage sobject fields",
    12  	Long: `
    13  Manage sobject fields
    14  
    15  Usage:
    16  
    17    force field list <object>
    18  
    19    force field create <object> <field>:<type> [<option>:<value>]
    20  
    21    force field delete <object> <field>
    22  
    23    force field type 
    24  
    25    force field type <fieldtype>
    26  
    27  Examples:
    28  
    29    force field list Todo__c
    30  
    31    force field create Todo__c Due:DateTime required:true
    32  
    33    force field delete Todo__c Due
    34  
    35    force field type     #displays all the supported field types 
    36  
    37    force field type email   #displays the required and optional attributes
    38    
    39  `,
    40  }
    41  
    42  func runField(cmd *Command, args []string) {
    43  	if len(args) == 0 {
    44  		cmd.printUsage()
    45  	} else {
    46  		switch args[0] {
    47  		case "list":
    48  			runFieldList(args[1:])
    49  		case "create", "add":
    50  			runFieldCreate(args[1:])
    51  		case "delete", "remove":
    52  			runFieldDelete(args[1:])
    53  		case "type":
    54  			if len(args) == 1 {
    55  				DisplayFieldTypes()
    56  			} else if len(args) == 2 {
    57  				DisplayFieldDetails(args[1])
    58  			}
    59  		default:
    60  			ErrorAndExit("no such command: %s", args[0])
    61  		}
    62  	}
    63  }
    64  
    65  func runFieldList(args []string) {
    66  	if len(args) != 1 {
    67  		ErrorAndExit("must specify object")
    68  	}
    69  	force, _ := ActiveForce()
    70  	sobject, err := force.GetSobject(args[0])
    71  	if err != nil {
    72  		ErrorAndExit(err.Error())
    73  	}
    74  	DisplayForceSobject(sobject)
    75  }
    76  
    77  func runFieldCreate(args []string) {
    78  	if len(args) < 2 {
    79  		ErrorAndExit("must specify object and at least one field")
    80  	}
    81  
    82  	force, _ := ActiveForce()
    83  
    84  	parts := strings.Split(args[1], ":")
    85  	if len(parts) != 2 {
    86  		ErrorAndExit("must specify name:type for fields")
    87  	}
    88  
    89  	var optionMap = make(map[string]string)
    90  	if len(args) > 2 {
    91  		for _, value := range args[2:] {
    92  			options := strings.Split(value, ":")
    93  			if len(options) != 2 {
    94  				ErrorAndExit(fmt.Sprintf("Missing value for field attribute %s", value))
    95  			}
    96  			optionMap[options[0]] = options[1]
    97  		}
    98  	}
    99  
   100  	// Validate the options for this field type
   101  	newOptions, err := force.Metadata.ValidateFieldOptions(parts[1], optionMap)
   102  	if err != nil {
   103  		ErrorAndExit(err.Error())
   104  	}
   105  	if err := force.Metadata.CreateCustomField(args[0], parts[0], parts[1], newOptions); err != nil {
   106  		fmt.Println("Got an error")
   107  		ErrorAndExit(err.Error())
   108  	}
   109  	fmt.Println("Custom field created")
   110  }
   111  
   112  func runFieldDelete(args []string) {
   113  	if len(args) < 2 {
   114  		ErrorAndExit("must specify object and at least one field")
   115  	}
   116  	force, _ := ActiveForce()
   117  	if err := force.Metadata.DeleteCustomField(args[0], args[1]); err != nil {
   118  		ErrorAndExit(err.Error())
   119  	}
   120  	fmt.Println("Custom field deleted")
   121  }