Skip to content

Commit

Permalink
fix env bug
Browse files Browse the repository at this point in the history
  • Loading branch information
AshutoshPatole committed Aug 11, 2024
1 parent c19d914 commit 38ae2fc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*.json
.env.production
cmd/.env.production
37 changes: 29 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package cmd

import (
"embed"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -27,15 +28,7 @@ var rootCmd = &cobra.Command{
logrus.SetLevel(logrus.InfoLevel)
}

// load env
err := godotenv.Load(".env.production")
if err != nil {
return
}
},
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -47,10 +40,38 @@ func Execute() {
}
}

//go:embed .env.production
var envFile embed.FS

func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ssm-v2.yaml)")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "toggle debug logs")

// Note: Not sure if this is right method, but I am finding it difficult
// to handle .env files and the firebase config file
data, err := envFile.ReadFile(".env.production")
if err != nil {
logrus.Errorf("error reading embedded env file: %v", err)
}

// Write the embedded data to a temporary file and load it with dotenv
tempFile, err := os.CreateTemp("", ".env")
if err != nil {
logrus.Errorf("error creating temporary file: %v", err)
}
defer func(tempFile *os.File) {
_ = tempFile.Close()
}(tempFile)

if _, err := tempFile.Write(data); err != nil {
logrus.Errorf("error writing to temporary file: %v", err)
}

err = godotenv.Load(tempFile.Name())
if err != nil {
return
}
}

// initConfig reads in config file and ENV variables if set.
Expand Down

0 comments on commit 38ae2fc

Please sign in to comment.