-
Notifications
You must be signed in to change notification settings - Fork 11
feat(commands): add non-interactive mode and Discover function #263
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
base: refactor/abstract-fs-3-discovery
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import ( | |
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "io/fs" | ||
| "os" | ||
| "path" | ||
|
|
||
|
|
@@ -23,28 +24,48 @@ type contextKey string | |
| var FlavorKey contextKey = "flavor" | ||
|
|
||
| func NewPlatformifyCmd(assets *vendorization.VendorAssets) *cobra.Command { | ||
| var noInteraction bool | ||
| cmd := &cobra.Command{ | ||
| Use: assets.Use, | ||
| Aliases: []string{"ify"}, | ||
| Short: fmt.Sprintf("Creates starter YAML files for your %s project", assets.ServiceName), | ||
| SilenceUsage: true, | ||
| SilenceErrors: true, | ||
| RunE: func(cmd *cobra.Command, _ []string) error { | ||
| return Platformify(cmd.Context(), cmd.OutOrStderr(), cmd.ErrOrStderr(), assets) | ||
| return Platformify( | ||
| cmd.Context(), | ||
| cmd.OutOrStderr(), | ||
| cmd.ErrOrStderr(), | ||
| noInteraction, | ||
| assets, | ||
| ) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().BoolVar(&noInteraction, "no-interaction", false, "Disable interactive prompts") | ||
| return cmd | ||
| } | ||
|
|
||
| func Platformify(ctx context.Context, stdout, stderr io.Writer, assets *vendorization.VendorAssets) error { | ||
| answers := models.NewAnswers() | ||
| answers.Flavor, _ = ctx.Value(FlavorKey).(string) | ||
| ctx = models.ToContext(ctx, answers) | ||
| ctx = colors.ToContext(ctx, stdout, stderr) | ||
| // Discover detects project configuration non-interactively. | ||
| // It is the entry point for programmatic callers like source-integration-apps. | ||
| func Discover( | ||
| ctx context.Context, | ||
| flavor string, | ||
| noInteraction bool, | ||
| fileSystem fs.FS, | ||
| ) (*platformifier.UserInput, error) { | ||
| answers, _ := models.FromContext(ctx) | ||
| if answers == nil { | ||
| answers = models.NewAnswers() | ||
| ctx = models.ToContext(ctx, answers) | ||
| } | ||
| answers.Flavor = flavor | ||
| answers.NoInteraction = noInteraction | ||
| if fileSystem != nil { | ||
| answers.WorkingDirectory = fileSystem | ||
| } | ||
| q := questionnaire.New( | ||
| &question.WorkingDirectory{}, | ||
| &question.FilesOverwrite{}, | ||
| &question.Welcome{}, | ||
| &question.Stack{}, | ||
| &question.Type{}, | ||
|
|
@@ -63,23 +84,48 @@ func Platformify(ctx context.Context, stdout, stderr io.Writer, assets *vendoriz | |
| ) | ||
| err := q.AskQuestions(ctx) | ||
| if errors.Is(err, questionnaire.ErrSilent) { | ||
| return nil | ||
| return nil, nil | ||
| } | ||
|
|
||
| if err != nil { | ||
| fmt.Fprintln(stderr, colors.Colorize(colors.ErrorCode, err.Error())) | ||
| return err | ||
| return nil, err | ||
| } | ||
|
|
||
| input := answers.ToUserInput() | ||
| return answers.ToUserInput(), nil | ||
| } | ||
|
|
||
| func Platformify( | ||
| ctx context.Context, | ||
| stdout, stderr io.Writer, | ||
| noInteraction bool, | ||
| assets *vendorization.VendorAssets, | ||
| ) error { | ||
| ctx = colors.ToContext(ctx, stdout, stderr) | ||
| ctx = models.ToContext(ctx, models.NewAnswers()) | ||
| input, err := Discover(ctx, assets.ConfigFlavor, noInteraction, nil) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
Comment on lines
+103
to
+108
|
||
| if input == nil { | ||
| return nil | ||
| } | ||
| answers, _ := models.FromContext(ctx) | ||
| pfier := platformifier.New(input, assets.ConfigFlavor) | ||
| configFiles, err := pfier.Platformify(ctx) | ||
| if err != nil { | ||
| fmt.Fprintln(stderr, colors.Colorize(colors.ErrorCode, err.Error())) | ||
| return fmt.Errorf("could not configure project: %w", err) | ||
| } | ||
|
|
||
| filesToCreateUpdate := make([]string, 0, len(configFiles)) | ||
| for file := range configFiles { | ||
| filesToCreateUpdate = append(filesToCreateUpdate, file) | ||
| } | ||
|
|
||
| filesOverwrite := question.FilesOverwrite{FilesToCreateUpdate: filesToCreateUpdate} | ||
| if err := filesOverwrite.Ask(ctx); err != nil { | ||
|
Comment on lines
+119
to
+125
|
||
| return err | ||
| } | ||
|
|
||
| for file, contents := range configFiles { | ||
| filePath := path.Join(answers.Cwd, file) | ||
| if err := os.MkdirAll(path.Dir(filePath), os.ModeDir|os.ModePerm); err != nil { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,17 +14,11 @@ func (q *Environment) Ask(ctx context.Context) error { | |
| return nil | ||
| } | ||
|
|
||
| answers.Environment = make(map[string]string) | ||
| for _, dm := range answers.DependencyManagers { | ||
| switch dm { | ||
| case models.Poetry: | ||
| answers.Environment["POETRY_VERSION"] = "1.8.4" | ||
| answers.Environment["POETRY_VIRTUALENVS_IN_PROJECT"] = "true" | ||
| case models.Pipenv: | ||
| answers.Environment["PIPENV_TOOL_VERSION"] = "2024.2.0" | ||
| answers.Environment["PIPENV_VENV_IN_PROJECT"] = "1" | ||
| } | ||
| environment, err := answers.Discoverer.Environment() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| answers.Environment = environment | ||
|
Comment on lines
+17
to
+22
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,28 +13,43 @@ import ( | |
| "github.com/platformsh/platformify/vendorization" | ||
| ) | ||
|
|
||
| type FilesOverwrite struct{} | ||
| // FilesOverwrite prompts the user to confirm overwriting existing config files. | ||
| // If FilesToCreateUpdate is set, those files are checked instead of the | ||
| // default proprietary files list. | ||
| type FilesOverwrite struct { | ||
| FilesToCreateUpdate []string | ||
| } | ||
|
Comment on lines
+16
to
+21
|
||
|
|
||
| func (q *FilesOverwrite) Ask(ctx context.Context) error { | ||
| answers, ok := models.FromContext(ctx) | ||
| if !ok { | ||
| return nil | ||
| } | ||
|
|
||
| if answers.NoInteraction { | ||
| return nil | ||
| } | ||
|
|
||
| _, stderr, ok := colors.FromContext(ctx) | ||
| if !ok { | ||
| return nil | ||
| } | ||
|
|
||
| assets, _ := vendorization.FromContext(ctx) | ||
| existingFiles := make([]string, 0, len(assets.ProprietaryFiles())) | ||
| for _, p := range assets.ProprietaryFiles() { | ||
| filesToCheck := q.FilesToCreateUpdate | ||
| if len(filesToCheck) == 0 { | ||
| assets, _ := vendorization.FromContext(ctx) | ||
| filesToCheck = assets.ProprietaryFiles() | ||
| } | ||
|
|
||
| existingFiles := make([]string, 0, len(filesToCheck)) | ||
| for _, p := range filesToCheck { | ||
| if st, err := fs.Stat(answers.WorkingDirectory, p); err == nil && !st.IsDir() { | ||
| existingFiles = append(existingFiles, p) | ||
| } | ||
| } | ||
|
|
||
| if len(existingFiles) > 0 { | ||
| assets, _ := vendorization.FromContext(ctx) | ||
| fmt.Fprintln( | ||
| stderr, | ||
| colors.Colorize( | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -28,7 +28,7 @@ func (q *Type) Ask(ctx context.Context) error { | |||||||||||||||||||
| return | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if answers.Stack != models.GenericStack && answers.Type.Runtime != nil { | ||||||||||||||||||||
| if answers.Type.Runtime != nil && answers.Type.Runtime.Title() != "" { | ||||||||||||||||||||
| fmt.Fprintf( | ||||||||||||||||||||
| stderr, | ||||||||||||||||||||
| "%s %s\n", | ||||||||||||||||||||
|
|
@@ -41,12 +41,29 @@ func (q *Type) Ask(ctx context.Context) error { | |||||||||||||||||||
| } | ||||||||||||||||||||
| }() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| runtime := models.RuntimeForStack(answers.Stack) | ||||||||||||||||||||
| if runtime == nil { | ||||||||||||||||||||
| typ, err := answers.Discoverer.Type() | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| runtime, _ := models.Runtimes.RuntimeByType(typ) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if answers.NoInteraction { | ||||||||||||||||||||
|
Comment on lines
+48
to
+50
|
||||||||||||||||||||
| runtime, _ := models.Runtimes.RuntimeByType(typ) | |
| if answers.NoInteraction { | |
| runtime, runtimeErr := models.Runtimes.RuntimeByType(typ) | |
| if answers.NoInteraction { | |
| if runtimeErr != nil { | |
| return fmt.Errorf("unsupported runtime type %s: %w", typ, runtimeErr) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Discover() docstring says it detects configuration “non-interactively”, but the function accepts noInteraction=false and will still run interactive questions. Consider updating the comment to reflect actual behavior (or enforce non-interactive behavior inside Discover).