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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  var cmdRecord = &Command{
     8  	Run:   runRecord,
     9  	Usage: "record <command> [<args>]",
    10  	Short: "Create, modify, or view records",
    11  	Long: `
    12  Create, modify, or view records
    13  
    14  Usage:
    15  
    16    force record get <object> <id>
    17  
    18    force record create <object> [<fields>]
    19  
    20    force record create:bulk <object> <file> [<format>]
    21  
    22    force record update <object> <id> [<fields>]
    23  
    24    force record delete <object> <id>
    25  
    26  Examples:
    27  
    28    force record get User 00Ei0000000000
    29  
    30    force record create User Name:"David Dollar" Phone:0000000000
    31  
    32    force record update User 00Ei0000000000 State:GA
    33  
    34    force record delete User 00Ei0000000000
    35  `,
    36  }
    37  
    38  func runRecord(cmd *Command, args []string) {
    39  	if len(args) == 0 {
    40  		cmd.printUsage()
    41  	} else {
    42  		switch args[0] {
    43  		case "get":
    44  			runRecordGet(args[1:])
    45  		case "create", "add":
    46  			runRecordCreate(args[1:])
    47  		case "create:bulk":
    48  			if len(args) == 3 {
    49  				createBulkInsertJob(args[2], args[1], "CSV")
    50  			} else if len(args) == 4 {
    51  				createBulkInsertJob(args[2], args[1], args[3])
    52  			}
    53  		case "update":
    54  			runRecordUpdate(args[1:])
    55  		case "delete", "remove":
    56  			runRecordDelete(args[1:])
    57  		default:
    58  			ErrorAndExit("no such command: %s", args[0])
    59  		}
    60  	}
    61  }
    62  
    63  func runRecordGet(args []string) {
    64  	if len(args) != 2 {
    65  		ErrorAndExit("must specify object and id")
    66  	}
    67  	force, _ := ActiveForce()
    68  	object, err := force.GetRecord(args[0], args[1])
    69  	if err != nil {
    70  		ErrorAndExit(err.Error())
    71  	} else {
    72  		DisplayForceRecord(object)
    73  	}
    74  }
    75  
    76  func runRecordCreate(args []string) {
    77  	if len(args) < 1 {
    78  		ErrorAndExit("must specify object")
    79  	}
    80  	force, _ := ActiveForce()
    81  	attrs := ParseArgumentAttrs(args[1:])
    82  	id, err := force.CreateRecord(args[0], attrs)
    83  	if err != nil {
    84  		ErrorAndExit(err.Error())
    85  	}
    86  	fmt.Printf("Record created: %s\n", id)
    87  }
    88  
    89  func runRecordUpdate(args []string) {
    90  	if len(args) < 2 {
    91  		ErrorAndExit("must specify object and id")
    92  	}
    93  	force, _ := ActiveForce()
    94  	attrs := ParseArgumentAttrs(args[2:])
    95  	err := force.UpdateRecord(args[0], args[1], attrs)
    96  	if err != nil {
    97  		ErrorAndExit(err.Error())
    98  	}
    99  	fmt.Println("Record updated")
   100  }
   101  
   102  func runRecordDelete(args []string) {
   103  	if len(args) != 2 {
   104  		ErrorAndExit("must specify object and id")
   105  	}
   106  	force, _ := ActiveForce()
   107  	err := force.DeleteRecord(args[0], args[1])
   108  	if err != nil {
   109  		ErrorAndExit(err.Error())
   110  	}
   111  	fmt.Println("Record deleted")
   112  }