Skip to content

Commit

Permalink
Issue #192: Added an argument to disable .env loading
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed Jul 25, 2024
1 parent 6a1645a commit bfcec4c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/cmd/project_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func getProjectRunner(process []string, noDeps bool, mainProcess string, mainPro
if *pcFlags.HideDisabled {
opts.AddAdmitter(&admitter.DisabledProcAdmitter{})
}
if *pcFlags.DisableDotEnv {
opts.DisableDotenv()
}
project, err := loader.Load(opts)
if err != nil {
log.Fatal().Err(err).Msg("Failed to load project")
Expand Down
1 change: 1 addition & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func init() {
rootCmd.Flags().StringArrayVarP(&nsAdmitter.EnabledNamespaces, "namespace", "n", nil, "run only specified namespaces (default all)")
rootCmd.PersistentFlags().StringVarP(pcFlags.LogFile, "log-file", "L", *pcFlags.LogFile, "Specify the log file path (env: "+config.LogPathEnvVarName+")")
rootCmd.PersistentFlags().BoolVar(pcFlags.IsReadOnlyMode, "read-only", *pcFlags.IsReadOnlyMode, "enable read-only mode (env: "+config.EnvVarReadOnlyMode+")")
rootCmd.Flags().BoolVar(pcFlags.DisableDotEnv, "disable-dotenv", *pcFlags.DisableDotEnv, "disable .env file loading (env: "+config.EnvVarDisableDotEnv+"=1)")
rootCmd.Flags().AddFlag(commonFlags.Lookup(flagReverse))
rootCmd.Flags().AddFlag(commonFlags.Lookup(flagSort))
rootCmd.Flags().AddFlag(commonFlags.Lookup(flagTheme))
Expand Down
1 change: 1 addition & 0 deletions src/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ func init() {

runCmd.Flags().BoolVarP(pcFlags.NoDependencies, "no-deps", "", *pcFlags.NoDependencies, "don't start dependent processes")
runCmd.Flags().AddFlag(rootCmd.Flags().Lookup("config"))
runCmd.Flags().AddFlag(rootCmd.Flags().Lookup("disable-dotenv"))

}
1 change: 1 addition & 0 deletions src/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func init() {
upCmd.Flags().AddFlag(rootCmd.Flags().Lookup("ref-rate"))
upCmd.Flags().AddFlag(rootCmd.Flags().Lookup("tui"))
upCmd.Flags().AddFlag(rootCmd.Flags().Lookup("hide-disabled"))
upCmd.Flags().AddFlag(rootCmd.Flags().Lookup("disable-dotenv"))
upCmd.Flags().AddFlag(commonFlags.Lookup(flagReverse))
upCmd.Flags().AddFlag(commonFlags.Lookup(flagSort))
upCmd.Flags().AddFlag(commonFlags.Lookup(flagTheme))
Expand Down
3 changes: 3 additions & 0 deletions src/config/Flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
EnvVarNameNoServer = "PC_NO_SERVER"
EnvVarUnixSocketPath = "PC_SOCKET_PATH"
EnvVarReadOnlyMode = "PC_READ_ONLY"
EnvVarDisableDotEnv = "PC_DISABLE_DOTENV"
)

// Flags represents PC configuration flags.
Expand Down Expand Up @@ -64,6 +65,7 @@ type Flags struct {
IsUnixSocket *bool
IsReadOnlyMode *bool
OutputFormat *string
DisableDotEnv *bool
}

// NewFlags returns new configuration flags.
Expand All @@ -90,6 +92,7 @@ func NewFlags() *Flags {
IsUnixSocket: toPtr(false),
IsReadOnlyMode: toPtr(getReadOnlyDefault()),
OutputFormat: toPtr(""),
DisableDotEnv: toPtr(getDisableDotEnvDefault()),
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,8 @@ func getReadOnlyDefault() bool {
_, found := os.LookupEnv(EnvVarReadOnlyMode)
return found
}

func getDisableDotEnvDefault() bool {
_, found := os.LookupEnv(EnvVarDisableDotEnv)
return found
}
10 changes: 6 additions & 4 deletions src/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Load(opts *LoaderOptions) (*types.Project, error) {
}

for _, file := range opts.FileNames {
p := loadProjectFromFile(file)
p := loadProjectFromFile(file, opts.disableDotenv)
opts.projects = append(opts.projects, p)
}
mergedProject, err := merge(opts)
Expand Down Expand Up @@ -77,7 +77,7 @@ func admitProcesses(opts *LoaderOptions, p *types.Project) *types.Project {
return p
}

func loadProjectFromFile(inputFile string) *types.Project {
func loadProjectFromFile(inputFile string, disableDotEnv bool) *types.Project {
yamlFile, err := os.ReadFile(inputFile)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
Expand All @@ -86,8 +86,10 @@ func loadProjectFromFile(inputFile string) *types.Project {
log.Fatal().Err(err).Msgf("Failed to read %s", inputFile)
}

// .env is optional we don't care if it errors
_ = godotenv.Load()
if !disableDotEnv {
// .env is optional we don't care if it errors
_ = godotenv.Load()
}

const envEscaped = "##PC_ENV_ESCAPED##"
// replace escaped $$ env vars in yaml
Expand Down
13 changes: 9 additions & 4 deletions src/loader/loader_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

type LoaderOptions struct {
workingDir string
FileNames []string
projects []*types.Project
admitters []admitter.Admitter
workingDir string
FileNames []string
projects []*types.Project
admitters []admitter.Admitter
disableDotenv bool
}

func (o *LoaderOptions) AddAdmitter(adm ...admitter.Admitter) {
Expand All @@ -33,3 +34,7 @@ func (o *LoaderOptions) getWorkingDir() (string, error) {
}
return os.Getwd()
}

func (o *LoaderOptions) DisableDotenv() {
o.disableDotenv = true
}

0 comments on commit bfcec4c

Please sign in to comment.