Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow scriptsPath to be configurable for use with godep etc. #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package zoom

import (
"os"
"path/filepath"
)

var (
scriptsPath = filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "albrow", "zoom", "scripts")
)

// defaultConfiguration holds the default values for each config option
// if the zero value is provided in the input configuration, the value
// will fallback to the default value
var defaultConfiguration = Configuration{
Address: "localhost:6379",
Network: "tcp",
Database: 0,
Password: "",
Address: "localhost:6379",
Network: "tcp",
Database: 0,
Password: "",
ScriptsPath: "",
}

// parseConfig returns a well-formed configuration struct.
Expand All @@ -18,14 +28,22 @@ func parseConfig(passedConfig *Configuration) *Configuration {
if passedConfig == nil {
return &defaultConfiguration
}

// copy the passedConfig
newConfig := *passedConfig

if newConfig.Address == "" {
newConfig.Address = defaultConfiguration.Address
}

if newConfig.Network == "" {
newConfig.Network = defaultConfiguration.Network
}

if newConfig.ScriptsPath == "" {
newConfig.ScriptsPath = scriptsPath
}

// since the zero value for int is 0, we can skip config.Database
// since the zero value for string is "", we can skip config.Address
return &newConfig
Expand All @@ -44,4 +62,8 @@ type Configuration struct {
// every connection will use the AUTH command during initialization
// to authenticate with the database. Default: ""
Password string

// ScriptsPath for location of lua scripts to load
// Default: $GOPATH/src/github.com/albrow/zoom/scripts
ScriptsPath string
}
10 changes: 3 additions & 7 deletions scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
package zoom

import (
"github.com/garyburd/redigo/redis"
"io/ioutil"
"os"
"path/filepath"

"github.com/garyburd/redigo/redis"
)

var (
Expand All @@ -22,13 +22,9 @@ var (
extractIdsFromStringIndexScript *redis.Script
)

var (
scriptsPath = filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "albrow", "zoom", "scripts")
)

// initScripts will parse all the lua script files in scriptsPath and assign them
// to the variables above. It must be run before any scripts are executed.
func initScripts() error {
func initScripts(scriptsPath string) error {
scriptsToParse := []struct {
script **redis.Script
filename string
Expand Down
2 changes: 1 addition & 1 deletion zoom.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package zoom
func Init(config *Configuration) error {
config = parseConfig(config)
initPool(config.Network, config.Address, config.Database, config.Password)
if err := initScripts(); err != nil {
if err := initScripts(config.ScriptsPath); err != nil {
return err
}
return nil
Expand Down