Skip to content

Commit

Permalink
Create Library Package
Browse files Browse the repository at this point in the history
Reorganize code to create a separate lib package so the code can be used
as a library in other go applications.

Move commands into a separate command package.

Add get, post, and session commands.
  • Loading branch information
cwarden committed Sep 21, 2017
1 parent 63c93be commit 33d6613
Show file tree
Hide file tree
Showing 67 changed files with 963 additions and 1,398 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -22,8 +22,8 @@ script:
- git config --global user.name "Your Name"
- test -z "$(go fmt)"
- go vet
- go test -v
- go test -v -race
- go test -v ./...
- go test -v -race ./...
- export OS_TARGETS="darwin windows linux"
- export ARCH_TARGETS="386 amd64"
- export GO_LINKER_VALUE=$(git describe --tags --always)
Expand Down
6 changes: 5 additions & 1 deletion active.go → command/active.go
@@ -1,11 +1,15 @@
package main
package command

import (
"encoding/json"
"fmt"
"os/exec"
"runtime"
"sort"

. "github.com/heroku/force/config"
. "github.com/heroku/force/error"
. "github.com/heroku/force/lib"
)

var cmdActive = &Command{
Expand Down
5 changes: 4 additions & 1 deletion apex.go → command/apex.go
@@ -1,9 +1,12 @@
package main
package command

import (
"fmt"
"io/ioutil"
"os"

. "github.com/heroku/force/error"
. "github.com/heroku/force/lib"
)

var cmdApex = &Command{
Expand Down
18 changes: 7 additions & 11 deletions apiversion.go → command/apiversion.go
@@ -1,13 +1,12 @@
package main
package command

import (
"fmt"
"os"
"regexp"
)

var apiVersionNumber = "40.0"
var apiVersion = fmt.Sprintf("v%s", apiVersionNumber)
. "github.com/heroku/force/error"
. "github.com/heroku/force/lib"
)

var cmdApiVersion = &Command{
Run: runApiVersion,
Expand All @@ -27,21 +26,18 @@ func init() {
}

func runApiVersion(cmd *Command, args []string) {
force, _ := ActiveForce()
if len(args) == 1 {
apiVersionNumber = args[0]
apiVersionNumber := args[0]
matched, err := regexp.MatchString("^\\d{2}\\.0$", apiVersionNumber)
if err != nil {
ErrorAndExit("%v", err)
}
if !matched {
ErrorAndExit("apiversion must be in the form of nn.0.")
}
apiVersion = fmt.Sprintf("v%s", apiVersionNumber)
force.Credentials.ApiVersion = apiVersionNumber
ForceSaveLogin(*force.Credentials, os.Stdout)
UpdateApiVersion(apiVersionNumber)
} else if len(args) == 0 {
fmt.Println(apiVersion)
fmt.Println(ApiVersion())
} else {
ErrorAndExit("The apiversion command only accepts a single argument in the form of nn.0")
}
Expand Down
2 changes: 1 addition & 1 deletion attrs.go → command/attrs.go
@@ -1,4 +1,4 @@
package main
package command

import (
"net/url"
Expand Down
6 changes: 5 additions & 1 deletion aura.go → command/aura.go
@@ -1,4 +1,4 @@
package main
package command

import (
"encoding/json"
Expand All @@ -7,6 +7,9 @@ import (
"os"
"path/filepath"
"strings"

. "github.com/heroku/force/error"
. "github.com/heroku/force/lib"
)

// Brief comment to fire commit
Expand Down Expand Up @@ -63,6 +66,7 @@ func init() {

var (
auraentityname string
metadataType string
)

func runAura(cmd *Command, args []string) {
Expand Down
7 changes: 5 additions & 2 deletions bigobject.go → command/bigobject.go
@@ -1,11 +1,14 @@
package main
package command

import (
"bitbucket.org/pkg/inflect"
"fmt"
"os"
"strconv"
"strings"

. "github.com/heroku/force/error"
. "github.com/heroku/force/lib"
)

var cmdBigObject = &Command{
Expand Down Expand Up @@ -74,7 +77,7 @@ func init() {

func runBigObject(cmd *Command, args []string) {
if len(args) == 0 {
cmd.printUsage()
cmd.PrintUsage()
} else {
if err := cmd.Flag.Parse(args[1:]); err != nil {
os.Exit(2)
Expand Down
9 changes: 6 additions & 3 deletions bulk.go → command/bulk.go
@@ -1,4 +1,4 @@
package main
package command

/*
Expand Down Expand Up @@ -56,6 +56,9 @@ import (
"fmt"
"io/ioutil"
"strings"

. "github.com/heroku/force/error"
. "github.com/heroku/force/lib"
)

var cmdBulk = &Command{
Expand Down Expand Up @@ -136,7 +139,7 @@ func init() {

func runBulk2(cmd *Command, args []string) {
if len(command) == 0 {
cmd.printUsage()
cmd.PrintUsage()
return
}
commandVersion = "new"
Expand Down Expand Up @@ -205,7 +208,7 @@ func runBulk(cmd *Command, args []string) {
return
}
if len(args) == 0 {
cmd.printUsage()
cmd.PrintUsage()
return
}

Expand Down
44 changes: 42 additions & 2 deletions command.go → command/command.go
@@ -1,4 +1,4 @@
package main
package command

import (
"flag"
Expand All @@ -9,6 +9,46 @@ import (
var flagEnv string
var flagProcfile string

var Commands = []*Command{
cmdApiVersion,
cmdUseDXAuth,
cmdLogin,
cmdLogout,
cmdLogins,
cmdCreate,
cmdActive,
cmdWhoami,
cmdDescribe,
cmdSobject,
cmdBigObject,
cmdField,
cmdRecord,
cmdBulk,
cmdFetch,
cmdImport,
cmdExport,
cmdQuery,
cmdApex,
cmdGet,
cmdPost,
cmdSession,
cmdTrace,
cmdLog,
cmdEventLogFile,
cmdOauth,
cmdTest,
cmdSecurity,
cmdVersion,
cmdUpdate,
cmdPush,
cmdAura,
cmdPassword,
cmdNotifySet,
cmdLimits,
cmdHelp,
cmdDataPipe,
}

type Command struct {
// args does not include the command name
Run func(cmd *Command, args []string)
Expand All @@ -19,7 +59,7 @@ type Command struct {
Long string // `forego help cmd` output
}

func (c *Command) printUsage() {
func (c *Command) PrintUsage() {
if c.Runnable() {
fmt.Printf("Usage: force %s\n\n", c.Usage)
}
Expand Down
13 changes: 13 additions & 0 deletions command/command_suite_test.go
@@ -0,0 +1,13 @@
package command_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestCommand(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Command Suite")
}
5 changes: 3 additions & 2 deletions command_test.go → command/command_test.go
@@ -1,13 +1,14 @@
package main
package command_test

import (
"github.com/bmizerany/assert"
. "github.com/heroku/force/command"
"testing"
)

// test that all avialable commands come with at least a name and short usage information
func TestUsage(t *testing.T) {
for _, cmd := range commands {
for _, cmd := range Commands {
assert.NotEqual(t, cmd.Name(), "")
assert.NotEqual(t, cmd.Short, "")
}
Expand Down
7 changes: 5 additions & 2 deletions create.go → command/create.go
@@ -1,8 +1,11 @@
package main
package command

import (
"fmt"
"strings"

. "github.com/heroku/force/error"
. "github.com/heroku/force/lib"
)

var cmdCreate = &Command{
Expand Down Expand Up @@ -34,7 +37,7 @@ func init() {
func runCreate(cmd *Command, args []string) {
force, _ := ActiveForce()
if len(what) == 0 || len(itemName) == 0 {
cmd.printUsage()
cmd.PrintUsage()
} else {
attrs := make(map[string]string)
switch what {
Expand Down
11 changes: 7 additions & 4 deletions datapipe.go → command/datapipe.go
@@ -1,9 +1,12 @@
package main
package command

import (
"fmt"
"io/ioutil"
"os"

. "github.com/heroku/force/error"
. "github.com/heroku/force/lib"
)

var cmdDataPipe = &Command{
Expand Down Expand Up @@ -76,8 +79,8 @@ func init() {
cmdDataPipe.Flag.StringVar(&masterlabel, "l", "", "set master label")
cmdDataPipe.Flag.StringVar(&scriptcontent, "scriptcontent", defaultContent, "set script content")
cmdDataPipe.Flag.StringVar(&scriptcontent, "c", defaultContent, "set script content")
cmdDataPipe.Flag.StringVar(&apiversion, "apiversion", apiVersionNumber, "set api version")
cmdDataPipe.Flag.StringVar(&apiversion, "v", apiVersionNumber, "set api version")
cmdDataPipe.Flag.StringVar(&apiversion, "apiversion", ApiVersionNumber(), "set api version")
cmdDataPipe.Flag.StringVar(&apiversion, "v", ApiVersionNumber(), "set api version")
cmdDataPipe.Flag.StringVar(&scripttype, "scripttype", "Pig", "set script type")
cmdDataPipe.Flag.StringVar(&scripttype, "t", "Pig", "set script type")
cmdDataPipe.Flag.StringVar(&query, "q", "", "SOQL query string on DataPipeline object")
Expand All @@ -91,7 +94,7 @@ func init() {

func runDataPipe(cmd *Command, args []string) {
if len(args) == 0 {
cmd.printUsage()
cmd.PrintUsage()
} else {
if err := cmd.Flag.Parse(args[1:]); err != nil {
os.Exit(2)
Expand Down
6 changes: 4 additions & 2 deletions describe.go → command/describe.go
@@ -1,8 +1,10 @@
package main
package command

import (
"encoding/xml"
//"fmt"

. "github.com/heroku/force/error"
. "github.com/heroku/force/lib"
)

var cmdDescribe = &Command{
Expand Down
9 changes: 7 additions & 2 deletions eventlogfile.go → command/eventlogfile.go
@@ -1,6 +1,11 @@
package main
package command

import "fmt"
import (
"fmt"

. "github.com/heroku/force/error"
. "github.com/heroku/force/lib"
)

var cmdEventLogFile = &Command{
Run: getEventLogFile,
Expand Down
8 changes: 6 additions & 2 deletions export.go → command/export.go
@@ -1,11 +1,15 @@
package main
package command

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/heroku/force/config"
. "github.com/heroku/force/error"
. "github.com/heroku/force/lib"
)

var cmdExport = &Command{
Expand Down Expand Up @@ -187,7 +191,7 @@ func runExport(cmd *Command, args []string) {
}

if root == "" {
root, err = GetSourceDir()
root, err = config.GetSourceDir()
if err != nil {
fmt.Printf("Error obtaining root directory\n")
ErrorAndExit(err.Error())
Expand Down

0 comments on commit 33d6613

Please sign in to comment.