dotenv

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 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
		}

		screen := editorScreen

		dotenvFile := filepath.Join(cwd, ".env")
		templateLines, templateFileUsed := readTemplate(dotenvFile)

		variables := parseVariablesOnly(templateLines)
		contents := strings.Join(templateLines, "")

		if repo.IsInRepo() {
			repoRoot, err := repo.FindRepoRoot()
			if err != nil {
				log.Fatalf("Error determining repo root: %v", err)
			}

			userPrompts, _, err := envbuilder.GatherUserPrompts(repoRoot)
			if err != nil {
				log.Fatalf("Error gathering user prompts: %v", err)
			}

			existingEnvVarsSet := make(map[string]struct{})

			for _, value := range variables {
				existingEnvVarsSet[value.name] = struct{}{}
			}

			extraEnvVarsFound := false

			for _, up := range userPrompts {
				_, exists := existingEnvVarsSet[up.Env]

				if !exists {
					extraEnvVarsFound = true

					existingEnvVarsSet[up.Env] = struct{}{}

					variables = append(variables, variable{name: up.Env, value: up.Default})
				}
			}

			if extraEnvVarsFound {
				fmt.Println("Environment Configuration")
				fmt.Println("=========================")
				fmt.Println("")
				fmt.Println("Editing .env file with component-specific variables...")
				fmt.Println("")
				for _, fv := range userPrompts {
					fmt.Println(fv.Env + " = " + fv.Default)
				}
				fmt.Println("")
				fmt.Println("Configure required missing variables now? (y/N): ")

				reader := bufio.NewReader(os.Stdin)
				selectedOption, err := reader.ReadString('\n')
				if err != nil {
					return err
				}

				if strings.ToLower(strings.TrimSpace(selectedOption)) == "y" {
					screen = wizardScreen
				}
			}
		}

		m := Model{
			initialScreen:  screen,
			DotenvFile:     dotenvFile,
			DotenvTemplate: templateFileUsed,
			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: "Edit .env file using setup wizard",
	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")
		templateLines, templateFileUsed := readTemplate(dotenvFile)
		variables, contents, _ := variablesFromTemplate(templateLines)

		m := Model{
			initialScreen:  wizardScreen,
			DotenvFile:     dotenvFile,
			DotenvTemplate: templateFileUsed,
			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 UpdateCmd = &cobra.Command{
	Use:   "update",
	Short: "Automatically update Datarobot credentials in .env file",
	Long:  "Automatically populate .env file with fresh Datarobot credentials",
	PreRunE: func(cmd *cobra.Command, _ []string) error {
		return auth.EnsureAuthenticatedE(cmd.Context())
	},
	Run: func(_ *cobra.Command, _ []string) {
		dotenvFile := ".env"

		_, _, _, err := writeUsingTemplateFile(dotenvFile)
		if err != nil {
			log.Error(err)
		}
	},
}

Functions

func Cmd

func Cmd() *cobra.Command

Types

type Model

type Model struct {
	DotenvFile     string
	DotenvTemplate 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