github.com/mhlo/force@v0.22.28-0.20150915022417-6d05ecfb0b47/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 get <object> <extid>:<value> 19 20 force record create <object> [<fields>] 21 22 force record create:bulk <object> <file> [<format>] 23 24 force record update <object> <id> [<fields>] 25 26 force record update <object> <extid>:<value> [<fields>] 27 28 force record delete <object> <id> 29 30 Examples: 31 32 force record get User 00Ei0000000000 33 34 force record get User username:user@name.org 35 36 force record create User Name:"David Dollar" Phone:0000000000 37 38 force record update User 00Ei0000000000 State:GA 39 40 force record update User username:user@name.org State:GA 41 42 force record delete User 00Ei0000000000 43 `, 44 } 45 46 func runRecord(cmd *Command, args []string) { 47 if len(args) == 0 { 48 cmd.printUsage() 49 } else { 50 switch args[0] { 51 case "get": 52 runRecordGet(args[1:]) 53 case "create", "add": 54 runRecordCreate(args[1:]) 55 case "create:bulk": 56 if len(args) == 3 { 57 createBulkInsertJob(args[2], args[1], "CSV") 58 } else if len(args) == 4 { 59 createBulkInsertJob(args[2], args[1], args[3]) 60 } 61 case "update": 62 runRecordUpdate(args[1:]) 63 case "delete", "remove": 64 runRecordDelete(args[1:]) 65 default: 66 ErrorAndExit("no such command: %s", args[0]) 67 } 68 } 69 } 70 71 func runRecordGet(args []string) { 72 if len(args) != 2 { 73 ErrorAndExit("must specify object and id") 74 } 75 force, _ := ActiveForce() 76 object, err := force.GetRecord(args[0], args[1]) 77 if err != nil { 78 ErrorAndExit(err.Error()) 79 } else { 80 DisplayForceRecord(object) 81 } 82 } 83 84 func runRecordCreate(args []string) { 85 if len(args) < 1 { 86 ErrorAndExit("must specify object") 87 } 88 force, _ := ActiveForce() 89 attrs := ParseArgumentAttrs(args[1:]) 90 id, err, emessages := force.CreateRecord(args[0], attrs) 91 if err != nil { 92 ErrorAndExit(err.Error(), emessages[0].ErrorCode) 93 } 94 fmt.Printf("Record created: %s\n", id) 95 } 96 97 func runRecordUpdate(args []string) { 98 if len(args) < 2 { 99 ErrorAndExit("must specify object and id") 100 } 101 force, _ := ActiveForce() 102 attrs := ParseArgumentAttrs(args[2:]) 103 err := force.UpdateRecord(args[0], args[1], attrs) 104 if err != nil { 105 ErrorAndExit(err.Error()) 106 } 107 fmt.Println("Record updated") 108 } 109 110 func runRecordDelete(args []string) { 111 if len(args) != 2 { 112 ErrorAndExit("must specify object and id") 113 } 114 force, _ := ActiveForce() 115 err := force.DeleteRecord(args[0], args[1]) 116 if err != nil { 117 ErrorAndExit(err.Error()) 118 } 119 fmt.Println("Record deleted") 120 }