dotenv

package
v0.2.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2025 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EditCmd = &cobra.Command{
	Use:   "edit",
	Short: "✏️ Edit '.env' file using built-in editor.",
	RunE: func(cmd *cobra.Command, _ []string) error {
		if viper.GetBool("debug") {
			f, err := tea.LogToFile("tea-debug.log", "debug")
			if err != nil {
				fmt.Println("fatal: ", err)
				os.Exit(1)
			}
			defer f.Close()
		}

		cwd, err := os.Getwd()
		if err != nil {
			return err
		}

		dotenvFile := filepath.Join(cwd, ".env")
		dotenvFileLines, contents := readDotenvFile(dotenvFile)

		variables := envbuilder.ParseVariablesOnly(dotenvFileLines)

		screen := editorScreen
		if repo.IsInRepo() {
			if handleExtraEnvVars(variables) {
				screen = wizardScreen
			}
		}

		m := Model{
			initialScreen: screen,
			DotenvFile:    dotenvFile,
			variables:     variables,
			contents:      contents,
			SuccessCmd:    tea.Quit,
		}
		p := tea.NewProgram(
			tui.NewInterruptibleModel(m),
			tea.WithAltScreen(),
			tea.WithContext(cmd.Context()),
		)
		_, err = p.Run()

		return err
	},
}
View Source
var SetupCmd = &cobra.Command{
	Use:   "setup",
	Short: "🧙 Environment configuration wizard.",
	Long: `Launch the interactive environment configuration wizard.

This wizard will help you:
  1️⃣  Review required environment variables
  2️⃣  Configure API keys and credentials
  3️⃣  Set up database connections (if needed)
  4️⃣  Validate your configuration

💡 Perfect for first-time setup or when adding new integrations.`,
	PreRunE: func(cmd *cobra.Command, _ []string) error {
		return auth.EnsureAuthenticatedE(cmd.Context())
	},
	Run: func(cmd *cobra.Command, _ []string) {
		if viper.GetBool("debug") {
			f, err := tea.LogToFile("tea-debug.log", "debug")
			if err != nil {
				fmt.Println("fatal: ", err)
				os.Exit(1)
			}
			defer f.Close()
		}

		repositoryRoot, err := ensureInRepo()
		if err != nil {
			os.Exit(1)
		}
		dotenvFile := filepath.Join(repositoryRoot, ".env")

		ifNeeded, _ := cmd.Flags().GetBool("if-needed")
		if ifNeeded {
			if _, err := os.Stat(dotenvFile); err == nil {

				dotenvFileLines, _ := readDotenvFile(dotenvFile)
				variables := envbuilder.ParseVariablesOnly(dotenvFileLines)

				hasContent := false
				for _, v := range variables {
					if v.Value != "" {
						hasContent = true
						break
					}
				}

				if hasContent {
					fmt.Println("Configuration already exists, skipping setup.")
					return
				}
			}
		}

		dotenvFileLines, _ := readDotenvFile(dotenvFile)
		variables, contents := envbuilder.VariablesFromLines(dotenvFileLines)

		m := Model{
			initialScreen: wizardScreen,
			DotenvFile:    dotenvFile,
			variables:     variables,
			contents:      contents,
			SuccessCmd:    tea.Quit,
		}
		p := tea.NewProgram(
			tui.NewInterruptibleModel(m),
			tea.WithAltScreen(),
			tea.WithContext(cmd.Context()),
		)
		_, err = p.Run()
		if err != nil {
			fmt.Printf("Error: %v\n", err)
			os.Exit(1)
		}

		_ = state.UpdateAfterDotenvSetup()
	},
}
View Source
var UpdateCmd = &cobra.Command{
	Use:   "update",
	Short: "🔄 Automatically update DataRobot credentials.",
	Long: `Automatically update your '.env' file with fresh DataRobot credentials.

This command will:
  • Refresh your DataRobot API credentials
  • Update environment variables automatically
  • Preserve your existing custom settings

💡 Use this when your credentials expire or you need to refresh your connection.`,
	PreRunE: func(cmd *cobra.Command, _ []string) error {
		return auth.EnsureAuthenticatedE(cmd.Context())
	},
	Run: func(_ *cobra.Command, _ []string) {
		dotenv, err := ensureInRepoWithDotenv()
		if err != nil {
			os.Exit(1)
		}

		_, _, err = updateDotenvFile(dotenv)
		if err != nil {
			log.Error(err)
			os.Exit(1)
		}
	},
}
View Source
var ValidateCmd = &cobra.Command{
	Use:   "validate",
	Short: "Validate '.env' and environment variable configuration against required settings.",
	Run: func(_ *cobra.Command, _ []string) {
		dotenv, err := ensureInRepoWithDotenv()
		if err != nil {
			os.Exit(1)
		}

		repoRoot := filepath.Dir(dotenv)

		dotenvFileLines, _ := readDotenvFile(dotenv)

		parsedVars := envbuilder.ParseVariablesOnly(dotenvFileLines)

		result := envbuilder.ValidateEnvironment(repoRoot, parsedVars)

		varStyle := lipgloss.NewStyle().Foreground(tui.DrPurple).Bold(true)
		valueStyle := lipgloss.NewStyle().Foreground(tui.DrGreen)

		fmt.Println("\nValidating required variables:")

		for _, valResult := range result.Results {
			if valResult.Valid {
				fmt.Printf("  %s: %s\n",
					varStyle.Render(valResult.Field),
					valueStyle.Render(valResult.Value))
			}
		}

		if result.HasErrors() {
			fmt.Println("\nValidation errors:")

			for _, valResult := range result.Results {
				if !valResult.Valid {
					fmt.Printf("\n%s: Required variable %s is not set\n",
						tui.ErrorStyle.Render("Error"), varStyle.Render(valResult.Field))

					if valResult.Help != "" {
						fmt.Printf("  Description: %s\n", valResult.Help)
					}

					fmt.Println("  Set this variable in your '.env' file or run `dr dotenv setup` to configure it.")
				}
			}

			os.Exit(1)
		}

		fmt.Println("\nValidation passed: all required variables are set.")
	},
}

Functions

func Cmd

func Cmd() *cobra.Command

Types

type Model

type Model struct {
	DotenvFile string

	SuccessCmd tea.Cmd
	// contains filtered or unexported fields
}

func (Model) Init

func (m Model) Init() tea.Cmd

func (Model) Update

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd)

func (Model) View

func (m Model) View() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL