Skip to content
Merged
51 changes: 40 additions & 11 deletions cli/cmd/env/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package env
import (
"context"
"fmt"
"os"
"slices"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/gitutil"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/rilldata/rill/runtime/compilers/rillv1"
"github.com/rilldata/rill/runtime/compilers/rillv1beta"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/pkg/activity"
"github.com/rilldata/rill/runtime/pkg/fileutil"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -187,11 +191,8 @@ func VariablesFlow(ctx context.Context, ch *cmdutil.Helper, projectPath string)
}

fmt.Printf("\nConfiguring connector %q:\n", c.Name)
if c.Spec.ServiceAccountDocs != "" {
fmt.Printf("For instructions on how to create a service account, see: %s\n", c.Spec.ServiceAccountDocs)
}
if c.Spec.Help != "" {
fmt.Println(c.Spec.Help)
if c.Spec.DocsURL != "" {
fmt.Printf("For instructions on how to configure, see: %s\n", c.Spec.DocsURL)
}

for i := range c.Spec.ConfigProperties {
Expand All @@ -210,12 +211,9 @@ func VariablesFlow(ctx context.Context, ch *cmdutil.Helper, projectPath string)
question.Prompt = &survey.Input{Message: msg, Default: prop.Default}
}

if prop.TransformFunc != nil {
question.Transform = prop.TransformFunc
}

if prop.ValidateFunc != nil {
question.Validate = prop.ValidateFunc
if prop.Type == drivers.FilePropertyType {
question.Transform = fileTransformFunc
question.Validate = fileValidateFunc
}

answer := ""
Expand All @@ -237,3 +235,34 @@ func VariablesFlow(ctx context.Context, ch *cmdutil.Helper, projectPath string)

return variables, nil
}

func fileValidateFunc(any interface{}) error {
val := any.(string)
if val == "" {
// user can chhose to leave empty for public sources
return nil
}

path, err := fileutil.ExpandHome(strings.TrimSpace(val))
if err != nil {
return err
}

_, err = os.Stat(path)
return err
}

func fileTransformFunc(any interface{}) interface{} {
val := any.(string)
if val == "" {
return ""
}

path, err := fileutil.ExpandHome(strings.TrimSpace(val))
if err != nil {
return err
}
// ignoring error since PathError is already validated
content, _ := os.ReadFile(path)
return string(content)
}
Loading