-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add environments to project configuration (#68)
- Loading branch information
Showing
7 changed files
with
142 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package project | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const bricksEnv = "BRICKS_ENV" | ||
|
||
const DefaultEnvironment = "development" | ||
|
||
// Workspace defines configurables at the workspace level. | ||
type Workspace struct { | ||
Profile string `json:"profile,omitempty"` | ||
} | ||
|
||
// Environment defines all configurables for a single environment. | ||
type Environment struct { | ||
Workspace Workspace `json:"workspace"` | ||
} | ||
|
||
// getEnvironment returns the name of the environment to operate in. | ||
func getEnvironment(cmd *cobra.Command) (value string) { | ||
// The command line flag takes precedence. | ||
flag := cmd.Flag("environment") | ||
if flag != nil { | ||
value = flag.Value.String() | ||
if value != "" { | ||
return | ||
} | ||
} | ||
|
||
// If it's not set, use the environment variable. | ||
value = os.Getenv(bricksEnv) | ||
if value != "" { | ||
return | ||
} | ||
|
||
return DefaultEnvironment | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package project | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEnvironmentFromCommand(t *testing.T) { | ||
var cmd cobra.Command | ||
cmd.Flags().String("environment", "", "specify environment") | ||
cmd.Flags().Set("environment", "env-from-arg") | ||
t.Setenv(bricksEnv, "") | ||
|
||
value := getEnvironment(&cmd) | ||
assert.Equal(t, "env-from-arg", value) | ||
} | ||
|
||
func TestEnvironmentFromEnvironment(t *testing.T) { | ||
var cmd cobra.Command | ||
cmd.Flags().String("environment", "", "specify environment") | ||
cmd.Flags().Set("environment", "") | ||
t.Setenv(bricksEnv, "env-from-env") | ||
|
||
value := getEnvironment(&cmd) | ||
assert.Equal(t, "env-from-env", value) | ||
} | ||
|
||
func TestEnvironmentDefault(t *testing.T) { | ||
var cmd cobra.Command | ||
cmd.Flags().String("environment", "", "specify environment") | ||
cmd.Flags().Set("environment", "") | ||
t.Setenv(bricksEnv, "") | ||
|
||
value := getEnvironment(&cmd) | ||
assert.Equal(t, DefaultEnvironment, value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: dev | ||
profile: demo | ||
dev_cluster: | ||
cluster_name: Shared Autoscaling | ||
cluster_name: Shared Autoscaling |