diff --git a/cmd/backend/add_param/add-param.go b/cmd/backend/add_param/add-param.go new file mode 100644 index 0000000..33c5d8d --- /dev/null +++ b/cmd/backend/add_param/add-param.go @@ -0,0 +1,74 @@ +package add_param + +/* +$ dlp-cli backend add-param --name "YourParameterName" --value "YourParameterValue" --type "String" +*/ + +import ( + "fmt" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/ssm" + "github.com/spf13/cobra" + "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend" +) + +var ( + paramName string + paramValue string + paramType string +) + +var validParamTypes = map[string]bool{ + "String": true, + "StringList": true, + "SecureString": true, +} + +var addParamCmd = &cobra.Command{ + Use: "add-param", + Short: "Add a new param to AWS Parameter Store", + Run: func(cmd *cobra.Command, args []string) { + if paramName == "" || paramValue == "" || paramType == "" { + fmt.Println("Error: You must provide a name, value, and type for the parameter.") + return + } + + if !isValidParamType(paramType) { + fmt.Println("Error: Invalid parameter type. Accepted types are: String, StringList, SecureString.") + return + } + + // Create an AWS session with the specified region + sess := session.Must(session.NewSession(&aws.Config{ + Region: aws.String(backend.AwsRegion), + })) + + svc := ssm.New(sess) + input := &ssm.PutParameterInput{ + Name: aws.String(paramName), + Value: aws.String(paramValue), + Type: aws.String(paramType), + } + + _, err := svc.PutParameter(input) + if err != nil { + fmt.Println("Error adding parameter:", err) + return + } + + fmt.Println("Parameter added successfully.") + }, +} + +func isValidParamType(typ string) bool { + _, exists := validParamTypes[typ] + return exists +} + +func init() { + addParamCmd.Flags().StringVar(¶mName, "name", "", "Name of the parameter") + addParamCmd.Flags().StringVar(¶mValue, "value", "", "Value of the parameter") + addParamCmd.Flags().StringVar(¶mType, "type", "", "Type of the parameter (String, StringList, SecureString)") + backend.BackendCmd.AddCommand(addParamCmd) +} diff --git a/cmd/backend/backend.go b/cmd/backend/backend.go index 6cae3e9..a9c0555 100644 --- a/cmd/backend/backend.go +++ b/cmd/backend/backend.go @@ -9,6 +9,7 @@ import ( ) const BackendDir string = "./training" +const AwsRegion = "us-west-2" // BackendCmd represents the backend command var BackendCmd = &cobra.Command{ diff --git a/cmd/backend/build_training_env/build-training-env-file.go b/cmd/backend/build_training_env/build-training-env-file.go new file mode 100644 index 0000000..d76f4b1 --- /dev/null +++ b/cmd/backend/build_training_env/build-training-env-file.go @@ -0,0 +1,48 @@ +package build_training_env + +/* +Sample Command: +$ dlp-cli build-training-env-file --secret "YourTrainingSecretName" +*/ + +import ( + "log" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/secretsmanager" + "github.com/spf13/cobra" + "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend" + "github.com/DSGT-DLP/Deep-Learning-Playground/cli/utils" // For utils/ +) + +var secretNameTraining string // Name of the secret in AWS Secrets Manager + +var buildTrainingEnvCmd = &cobra.Command{ + Use: "build-training-env-file", + Short: "Build .env file for training/", + Run: func(cmd *cobra.Command, args []string) { + sess, err := session.NewSession(&aws.Config{Region: aws.String(backend.AwsRegion)}) + if err != nil { + log.Fatal("error session: ", err) + } + + smClient := secretsmanager.New(sess) + secretValue, err := smClient.GetSecretValue(&secretsmanager.GetSecretValueInput{SecretId: aws.String(secretNameTraining)}) + if err != nil { + log.Fatal("error retrieving secret: ", err) + } + + path := "./training" + + // Adding secrets to the .env file + utils.WriteToEnvFile(secretNameTraining, *secretValue.SecretString, path) + + // Hardcoding bucket name as a constant + utils.WriteToEnvFile("BUCKET_NAME", utils.DlpUploadBucket, path) + }, +} + +func init() { + buildTrainingEnvCmd.Flags().StringVar(&secretNameTraining, "secret", "", "Name of the secret in AWS Secrets Manager for training") + backend.BackendCmd.AddCommand(buildTrainingEnvCmd) +} diff --git a/cmd/backend/get_secret/get-secret.go b/cmd/backend/get_secret/get-secret.go new file mode 100644 index 0000000..0580804 --- /dev/null +++ b/cmd/backend/get_secret/get-secret.go @@ -0,0 +1,63 @@ +package get_secret + +/* +dlp-cli backend get-secret --name "YourSecretName" +*/ + +import ( + "fmt" + "strings" + + "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/secretsmanager" + "github.com/spf13/cobra" +) + +var secretName string + +var getSecretCmd = &cobra.Command{ + Use: "get-secret", + Short: "Retrieve a secret from AWS Secrets Manager", + Run: func(cmd *cobra.Command, args []string) { + if secretName == "" { + fmt.Println("Error: You must provide the name of the secret.") + return + } + + sess, err := session.NewSession(&aws.Config{ + Region: aws.String(backend.AwsRegion)}, + ) + if err != nil { + fmt.Println("Error creating AWS session:", err) + return + } + + smClient := secretsmanager.New(sess) + + input := &secretsmanager.GetSecretValueInput{ + SecretId: aws.String(secretName), + } + + result, err := smClient.GetSecretValue(input) +if err != nil { + // Check if the error is due to the secret not existing or a possible typo. + if strings.Contains(err.Error(), "secret not found") || strings.Contains(err.Error(), "secret name typo") { + fmt.Println("Error: The secret could not be retrieved. It may not exist or there could be a typo in the secret name.") + } else { + // Handle other error cases. + fmt.Println("Error retrieving secret:", err) + } + return +} + + + fmt.Printf("Secret [%s] successfully received: %s\n", secretName, *result.SecretString) //this validates that the secret was retrieved + }, +} + +func init() { + getSecretCmd.Flags().StringVar(&secretName, "name", "", "Name of the secret to retrieve") + backend.BackendCmd.AddCommand(getSecretCmd) +} diff --git a/cmd/backend/pull_config/pull-config.go b/cmd/backend/pull_config/pull-config.go new file mode 100644 index 0000000..6df58de --- /dev/null +++ b/cmd/backend/pull_config/pull-config.go @@ -0,0 +1,54 @@ +package pull_config + +/* +$ go run main.go backend pull-config +*/ + +import ( + "log" + + "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend" + "github.com/DSGT-DLP/Deep-Learning-Playground/cli/utils" // For utils/ + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/ssm" // For Parameter Store + "github.com/spf13/cobra" +) + +var pullConfigCmd = &cobra.Command{ + Use: "pull-config", + Short: "Pulls values from parameter store/secrets manager and writes them to .env files", + Long: `This command pulls values from parameter store/secrets manager and writes them to .env files in /training and /frontend.`, + Args: cobra.ExactArgs(0), + Run: func(cmd *cobra.Command, args []string) { + sess, err := session.NewSession(&aws.Config{ + Region: aws.String(backend.AwsRegion)}, + ) + + if err != nil { + log.Fatal("Error creating AWS session: ", err) + } + + ssmClient := ssm.New(sess) + + parameterNames := []string{"parameter-name"} + + for _, paramName := range parameterNames { + result, err := ssmClient.GetParameter(&ssm.GetParameterInput{ + Name: aws.String(paramName), + WithDecryption: aws.Bool(true), + }) + + if err != nil { + log.Fatal("Error getting parameter: ", err) + } + + utils.WriteToEnvFile(paramName, *result.Parameter.Value, backend.BackendDir) + utils.WriteToEnvFile(paramName, *result.Parameter.Value, "./frontend") + } + }, +} + +func init() { + backend.BackendCmd.AddCommand(pullConfigCmd) +} \ No newline at end of file diff --git a/cmd/backend/remove_param/remove-param.go b/cmd/backend/remove_param/remove-param.go new file mode 100644 index 0000000..f8218a2 --- /dev/null +++ b/cmd/backend/remove_param/remove-param.go @@ -0,0 +1,52 @@ +package remove_param + +/* +$ dlp-cli backend remove-param --name "YourParameterName" +*/ + +import ( + "fmt" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/ssm" + "github.com/spf13/cobra" + "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend" +) + +var ( + paramName string +) + +var removeParamCmd = &cobra.Command{ + Use: "remove-param", + Short: "Remove a parameter from AWS Parameter Store", + Run: func(cmd *cobra.Command, args []string) { + if paramName == "" { + fmt.Println("Error: You must provide the name of the parameter to remove.") + return + } + + // Create an AWS session with the specified region + sess := session.Must(session.NewSession(&aws.Config{ + Region: aws.String(backend.AwsRegion), + })) + + svc := ssm.New(sess) + input := &ssm.DeleteParameterInput{ + Name: aws.String(paramName), + } + + _, err := svc.DeleteParameter(input) + if err != nil { + fmt.Println("Error removing parameter:", err) + return + } + + fmt.Println("Parameter removed successfully.") + }, +} + +func init() { + removeParamCmd.Flags().StringVar(¶mName, "name", "", "Name of the parameter to remove") + backend.BackendCmd.AddCommand(removeParamCmd) +} diff --git a/cmd/backend/update_param/update-param.go b/cmd/backend/update_param/update-param.go new file mode 100644 index 0000000..8569240 --- /dev/null +++ b/cmd/backend/update_param/update-param.go @@ -0,0 +1,82 @@ +package update_param + +/* +$ dlp-cli backend update-param --name "ExistingParameterName" --value "NewParameterValue" --type "String" --overwrite true +*/ + +import ( + "fmt" + + "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/ssm" + "github.com/spf13/cobra" +) + +var ( + paramName string + paramValue string + paramType string + overwrite bool +) + +func validateParamType(paramType string) bool { + // Define a list of valid parameter types + validTypes := []string{"String", "StringList", "SecureString"} + + // Check if paramType is in the list of valid types + for _, t := range validTypes { + if paramType == t { + return true + } + } + + return false +} + +var updateParamCmd = &cobra.Command{ + Use: "update-param", + Short: "Update an existing parameter value in AWS Parameter Store", + Run: func(cmd *cobra.Command, args []string) { + if paramName == "" || paramValue == "" || paramType == "" { + fmt.Println("Error: You must provide a name, value, and type for the parameter.") + return + } + + // Validate the parameter type + if !validateParamType(paramType) { + fmt.Println("Error: Invalid parameter type. Please use one of: String, StringList, SecureString") + return + } + + // Create an AWS session with the specified region + sess := session.Must(session.NewSession(&aws.Config{ + Region: aws.String(backend.AwsRegion), + })) + + svc := ssm.New(sess) + input := &ssm.PutParameterInput{ + Name: aws.String(paramName), + Value: aws.String(paramValue), + Type: aws.String(paramType), + Overwrite: aws.Bool(overwrite), + } + + _, err := svc.PutParameter(input) + if err != nil { + fmt.Println("Error updating parameter:", err) + return + } + + fmt.Println("Parameter updated successfully.") + }, +} + +func init() { + updateParamCmd.Flags().StringVar(¶mName, "name", "", "Name of the parameter to update") + updateParamCmd.Flags().StringVar(¶mValue, "value", "", "New value for the parameter") + updateParamCmd.Flags().StringVar(¶mType, "type", "", "Type of the parameter (String, StringList, SecureString)") + updateParamCmd.Flags().BoolVar(&overwrite, "overwrite", true, "Whether to overwrite the parameter if it exists") + backend.BackendCmd.AddCommand(updateParamCmd) +} diff --git a/cmd/frontend/build_frontend_env/build-frontend-env-file.go b/cmd/frontend/build_frontend_env/build-frontend-env-file.go new file mode 100644 index 0000000..64b537a --- /dev/null +++ b/cmd/frontend/build_frontend_env/build-frontend-env-file.go @@ -0,0 +1,48 @@ +package build_frontend_env + +/* +Sample Command: +$ dlp-cli build-frontend-env-file --secret "YourSecretName" +*/ + +import ( + "log" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/secretsmanager" + "github.com/spf13/cobra" + "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/frontend" // For frontend/ + "github.com/DSGT-DLP/Deep-Learning-Playground/cli/utils" // For utils/ +) + +var secretName string // Name of the secret in AWS Secrets Manager + +var buildFrontendEnvCmd = &cobra.Command{ + Use: "build-frontend-env-file", + Short: "Build .env file for frontend/", + Run: func(cmd *cobra.Command, args []string) { + sess, err := session.NewSession(&aws.Config{Region: aws.String(frontend.AwsRegion)}) + if err != nil { + log.Fatal("error creating AWS session: ", err) + } + + smClient := secretsmanager.New(sess) + secretValue, err := smClient.GetSecretValue(&secretsmanager.GetSecretValueInput{SecretId: aws.String(secretName)}) + if err != nil { + log.Fatal("error retrieving secret: ", err) + } + + path := "./frontend" + + // Adding secrets to the .env file + utils.WriteToEnvFile(secretName, *secretValue.SecretString, path) + + // Hardcoding bucket name as a constant + utils.WriteToEnvFile("BUCKET_NAME", utils.DlpUploadBucket, path) + }, +} + +func init() { + buildFrontendEnvCmd.Flags().StringVar(&secretName, "secret", "", "Name of the secret in AWS Secrets Manager") + frontend.FrontendCmd.AddCommand(buildFrontendEnvCmd) +} diff --git a/cmd/frontend/frontend.go b/cmd/frontend/frontend.go index 10cebfd..2dc6f9d 100644 --- a/cmd/frontend/frontend.go +++ b/cmd/frontend/frontend.go @@ -9,6 +9,7 @@ import ( ) const FrontendDir string = "./frontend" +const AwsRegion = "us-west-2" // FrontendCmd represents the frontend command var FrontendCmd = &cobra.Command{ diff --git a/cmd/serverless/build_serverless_env/build-serverless-env-file.go b/cmd/serverless/build_serverless_env/build-serverless-env-file.go new file mode 100644 index 0000000..b58eb2a --- /dev/null +++ b/cmd/serverless/build_serverless_env/build-serverless-env-file.go @@ -0,0 +1,38 @@ +package build_serverless_env + +/* +Sample Command: +$ dlp-cli build-serverless-env-file --sst "YourSSTVariables" --dev-endpoints "YourDevEndpoints" +*/ + +import ( + "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/serverless" // For serverless/ + "github.com/spf13/cobra" + "github.com/DSGT-DLP/Deep-Learning-Playground/cli/utils" // For utils/ +) + +var sstVariables string // Variables related to sst +var devEndpoints string // Development endpoints + +var buildServerlessEnvCmd = &cobra.Command{ + Use: "build-serverless-env-file", + Short: "Build .env file for serverless/", + Run: func(cmd *cobra.Command, args []string) { + path := "./serverless" + + // Adding sst variables to the .env file + utils.WriteToEnvFile("SST_VARIABLES", sstVariables, path) + + // Adding dev endpoints to the .env file + utils.WriteToEnvFile("DEV_ENDPOINTS", devEndpoints, path) + + // Hardcoding bucket name as a constant + utils.WriteToEnvFile("BUCKET_NAME", utils.DlpUploadBucket, path) + }, +} + +func init() { + buildServerlessEnvCmd.Flags().StringVar(&sstVariables, "sst", "", "Variables related to serverless stack (sst)") + buildServerlessEnvCmd.Flags().StringVar(&devEndpoints, "dev-endpoints", "", "Development endpoints for serverless") + serverless.ServerlessCmd.AddCommand(buildServerlessEnvCmd) +} diff --git a/go.mod b/go.mod index 7d18049..3120d89 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,8 @@ require ( ) require ( + github.com/aws/aws-sdk-go v1.45.3 // indirect + github.com/jmespath/go-jmespath v0.4.0 // indirect atomicgo.dev/cursor v0.2.0 // indirect atomicgo.dev/keyboard v0.2.9 // indirect atomicgo.dev/schedule v0.1.0 // indirect diff --git a/go.sum b/go.sum index 6c9a1b1..a93c4ae 100644 --- a/go.sum +++ b/go.sum @@ -44,6 +44,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/aws/aws-sdk-go v1.45.3 h1:Q8BksXg2ZUu2dCbA62+UCEtfvqsW8EO4tzt2IVeYAws= +github.com/aws/aws-sdk-go v1.45.3/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= github.com/MarvinJWendt/testza v0.1.0/go.mod h1:7AxNvlfeHP7Z/hDQ5JtE3OKYT3XFUeLCDE2DQninSqs= github.com/MarvinJWendt/testza v0.2.1/go.mod h1:God7bhG8n6uQxwdScay+gjm9/LnO4D3kkcZX4hv9Rp8= github.com/MarvinJWendt/testza v0.2.8/go.mod h1:nwIcjmr0Zz+Rcwfh3/4UhBp7ePKVhuBExvZqnKYWlII= @@ -149,6 +151,9 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= @@ -550,7 +555,7 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/main.go b/main.go index 078cbb5..5cc75ce 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,14 @@ import ( _ "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/serverless/functions/remove" _ "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/serverless/install" _ "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/serverless/start" + _ "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend/pull_config" + _ "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend/add_param" + _ "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend/get_secret" + _ "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend/update_param" + _ "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend/remove_param" + _ "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/frontend/build_frontend_env" + _ "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend/build_training_env" + _ "github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/serverless/build_serverless_env" ) func main() { diff --git a/utils/constants.go b/utils/constants.go new file mode 100644 index 0000000..3fcd327 --- /dev/null +++ b/utils/constants.go @@ -0,0 +1,8 @@ +// Package utils provides utility constants for the application. +package utils + +// AwsRegion specifies the AWS region to use for services. +const AwsRegion = "us-west-2" + +// DlpUploadBucket specifies the name of the bucket to use for DLP uploads. +const DlpUploadBucket = "dlp-upload-bucket" diff --git a/utils/envfile.go b/utils/envfile.go new file mode 100644 index 0000000..890357f --- /dev/null +++ b/utils/envfile.go @@ -0,0 +1,34 @@ +package utils + +import ( + "fmt" + "os" + "strings" +) + +func WriteToEnvFile(paramName string, paramValue string, path string) error { + if err := os.MkdirAll(path, os.ModePerm); err != nil { + return fmt.Errorf("error creating directory: %v", err) + } + + content := strings.ToUpper(paramName) + "=" + paramValue + f, err := os.OpenFile(path+"/.env", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return fmt.Errorf("error creating .env file: %v", err) + } + defer f.Close() + + fmt.Println("Writing to .env file", path) + n, err := f.WriteString(content + "\n") + if err != nil { + return fmt.Errorf("error writing to .env file: %v", err) + } + fmt.Printf("Wrote %d bytes to .env file\n", n) + + err = f.Sync() + if err != nil { + return fmt.Errorf("error syncing .env file: %v", err) + } + + return nil +}