Skip to content
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
69 changes: 37 additions & 32 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/windsorcli/cli/pkg/context"
"github.com/windsorcli/cli/pkg/di"
"github.com/windsorcli/cli/pkg/runtime"
)

var envCmd = &cobra.Command{
Expand All @@ -15,57 +15,61 @@ var envCmd = &cobra.Command{
Long: "Output commands to set environment variables for the application.",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
// Get flags
hook, _ := cmd.Flags().GetBool("hook")
decrypt, _ := cmd.Flags().GetBool("decrypt")
verbose, _ := cmd.Flags().GetBool("verbose")

// Set NO_CACHE=true unless --hook is specified or NO_CACHE is already set
if !hook && os.Getenv("NO_CACHE") == "" {
if err := os.Setenv("NO_CACHE", "true"); err != nil {
return fmt.Errorf("failed to set NO_CACHE environment variable: %w", err)
}
}

// Create dependencies with injector from command context
deps := &runtime.Dependencies{
Injector: cmd.Context().Value(injectorKey).(di.Injector),
injector := cmd.Context().Value(injectorKey).(di.Injector)

execCtx := &context.ExecutionContext{
Injector: injector,
}

execCtx, err := context.NewContext(execCtx)
if err != nil {
return fmt.Errorf("failed to initialize context: %w", err)
}

if err := execCtx.CheckTrustedDirectory(); err != nil {
return fmt.Errorf("not in a trusted directory. If you are in a Windsor project, run 'windsor init' to approve")
}

if err := execCtx.HandleSessionReset(); err != nil {
return err
}

if err := execCtx.LoadConfig(); err != nil {
return err
}

if err := execCtx.LoadEnvironment(decrypt); err != nil {
return fmt.Errorf("failed to load environment: %w", err)
}

// Create output function for environment variables and aliases
outputFunc := func(output string) {
fmt.Fprint(cmd.OutOrStdout(), output)
if output != "" {
fmt.Fprint(cmd.OutOrStdout(), output)
}
}

// Execute the complete workflow
rt := runtime.NewRuntime(deps).
LoadShell().
CheckTrustedDirectory().
HandleSessionReset().
LoadConfig().
LoadSecretsProviders().
LoadEnvVars(runtime.EnvVarsOptions{
Decrypt: decrypt,
Verbose: verbose,
}).
PrintEnvVars(runtime.EnvVarsOptions{
Verbose: verbose,
Export: hook,
OutputFunc: outputFunc,
})

// Only print aliases in hook mode
if hook {
rt = rt.PrintAliases(outputFunc)
outputFunc(execCtx.PrintEnvVarsExport())
outputFunc(execCtx.PrintAliases())
} else {
outputFunc(execCtx.PrintEnvVars())
}

if err := rt.ExecutePostEnvHook(verbose).Do(); err != nil {
if hook {
// In hook mode, return success even if there are errors
// This prevents shell initialization failures from breaking the environment
if err := execCtx.ExecutePostEnvHooks(); err != nil {
if hook || !verbose {
return nil
}
return fmt.Errorf("Error executing environment workflow: %w", err)
return err
}

return nil
Expand All @@ -75,5 +79,6 @@ var envCmd = &cobra.Command{
func init() {
envCmd.Flags().Bool("decrypt", false, "Decrypt secrets before setting environment variables")
envCmd.Flags().Bool("hook", false, "Flag that indicates the command is being executed by the hook")
envCmd.Flags().Bool("verbose", false, "Show verbose error output")
rootCmd.AddCommand(envCmd)
}
Loading
Loading