Files
steampipe/pkg/workspace/load_workspace.go
2024-10-22 17:25:37 +05:30

68 lines
2.2 KiB
Go

package workspace
import (
"context"
"fmt"
"github.com/turbot/pipe-fittings/error_helpers"
"log"
"time"
"github.com/spf13/viper"
"github.com/turbot/steampipe/pkg/constants"
"github.com/turbot/steampipe/pkg/steampipeconfig/inputvars"
"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
"github.com/turbot/terraform-components/terraform"
)
func LoadWorkspacePromptingForVariables(ctx context.Context) (*Workspace, error_helpers.ErrorAndWarnings) {
t := time.Now()
defer func() {
log.Printf("[TRACE] Workspace load took %dms\n", time.Since(t).Milliseconds())
}()
w, inputVariables, errAndWarnings := LoadWorkspaceVars(ctx)
if errAndWarnings.GetError() != nil {
return w, errAndWarnings
}
// load the workspace mod
errAndWarnings = w.LoadWorkspaceMod(ctx, inputVariables)
return w, errAndWarnings
}
func promptForMissingVariables(ctx context.Context, missingVariables []*modconfig.Variable, workspacePath string) error {
fmt.Println() //nolint:forbidigo // UI formatting
fmt.Println("Variables defined with no value set.") //nolint:forbidigo // UI formatting
for _, v := range missingVariables {
variableName := v.ShortName
variableDisplayName := fmt.Sprintf("var.%s", v.ShortName)
// if this variable is NOT part of the workspace mod, add the mod name to the variable name
if v.Mod.ModPath != workspacePath {
variableDisplayName = fmt.Sprintf("%s.var.%s", v.ModName, v.ShortName)
variableName = fmt.Sprintf("%s.%s", v.ModName, v.ShortName)
}
r, err := promptForVariable(ctx, variableDisplayName, v.GetDescription())
if err != nil {
return err
}
addInteractiveVariableToViper(variableName, r)
}
return nil
}
func promptForVariable(ctx context.Context, name, description string) (string, error) {
uiInput := &inputvars.UIInput{}
rawValue, err := uiInput.Input(ctx, &terraform.InputOpts{
Id: name,
Query: name,
Description: description,
})
return rawValue, err
}
func addInteractiveVariableToViper(name string, rawValue string) {
varMap := viper.GetStringMap(constants.ConfigInteractiveVariables)
varMap[name] = rawValue
viper.Set(constants.ConfigInteractiveVariables, varMap)
}