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

fix: target set file path improvements #1193

Merged
merged 1 commit into from
Oct 4, 2024
Merged
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
33 changes: 26 additions & 7 deletions pkg/views/target/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package target
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -110,7 +111,12 @@ func SetTargetForm(target *apiclient.ProviderTarget, targetManifest map[string]a
fields = append(fields, selectField)
options[name] = value
case apiclient.ProviderTargetPropertyTypeFilePath:
group, value := getFilePicker(name, property)
var initialValue *string
v, ok := options[name].(string)
if ok {
initialValue = &v
}
group, value := getFilePicker(name, property, initialValue)
groups = append(groups, group...)
options[name] = value
}
Expand Down Expand Up @@ -219,7 +225,7 @@ func getConfirm(name string, property apiclient.ProviderProviderTargetProperty,
Value(&value), &value
}

func getFilePicker(name string, property apiclient.ProviderProviderTargetProperty) ([]*huh.Group, *string) {
func getFilePicker(name string, property apiclient.ProviderProviderTargetProperty, initialValue *string) ([]*huh.Group, *string) {
dirPath := "~"

if property.DefaultValue != nil {
Expand All @@ -244,9 +250,15 @@ func getFilePicker(name string, property apiclient.ProviderProviderTargetPropert
}
}

var value *string = new(string)
if initialValue != nil {
*value = *initialValue
}

customPathInput := huh.NewInput().
Title(name).
Description(*property.Description).
Value(value).
Validate(func(filePath string) error {
fileInfo, err := os.Stat(filePath)
if os.IsNotExist(err) {
Expand All @@ -263,23 +275,30 @@ func getFilePicker(name string, property apiclient.ProviderProviderTargetPropert
})

if len(options) == 0 {
return []*huh.Group{}, nil
return []*huh.Group{huh.NewGroup(customPathInput)}, value
}

options = append(options, huh.NewOption("Custom path", "custom-path"))
options = append(options, huh.NewOption("None", "none"))

var value *string = new(string)
description := fmt.Sprintf("%s\nShowing files in: %s\nYou can select a file, choose None or enter a Custom path", *property.Description, dirPath)

return []*huh.Group{
huh.NewGroup(
huh.NewSelect[string]().
Title(name).
Description(description).
Options(options...).
Value(value),
).WithHeight(9),
Value(value).Validate(func(s string) error {
if s == "custom-path" {
*value = ""
}
return nil
}).
WithHeight(10 + strings.Count(description, "\n")),
),
huh.NewGroup(customPathInput).WithHideFunc(func() bool {
return *value != "custom-path"
return *value != ""
}),
}, value
}
Loading