Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmd): expand project env from anywhere #475

Merged
merged 1 commit into from
Dec 11, 2024
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
37 changes: 20 additions & 17 deletions cmd/lk/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ var (
},
{
Name: "env",
Usage: "Print project environment variables expanded from a .env.example file",
Usage: "Expand environment variables from the current project, and if present, the .env.example file",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "w",
Expand All @@ -112,22 +112,7 @@ var (
},
ArgsUsage: "[DIR] location of the project directory (default: current directory)",
Before: requireProject,
Action: func(ctx context.Context, cmd *cli.Command) error {
rootDir := cmd.Args().First()
if rootDir == "" {
rootDir = "."
}

env, err := instantiateEnv(ctx, cmd, rootDir, nil)
if err != nil {
return err
}
if cmd.Bool("write") {
return bootstrap.WriteDotEnv(rootDir, env)
} else {
return bootstrap.PrintDotEnv(env)
}
},
Action: manageEnv,
},
},
},
Expand Down Expand Up @@ -363,6 +348,24 @@ func cleanupTemplate(ctx context.Context, cmd *cli.Command, appName string) erro
return bootstrap.CleanupTemplate(appName)
}

func manageEnv(ctx context.Context, cmd *cli.Command) error {
rootDir := cmd.Args().First()
if rootDir == "" {
rootDir = "."
}

env, err := instantiateEnv(ctx, cmd, rootDir, nil)
if err != nil {
return err
}

if cmd.Bool("write") {
return bootstrap.WriteDotEnv(rootDir, env)
} else {
return bootstrap.PrintDotEnv(env)
}
}

func instantiateEnv(ctx context.Context, cmd *cli.Command, rootPath string, addlEnv *map[string]string) (map[string]string, error) {
env := map[string]string{
"LIVEKIT_API_KEY": project.APIKey,
Expand Down
56 changes: 29 additions & 27 deletions pkg/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,40 +192,42 @@ type PromptFunc func(key string, value string) (string, error)
// prompting for others, and returning the result as a map.
func InstantiateDotEnv(ctx context.Context, rootDir string, substitutions map[string]string, verbose bool, prompt PromptFunc) (map[string]string, error) {
promptedVars := map[string]string{}

envExamplePath := path.Join(rootDir, EnvExampleFile)

stat, err := os.Stat(envExamplePath)
if err != nil {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
if stat.IsDir() {
return nil, errors.New("env.example file is a directory")
}
} else if stat != nil {
if stat.IsDir() {
return nil, errors.New("env.example file is a directory")
}

envMap, err := godotenv.Read(envExamplePath)
if err != nil {
return nil, err
}
envMap, err := godotenv.Read(envExamplePath)
if err != nil {
return nil, err
}

for key, oldValue := range envMap {
// if key is a substitution, replace it
if value, ok := substitutions[key]; ok {
envMap[key] = value
// if key was already promped, use that value
} else if alreadyPromptedValue, ok := promptedVars[key]; ok {
envMap[key] = alreadyPromptedValue
} else {
// prompt for value
newValue, err := prompt(key, oldValue)
if err != nil {
return nil, err
for key, oldValue := range envMap {
// if key is a substitution, replace it
if value, ok := substitutions[key]; ok {
envMap[key] = value
// if key was already promped, use that value
} else if alreadyPromptedValue, ok := promptedVars[key]; ok {
envMap[key] = alreadyPromptedValue
} else {
// prompt for value
newValue, err := prompt(key, oldValue)
if err != nil {
return nil, err
}
envMap[key] = newValue
promptedVars[key] = newValue
}
envMap[key] = newValue
promptedVars[key] = newValue
}
return envMap, nil
} else {
return substitutions, nil
}

return envMap, nil
}

func PrintDotEnv(envMap map[string]string) error {
Expand All @@ -243,7 +245,7 @@ func WriteDotEnv(rootDir string, envMap map[string]string) error {
return err
}
envLocalPath := path.Join(rootDir, EnvLocalFile)
return os.WriteFile(envLocalPath, []byte(envContents), 0700)
return os.WriteFile(envLocalPath, []byte(envContents+"\n"), 0700)
}

func CloneTemplate(url, dir string) (string, string, error) {
Expand Down
Loading