diff --git a/.gitignore b/.gitignore index e44922776..91a950091 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +.idea .cover coverage.html diff --git a/README.md b/README.md index da3259e59..c6c3b10db 100644 --- a/README.md +++ b/README.md @@ -3,83 +3,15 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/optimizely/go-sdk)](https://goreportcard.com/report/github.com/optimizely/go-sdk) [![Coverage Status](https://coveralls.io/repos/github/optimizely/go-sdk/badge.svg?branch=master)](https://coveralls.io/github/optimizely/go-sdk?branch=master) -## Usage - -### Instantiation -To start using the SDK, create an instance using our factory method: - -``` -import "github.com/optimizely/go-sdk/pkg/client" - -optimizelyFactory := &client.OptimizelyFactory{ - SDKKey: "[SDK_KEY_HERE]", -} - -client, err := optimizelyFactory.Client() - -// You can also instantiate with a hard-coded datafile -optimizelyFactory := &client.OptimizelyFactory{ - Datafile: []byte("datafile_string"), -} - -client, err := optimizelyFactory.Client() - -``` - -### Feature Rollouts -``` -import ( - "github.com/optimizely/go-sdk/pkg/client" - "github.com/optimizely/go-sdk/pkg/entities" -) - -user := entities.UserContext{ - ID: "optimizely end user", - Attributes: map[string]interface{}{ - "state": "California", - "likes_donuts": true, - }, -} - -enabled, _ := client.IsFeatureEnabled("binary_feature", user) -``` - -## Command line interface -A CLI has been provided to illustrate the functionality of the SDK. Simply run `go-sdk` for help. -```$sh -go-sdk provides cli access to your Optimizely fullstack project - -Usage: - go-sdk [command] - -Available Commands: - help Help about any command - is_feature_enabled Is feature enabled? - get_enabled_features Get enabled features - track Track a conversion event - get_feature_variable_boolean Get feature variable boolean value - get_feature_variable_double Get feature variable double value - get_feature_variable_integer Get feature variable integer value - get_feature_variable_string Get feature variable string value - -Flags: - -h, --help help for go-sdk - -s, --sdkKey string Optimizely project SDK key - -Use "go-sdk [command] --help" for more information about a command. -``` - -Each supported SDK API method is it's own [cobra](https://github.com/spf13/cobra) command and requires the -input of an `--sdkKey`. +## Installation -### Installation -Install the CLI from github: +### Install from github: ```$sh go install github.com/optimizely/go-sdk ``` -Install the CLI from source: +### Install from source: ```$sh go get github.com/optimizely/go-sdk cd $GOPATH/src/github.com/optimizely/go-sdk @@ -89,20 +21,22 @@ go install NOTE: We practice trunk-based development, and as such our default branch, `master` might not always be the most stable. We do tag releases on Github and you can pin your installation to those particular release versions. One way to do this is to use [*Go Modules*](https://blog.golang.org/using-go-modules) for managing external dependencies: +### Install using go.mod: + ``` module mymodule go 1.12 require ( - github.com/optimizely/go-sdk v1.0.0-beta2 + github.com/optimizely/go-sdk v1.0.0-beta6 ) ``` If you are already using `go.mod` in your application you can run the following: ``` -go mod edit -require github.com/optimizely/go-sdk@v1.0.0-beta2 +go mod edit -require github.com/optimizely/go-sdk@v1.0.0-beta6 ``` NOTE: @@ -115,122 +49,46 @@ go get github.com/optimizely/go-sdk/pkg ``` will install it as a package to pkg directory, rather than src directory. It could be useful for future development and vendoring. -### Commands - -#### is_feature_enabled -``` -Determines if a feature is enabled - -Usage: - go-sdk is_feature_enabled [flags] - -Flags: - -f, --featureKey string feature key to enable - -h, --help help for is_feature_enabled - -u, --userId string user id - -Global Flags: - -s, --sdkKey string Optimizely project SDK key - ``` - -#### get_enabled_features -``` -Returns enabled features for userId - -Usage: - go-sdk get_enabled_features [flags] - -Flags: - -h, --help help for get_enabled_features - -u, --userId string user id +## Usage -Global Flags: - -s, --sdkKey string Optimizely project SDK key - ``` +### Instantiation +To start using the SDK, create an instance using our factory method: -#### track ``` -Tracks a conversion event - -Usage: - go-sdk track [flags] - -Flags: - -e, --eventKey string event key to track - -h, --help help for track - -u, --userId string user id - -Global Flags: - -s, --sdkKey string Optimizely project SDK key - ``` +import "github.com/optimizely/go-sdk/pkg/client" -#### get_feature_variable_boolean -``` -Returns feature variable boolean value +optimizelyFactory := &client.OptimizelyFactory{ + SDKKey: "[SDK_KEY_HERE]", +} -Usage: - go-sdk get_feature_variable_boolean [flags] +client, err := optimizelyFactory.Client() -Flags: - -f, --featureKey string feature key for feature - -v, --variableKey string variable key for feature variable - -h, --help help for get_feature_variable_boolean - -u, --userId string user id +// You can also instantiate with a hard-coded datafile +optimizelyFactory := &client.OptimizelyFactory{ + Datafile: []byte("datafile_string"), +} -Global Flags: - -s, --sdkKey string Optimizely project SDK key - ``` +client, err := optimizelyFactory.Client() -#### get_feature_variable_double ``` -Returns feature variable double value - -Usage: - go-sdk get_feature_variable_double [flags] - -Flags: - -f, --featureKey string feature key for feature - -v, --variableKey string variable key for feature variable - -h, --help help for get_feature_variable_double - -u, --userId string user id - -Global Flags: - -s, --sdkKey string Optimizely project SDK key - ``` -#### get_feature_variable_integer +### Feature Rollouts ``` -Returns feature variable integer value - -Usage: - go-sdk get_feature_variable_integer [flags] - -Flags: - -f, --featureKey string feature key for feature - -v, --variableKey string variable key for feature variable - -h, --help help for get_feature_variable_integer - -u, --userId string user id +import ( + "github.com/optimizely/go-sdk/pkg/client" + "github.com/optimizely/go-sdk/pkg/entities" +) -Global Flags: - -s, --sdkKey string Optimizely project SDK key - ``` +user := entities.UserContext{ + ID: "optimizely end user", + Attributes: map[string]interface{}{ + "state": "California", + "likes_donuts": true, + }, +} -#### get_feature_variable_string +enabled, _ := client.IsFeatureEnabled("binary_feature", user) ``` -Returns feature variable string value - -Usage: - go-sdk get_feature_variable_string [flags] - -Flags: - -f, --featureKey string feature key for feature - -v, --variableKey string variable key for feature variable - -h, --help help for get_feature_variable_string - -u, --userId string user id - -Global Flags: - -s, --sdkKey string Optimizely project SDK key - ``` ## Credits diff --git a/cmd/get_enabled_features.go b/cmd/get_enabled_features.go deleted file mode 100644 index d98109740..000000000 --- a/cmd/get_enabled_features.go +++ /dev/null @@ -1,57 +0,0 @@ -/**************************************************************************** - * Copyright 2019, Optimizely, Inc. and contributors * - * * - * Licensed under the Apache License, Version 2.0 (the "License"); * - * you may not use this file except in compliance with the License. * - * You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * - * See the License for the specific language governing permissions and * - * limitations under the License. * - ***************************************************************************/ - -package cmd - -import ( - "fmt" - - "github.com/optimizely/go-sdk/pkg/client" - "github.com/optimizely/go-sdk/pkg/entities" - "github.com/spf13/cobra" -) - -var getEnabledFeaturesCmd = &cobra.Command{ - Use: "get_enabled_features", - Short: "Get enabled features", - Long: `Returns enabled features`, - Run: func(cmd *cobra.Command, args []string) { - optimizelyFactory := &client.OptimizelyFactory{ - SDKKey: sdkKey, - } - - client, err := optimizelyFactory.StaticClient() - - if err != nil { - fmt.Printf("Error instantiating client: %s\n", err) - return - } - - user := entities.UserContext{ - ID: userID, - Attributes: map[string]interface{}{}, - } - - features, _ := client.GetEnabledFeatures(user) - fmt.Printf("Enabled features for \"%s\": %v\n", userID, features) - }, -} - -func init() { - rootCmd.AddCommand(getEnabledFeaturesCmd) - getEnabledFeaturesCmd.Flags().StringVarP(&userID, "userId", "u", "", "user id") - getEnabledFeaturesCmd.MarkFlagRequired("userId") -} diff --git a/cmd/get_feature_variable_boolean.go b/cmd/get_feature_variable_boolean.go deleted file mode 100644 index 2e0d9b02d..000000000 --- a/cmd/get_feature_variable_boolean.go +++ /dev/null @@ -1,65 +0,0 @@ -/**************************************************************************** - * Copyright 2019, Optimizely, Inc. and contributors * - * * - * Licensed under the Apache License, Version 2.0 (the "License"); * - * you may not use this file except in compliance with the License. * - * You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * - * See the License for the specific language governing permissions and * - * limitations under the License. * - ***************************************************************************/ - -package cmd - -import ( - "fmt" - - "github.com/optimizely/go-sdk/pkg/client" - "github.com/optimizely/go-sdk/pkg/entities" - "github.com/spf13/cobra" -) - -var getFeatureVariableBooleanCmd = &cobra.Command{ - Use: "get_feature_variable_boolean", - Short: "Get feature variable boolean", - Long: `Returns boolean feature variable`, - Run: func(cmd *cobra.Command, args []string) { - optimizelyFactory := &client.OptimizelyFactory{ - SDKKey: sdkKey, - } - - client, err := optimizelyFactory.StaticClient() - - if err != nil { - fmt.Printf("Error instantiating client: %s\n", err) - return - } - - user := entities.UserContext{ - ID: userID, - Attributes: map[string]interface{}{}, - } - - value, _ := client.GetFeatureVariableBoolean(featureKey, variableKey, user) - if err == nil { - fmt.Printf("Feature \"%s\" variable \"%s\" boolean value: %t\n", featureKey, variableKey, value) - } else { - fmt.Printf("Get feature \"%s\" variable \"%s\" boolean failed with error: %s\n", featureKey, variableKey, err) - } - }, -} - -func init() { - rootCmd.AddCommand(getFeatureVariableBooleanCmd) - getFeatureVariableBooleanCmd.Flags().StringVarP(&userID, "userId", "u", "", "user id") - getFeatureVariableBooleanCmd.MarkFlagRequired("userId") - getFeatureVariableBooleanCmd.Flags().StringVarP(&featureKey, "featureKey", "f", "", "feature key for feature") - getFeatureVariableBooleanCmd.MarkFlagRequired("featureKey") - getFeatureVariableBooleanCmd.Flags().StringVarP(&variableKey, "variableKey", "v", "", "variable key for feature variable") - getFeatureVariableBooleanCmd.MarkFlagRequired("variableKey") -} diff --git a/cmd/get_feature_variable_double.go b/cmd/get_feature_variable_double.go deleted file mode 100644 index 6980fc68f..000000000 --- a/cmd/get_feature_variable_double.go +++ /dev/null @@ -1,65 +0,0 @@ -/**************************************************************************** - * Copyright 2019, Optimizely, Inc. and contributors * - * * - * Licensed under the Apache License, Version 2.0 (the "License"); * - * you may not use this file except in compliance with the License. * - * You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * - * See the License for the specific language governing permissions and * - * limitations under the License. * - ***************************************************************************/ - -package cmd - -import ( - "fmt" - - "github.com/optimizely/go-sdk/pkg/client" - "github.com/optimizely/go-sdk/pkg/entities" - "github.com/spf13/cobra" -) - -var getFeatureVariableDoubleCmd = &cobra.Command{ - Use: "get_feature_variable_double", - Short: "Get feature variable double", - Long: `Returns double feature variable`, - Run: func(cmd *cobra.Command, args []string) { - optimizelyFactory := &client.OptimizelyFactory{ - SDKKey: sdkKey, - } - - client, err := optimizelyFactory.StaticClient() - - if err != nil { - fmt.Printf("Error instantiating client: %s\n", err) - return - } - - user := entities.UserContext{ - ID: userID, - Attributes: map[string]interface{}{}, - } - - value, err := client.GetFeatureVariableDouble(featureKey, variableKey, user) - if err == nil { - fmt.Printf("Feature \"%s\" variable \"%s\" double value: %v\n", featureKey, variableKey, value) - } else { - fmt.Printf("Get feature \"%s\" variable \"%s\" double failed with error: %s\n", featureKey, variableKey, err) - } - }, -} - -func init() { - rootCmd.AddCommand(getFeatureVariableDoubleCmd) - getFeatureVariableDoubleCmd.Flags().StringVarP(&userID, "userId", "u", "", "user id") - getFeatureVariableDoubleCmd.MarkFlagRequired("userId") - getFeatureVariableDoubleCmd.Flags().StringVarP(&featureKey, "featureKey", "f", "", "feature key for feature") - getFeatureVariableDoubleCmd.MarkFlagRequired("featureKey") - getFeatureVariableDoubleCmd.Flags().StringVarP(&variableKey, "variableKey", "v", "", "variable key for feature variable") - getFeatureVariableDoubleCmd.MarkFlagRequired("variableKey") -} diff --git a/cmd/get_feature_variable_integer.go b/cmd/get_feature_variable_integer.go deleted file mode 100644 index e912176cc..000000000 --- a/cmd/get_feature_variable_integer.go +++ /dev/null @@ -1,65 +0,0 @@ -/**************************************************************************** - * Copyright 2019, Optimizely, Inc. and contributors * - * * - * Licensed under the Apache License, Version 2.0 (the "License"); * - * you may not use this file except in compliance with the License. * - * You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * - * See the License for the specific language governing permissions and * - * limitations under the License. * - ***************************************************************************/ - -package cmd - -import ( - "fmt" - - "github.com/optimizely/go-sdk/pkg/client" - "github.com/optimizely/go-sdk/pkg/entities" - "github.com/spf13/cobra" -) - -var getFeatureVariableIntegerCmd = &cobra.Command{ - Use: "get_feature_variable_integer", - Short: "Get feature variable integer", - Long: `Returns integer feature variable`, - Run: func(cmd *cobra.Command, args []string) { - optimizelyFactory := &client.OptimizelyFactory{ - SDKKey: sdkKey, - } - - client, err := optimizelyFactory.StaticClient() - - if err != nil { - fmt.Printf("Error instantiating client: %s\n", err) - return - } - - user := entities.UserContext{ - ID: userID, - Attributes: map[string]interface{}{}, - } - - value, err := client.GetFeatureVariableInteger(featureKey, variableKey, user) - if err == nil { - fmt.Printf("Feature \"%s\" variable \"%s\" integer value: %v\n", featureKey, variableKey, value) - } else { - fmt.Printf("Get feature \"%s\" variable \"%s\" integer failed with error: %s\n", featureKey, variableKey, err) - } - }, -} - -func init() { - rootCmd.AddCommand(getFeatureVariableIntegerCmd) - getFeatureVariableIntegerCmd.Flags().StringVarP(&userID, "userId", "u", "", "user id") - getFeatureVariableIntegerCmd.MarkFlagRequired("userId") - getFeatureVariableIntegerCmd.Flags().StringVarP(&featureKey, "featureKey", "f", "", "feature key for feature") - getFeatureVariableIntegerCmd.MarkFlagRequired("featureKey") - getFeatureVariableIntegerCmd.Flags().StringVarP(&variableKey, "variableKey", "v", "", "variable key for feature variable") - getFeatureVariableIntegerCmd.MarkFlagRequired("variableKey") -} diff --git a/cmd/get_feature_variable_string.go b/cmd/get_feature_variable_string.go deleted file mode 100644 index f1d80a130..000000000 --- a/cmd/get_feature_variable_string.go +++ /dev/null @@ -1,65 +0,0 @@ -/**************************************************************************** - * Copyright 2019, Optimizely, Inc. and contributors * - * * - * Licensed under the Apache License, Version 2.0 (the "License"); * - * you may not use this file except in compliance with the License. * - * You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * - * See the License for the specific language governing permissions and * - * limitations under the License. * - ***************************************************************************/ - -package cmd - -import ( - "fmt" - - "github.com/optimizely/go-sdk/pkg/client" - "github.com/optimizely/go-sdk/pkg/entities" - "github.com/spf13/cobra" -) - -var getFeatureVariableStringCmd = &cobra.Command{ - Use: "get_feature_variable_string", - Short: "Get feature variable string", - Long: `Returns string feature variable`, - Run: func(cmd *cobra.Command, args []string) { - optimizelyFactory := &client.OptimizelyFactory{ - SDKKey: sdkKey, - } - - client, err := optimizelyFactory.StaticClient() - - if err != nil { - fmt.Printf("Error instantiating client: %s\n", err) - return - } - - user := entities.UserContext{ - ID: userID, - Attributes: map[string]interface{}{}, - } - - value, _ := client.GetFeatureVariableString(featureKey, variableKey, user) - if err == nil { - fmt.Printf("Feature \"%s\" variable \"%s\" string value: %s\n", featureKey, variableKey, value) - } else { - fmt.Printf("Get feature \"%s\" variable \"%s\" string failed with error: %s\n", featureKey, variableKey, err) - } - }, -} - -func init() { - rootCmd.AddCommand(getFeatureVariableStringCmd) - getFeatureVariableStringCmd.Flags().StringVarP(&userID, "userId", "u", "", "user id") - getFeatureVariableStringCmd.MarkFlagRequired("userId") - getFeatureVariableStringCmd.Flags().StringVarP(&featureKey, "featureKey", "f", "", "feature key for feature") - getFeatureVariableStringCmd.MarkFlagRequired("featureKey") - getFeatureVariableStringCmd.Flags().StringVarP(&variableKey, "variableKey", "v", "", "variable key for feature variable") - getFeatureVariableStringCmd.MarkFlagRequired("variableKey") -} diff --git a/cmd/is_feature_enabled.go b/cmd/is_feature_enabled.go deleted file mode 100644 index 316c15e2d..000000000 --- a/cmd/is_feature_enabled.go +++ /dev/null @@ -1,59 +0,0 @@ -/**************************************************************************** - * Copyright 2019, Optimizely, Inc. and contributors * - * * - * Licensed under the Apache License, Version 2.0 (the "License"); * - * you may not use this file except in compliance with the License. * - * You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * - * See the License for the specific language governing permissions and * - * limitations under the License. * - ***************************************************************************/ - -package cmd - -import ( - "fmt" - - "github.com/optimizely/go-sdk/pkg/client" - "github.com/optimizely/go-sdk/pkg/entities" - "github.com/spf13/cobra" -) - -var isFeatureEnabledCmd = &cobra.Command{ - Use: "is_feature_enabled", - Short: "Is feature enabled?", - Long: `Determines if a feature is enabled`, - Run: func(cmd *cobra.Command, args []string) { - optimizelyFactory := &client.OptimizelyFactory{ - SDKKey: sdkKey, - } - - client, err := optimizelyFactory.StaticClient() - - if err != nil { - fmt.Printf("Error instantiating client: %s\n", err) - return - } - - user := entities.UserContext{ - ID: userID, - Attributes: map[string]interface{}{}, - } - - enabled, _ := client.IsFeatureEnabled(featureKey, user) - fmt.Printf("Is feature \"%s\" enabled for \"%s\"? %t\n", featureKey, userID, enabled) - }, -} - -func init() { - rootCmd.AddCommand(isFeatureEnabledCmd) - isFeatureEnabledCmd.Flags().StringVarP(&userID, "userId", "u", "", "user id") - isFeatureEnabledCmd.MarkFlagRequired("userId") - isFeatureEnabledCmd.Flags().StringVarP(&featureKey, "featureKey", "f", "", "feature key to enable") - isFeatureEnabledCmd.MarkFlagRequired("featureKey") -} diff --git a/cmd/track.go b/cmd/track.go deleted file mode 100644 index c095c0710..000000000 --- a/cmd/track.go +++ /dev/null @@ -1,63 +0,0 @@ -/**************************************************************************** - * Copyright 2019, Optimizely, Inc. and contributors * - * * - * Licensed under the Apache License, Version 2.0 (the "License"); * - * you may not use this file except in compliance with the License. * - * You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * - * See the License for the specific language governing permissions and * - * limitations under the License. * - ***************************************************************************/ - -package cmd - -import ( - "fmt" - - "github.com/optimizely/go-sdk/pkg/client" - "github.com/optimizely/go-sdk/pkg/entities" - "github.com/spf13/cobra" -) - -var trackCmd = &cobra.Command{ - Use: "track", - Short: "track event", - Long: `Tracks a conversion event with eventKey`, - Run: func(cmd *cobra.Command, args []string) { - optimizelyFactory := &client.OptimizelyFactory{ - SDKKey: sdkKey, - } - - client, err := optimizelyFactory.StaticClient() - - if err != nil { - fmt.Printf("Error instantiating client: %s\n", err) - return - } - - user := entities.UserContext{ - ID: userID, - Attributes: map[string]interface{}{}, - } - - err = client.Track(eventKey, user, map[string]interface{}{}) - if err == nil { - fmt.Printf("Tracked event \"%s\" for \"%s\"", eventKey, userID) - } else { - fmt.Printf("Failed to Track event \"%s\" for \"%s\"", eventKey, userID) - } - }, -} - -func init() { - rootCmd.AddCommand(trackCmd) - trackCmd.Flags().StringVarP(&userID, "userId", "u", "", "user id") - trackCmd.MarkFlagRequired("userId") - trackCmd.Flags().StringVarP(&eventKey, "eventKey", "e", "", "event key to track") - trackCmd.MarkFlagRequired("eventKey") -} diff --git a/examples/main.go b/examples/main.go index ae0738b5c..dbfe6affa 100644 --- a/examples/main.go +++ b/examples/main.go @@ -7,8 +7,8 @@ import ( "fmt" "time" + "github.com/optimizely/go-sdk" "github.com/optimizely/go-sdk/pkg/client" - "github.com/optimizely/go-sdk/pkg/entities" "github.com/optimizely/go-sdk/pkg/event" "github.com/optimizely/go-sdk/pkg/logging" ) @@ -16,32 +16,40 @@ import ( func main() { sdkKey := "4SLpaJA1r1pgE6T2CoMs9q" logging.SetLogLevel(logging.LogLevelDebug) - user := entities.UserContext{ - ID: "mike ng", - Attributes: map[string]interface{}{ + + /************* Simple usage ********************/ + + user := optimizely.UserContext( + "mike ng", + map[string]interface{}{ "country": "Unknown", "likes_donuts": true, }, - } + ) + optimizelyClient, err := optimizely.Client(sdkKey) + enabled, _ := optimizelyClient.IsFeatureEnabled("mutext_feat", user) + fmt.Printf("Is feature enabled? %v\n", enabled) + + /************* StaticClient ********************/ + optimizelyFactory := &client.OptimizelyFactory{ SDKKey: sdkKey, } - /************* StaticClient ********************/ - - optimizelyClient, err := optimizelyFactory.StaticClient() + optimizelyClient, err = optimizelyFactory.StaticClient() if err != nil { fmt.Printf("Error instantiating client: %s", err) return } - enabled, _ := optimizelyClient.IsFeatureEnabled("mutext_feat", user) + enabled, _ = optimizelyClient.IsFeatureEnabled("mutext_feat", user) fmt.Printf("Is feature enabled? %v\n", enabled) fmt.Println() optimizelyClient.Close() // user can close dispatcher fmt.Println() + /************* Client ********************/ optimizelyFactory = &client.OptimizelyFactory{ diff --git a/go.mod b/go.mod index 9ef37e039..a2809be02 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/google/uuid v1.1.1 github.com/json-iterator/go v1.1.7 github.com/pkg/profile v1.3.0 - github.com/spf13/cobra v0.0.5 github.com/stretchr/testify v1.4.0 github.com/twmb/murmur3 v1.0.0 golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e diff --git a/go.sum b/go.sum index d0ec11357..8e5899d90 100644 --- a/go.sum +++ b/go.sum @@ -31,8 +31,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= -github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s= -github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= diff --git a/main.go b/main.go deleted file mode 100644 index 6dd5d4f21..000000000 --- a/main.go +++ /dev/null @@ -1,23 +0,0 @@ -/**************************************************************************** - * Copyright 2019, Optimizely, Inc. and contributors * - * * - * Licensed under the Apache License, Version 2.0 (the "License"); * - * you may not use this file except in compliance with the License. * - * You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * - * See the License for the specific language governing permissions and * - * limitations under the License. * - ***************************************************************************/ - -package main - -import "github.com/optimizely/go-sdk/cmd" - -func main() { - cmd.Execute() -} diff --git a/cmd/root.go b/optimizely.go similarity index 66% rename from cmd/root.go rename to optimizely.go index 04b7fded0..5098d029d 100644 --- a/cmd/root.go +++ b/optimizely.go @@ -14,37 +14,25 @@ * limitations under the License. * ***************************************************************************/ -package cmd +package optimizely import ( - "fmt" - "os" - - "github.com/spf13/cobra" -) - -var ( - userID string - featureKey string - variableKey string - eventKey string - sdkKey string + "github.com/optimizely/go-sdk/pkg/client" + "github.com/optimizely/go-sdk/pkg/entities" ) -var rootCmd = &cobra.Command{ - Use: "go-sdk", - Short: "go-sdk provides cli access to your Optimizely fullstack project", -} - -// Execute executes rootCmd, exits if error found -func Execute() { - if err := rootCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(1) +// Client returns an OptimizelyClient instantitated with the given key and options +func Client(sdkKey string, options ...client.OptionFunc) (*client.OptimizelyClient, error) { + factory := &client.OptimizelyFactory{ + SDKKey: sdkKey, } + return factory.Client(options...) } -func init() { - rootCmd.PersistentFlags().StringVarP(&sdkKey, "sdkKey", "s", "", "Optimizely project SDK key") - rootCmd.MarkPersistentFlagRequired("sdkKey") +// UserContext is a helper method for creating a user context +func UserContext(userID string, attributes map[string]interface{}) entities.UserContext { + return entities.UserContext{ + ID: userID, + Attributes: attributes, + } } diff --git a/pkg/client/factory.go b/pkg/client/factory.go index 205864673..bdf3f7f54 100644 --- a/pkg/client/factory.go +++ b/pkg/client/factory.go @@ -41,10 +41,10 @@ type OptimizelyFactory struct { overrideStore decision.ExperimentOverrideStore } -// OptionFunc is a type to a proper func +// OptionFunc is used to provide custom client configuration to the OptimizelyFactory type OptionFunc func(*OptimizelyFactory) -// Client gets client and sets some parameters +// Client instantiates a new OptimizelyClient with the given options func (f OptimizelyFactory) Client(clientOptions ...OptionFunc) (*OptimizelyClient, error) { // extract options for _, opt := range clientOptions {