Skip to content

Commit

Permalink
Merge pull request #229 from platformsh/feature/export-command
Browse files Browse the repository at this point in the history
Export the Platformify command to be used outside of a Cobra context
  • Loading branch information
akalipetis authored Aug 5, 2024
2 parents c69761f + 285a6b2 commit 979f5f6
Showing 1 changed file with 52 additions and 46 deletions.
98 changes: 52 additions & 46 deletions commands/platformify.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package commands

import (
"context"
"errors"
"fmt"
"io"

"github.com/spf13/cobra"

Expand All @@ -26,56 +28,60 @@ func NewPlatformifyCmd(assets *vendorization.VendorAssets) *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, _ []string) error {
answers := models.NewAnswers()
answers.Flavor, _ = cmd.Context().Value(FlavorKey).(string)
ctx := models.ToContext(cmd.Context(), answers)
ctx = colors.ToContext(
ctx,
cmd.OutOrStderr(),
cmd.ErrOrStderr(),
)
q := questionnaire.New(
&question.WorkingDirectory{},
&question.FilesOverwrite{},
&question.Welcome{},
&question.Stack{},
&question.Type{},
&question.DependencyManager{},
&question.Locations{},
&question.Mounts{},
&question.Name{},
&question.ApplicationRoot{},
&question.Environment{},
&question.BuildSteps{},
&question.DeployCommand{},
&question.SocketFamily{},
&question.WebCommand{},
&question.AlmostDone{},
&question.Services{},
)
err := q.AskQuestions(ctx)
if errors.Is(err, questionnaire.ErrSilent) {
return nil
}
return Platformify(cmd.Context(), cmd.OutOrStderr(), cmd.ErrOrStderr(), assets)
},
}

if err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), colors.Colorize(colors.ErrorCode, err.Error()))
return err
}
return cmd
}

input := answers.ToUserInput()
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,
)
q := questionnaire.New(
&question.WorkingDirectory{},
&question.FilesOverwrite{},
&question.Welcome{},
&question.Stack{},
&question.Type{},
&question.DependencyManager{},
&question.Locations{},
&question.Mounts{},
&question.Name{},
&question.ApplicationRoot{},
&question.Environment{},
&question.BuildSteps{},
&question.DeployCommand{},
&question.SocketFamily{},
&question.WebCommand{},
&question.AlmostDone{},
&question.Services{},
)
err := q.AskQuestions(ctx)
if errors.Is(err, questionnaire.ErrSilent) {
return nil
}

pfier := platformifier.New(input, assets.ConfigFlavor)
err = pfier.Platformify(ctx)
if err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), colors.Colorize(colors.ErrorCode, err.Error()))
return fmt.Errorf("could not configure project: %w", err)
}
if err != nil {
fmt.Fprintln(stderr, colors.Colorize(colors.ErrorCode, err.Error()))
return err
}

done := question.Done{}
return done.Ask(ctx)
},
input := answers.ToUserInput()

pfier := platformifier.New(input, assets.ConfigFlavor)
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)
}

return cmd
done := question.Done{}
return done.Ask(ctx)
}

0 comments on commit 979f5f6

Please sign in to comment.