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

Make environments string constants #6

Merged
merged 1 commit into from
Jun 24, 2024
Merged
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
3 changes: 2 additions & 1 deletion cmd/install_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/spf13/cobra"
"gov.gsa.fac.cgov-util/internal/environments"
"gov.gsa.fac.cgov-util/internal/logging"
"gov.gsa.fac.cgov-util/internal/util"
)
Expand Down Expand Up @@ -84,7 +85,7 @@ var installAwsCmd = &cobra.Command{
Short: "Install AWS-CLI",
Long: `This command will curl the necessary aws-cli package and install it`,
Run: func(cmd *cobra.Command, args []string) {
if slices.Contains([]string{"DEV", "PREVIEW", "STAGING", "PRODUCTION"}, os.Getenv("ENV")) {
if slices.Contains([]string{environments.DEVELOPMENT, environments.PREVIEW, environments.STAGING, environments.PRODUCTION}, os.Getenv("ENV")) {
InstallAWS()
} else {
logging.Logger.Printf("ENV set to LOCAL or TESTING, aws-cli is not necessary to install.")
Expand Down
13 changes: 7 additions & 6 deletions cmd/s3_to_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/bitfield/script"
"github.com/spf13/cobra"
"gov.gsa.fac.cgov-util/internal/environments"
"gov.gsa.fac.cgov-util/internal/logging"
"gov.gsa.fac.cgov-util/internal/pipes"
"gov.gsa.fac.cgov-util/internal/structs"
Expand Down Expand Up @@ -122,9 +123,9 @@ var S3toDBCmd = &cobra.Command{
}

switch os.Getenv("ENV") {
case "LOCAL":
case environments.LOCAL:
fallthrough
case "TESTING":
case environments.TESTING:
bucket_creds, err := vcap.VCS.GetCredentials("user-provided", path_struct.Bucket)
if err != nil {
logging.Logger.Printf("S3TODB could not get minio credentials")
Expand All @@ -133,13 +134,13 @@ var S3toDBCmd = &cobra.Command{
bucket_to_local_tables(db_creds, bucket_creds, path_struct)
os.Remove("pg_dump_tables")
logging.Logger.Println("Finished Restore and cleaning residual files/folders.")
case "DEV":
case environments.DEVELOPMENT:
fallthrough
case "STAGING":
case environments.PREVIEW:
fallthrough
case "PREVIEW":
case environments.STAGING:
fallthrough
case "PRODUCTION":
case environments.PRODUCTION:
bucket_creds, err := vcap.VCS.GetCredentials("s3", path_struct.Bucket)
if err != nil {
logging.Logger.Printf("S3toDB could not get s3 credentials")
Expand Down
25 changes: 13 additions & 12 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"

"gov.gsa.fac.cgov-util/internal/environments"
"gov.gsa.fac.cgov-util/internal/logging"
"gov.gsa.fac.cgov-util/internal/structs"
"gov.gsa.fac.cgov-util/internal/vcap"
Expand Down Expand Up @@ -33,17 +34,17 @@ func parseS3Path(s3_path string) *structs.S3Path {

func runLocalOrRemote(funs structs.Choice) {
switch os.Getenv("ENV") {
case "LOCAL":
case environments.LOCAL:
fallthrough
case "TESTING":
case environments.TESTING:
funs.Local()
case "DEVELOPMENT":
case environments.DEVELOPMENT:
fallthrough
case "STAGING":
case environments.PREVIEW:
fallthrough
case "PREVIEW":
case environments.STAGING:
fallthrough
case "PRODUCTION":
case environments.PRODUCTION:
funs.Remote()
default:
logging.Logger.Printf("LOCALORREMOTE impossible condition")
Expand All @@ -63,22 +64,22 @@ func getDBCredentials(db_name string) vcap.Credentials {

func getBucketCredentials(s3path *structs.S3Path) vcap.Credentials {
switch os.Getenv("ENV") {
case "LOCAL":
case environments.LOCAL:
fallthrough
case "TESTING":
case environments.TESTING:
bucket_creds, err := vcap.VCS.GetCredentials("user-provided", s3path.Bucket)
if err != nil {
logging.Logger.Printf("GetCredentials could not get minio credentials: %s", s3path)
os.Exit(logging.COULD_NOT_FIND_CREDENTIALS)
}
return bucket_creds
case "DEV":
case environments.DEVELOPMENT:
fallthrough
case "STAGING":
case environments.PREVIEW:
fallthrough
case "PREVIEW":
case environments.STAGING:
fallthrough
case "PRODUCTION":
case environments.PRODUCTION:
bucket_creds, err := vcap.VCS.GetCredentials("s3", s3path.Bucket)
if err != nil {
logging.Logger.Printf("DBTOS3 could not get s3 credentials")
Expand Down
9 changes: 9 additions & 0 deletions internal/environments/environments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package environments

const LOCAL = "LOCAL"
const TESTING = "TESTING"

const DEVELOPMENT = "DEVELOPMENT"
const PREVIEW = "PREVIEW"
const STAGING = "STAGING"
const PRODUCTION = "PRODUCTION"
13 changes: 7 additions & 6 deletions internal/util/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"os"

"gov.gsa.fac.cgov-util/internal/environments"
"gov.gsa.fac.cgov-util/internal/logging"
)

Expand All @@ -13,20 +14,20 @@ var PSQL_path = "NO PATH SET"

func SetPaths(env string) {
switch env {
case "LOCAL":
case environments.LOCAL:
fallthrough
case "TESTING":
case environments.TESTING:
AWS_path = "aws"
PGDUMP_path = "pg_dump"
PGRESTORE_path = "pg_restore"
PSQL_path = "psql"
case "DEV":
case environments.DEVELOPMENT:
fallthrough
case "STAGING":
case environments.PREVIEW:
fallthrough
case "PREVIEW":
case environments.STAGING:
fallthrough
case "PRODUCTION":
case environments.PRODUCTION:
AWS_path = "/home/vcap/app/bin/aws"
PGDUMP_path = "/home/vcap/deps/0/apt/usr/lib/postgresql/15/bin/pg_dump"
PGRESTORE_path = "/home/vcap/deps/0/apt/usr/lib/postgresql/15/bin/pg_restore"
Expand Down
3 changes: 2 additions & 1 deletion internal/util/unset_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"os"
"slices"

"gov.gsa.fac.cgov-util/internal/environments"
"gov.gsa.fac.cgov-util/internal/logging"
)

func UnsetProxy() {
if slices.Contains([]string{"DEV", "PREVIEW", "STAGING", "PRODUCTION"}, os.Getenv("ENV")) {
if slices.Contains([]string{environments.DEVELOPMENT, environments.PREVIEW, environments.STAGING, environments.PRODUCTION}, os.Getenv("ENV")) {
if IsDebugLevel("DEBUG") {
logging.Logger.Println("Proxy:", os.Getenv("https_proxy"))
}
Expand Down
Loading