Skip to content

Commit 979f5f6

Browse files
authored
Merge pull request #229 from platformsh/feature/export-command
Export the Platformify command to be used outside of a Cobra context
2 parents c69761f + 285a6b2 commit 979f5f6

File tree

1 file changed

+52
-46
lines changed

1 file changed

+52
-46
lines changed

commands/platformify.go

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package commands
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
7+
"io"
68

79
"github.com/spf13/cobra"
810

@@ -26,56 +28,60 @@ func NewPlatformifyCmd(assets *vendorization.VendorAssets) *cobra.Command {
2628
SilenceUsage: true,
2729
SilenceErrors: true,
2830
RunE: func(cmd *cobra.Command, _ []string) error {
29-
answers := models.NewAnswers()
30-
answers.Flavor, _ = cmd.Context().Value(FlavorKey).(string)
31-
ctx := models.ToContext(cmd.Context(), answers)
32-
ctx = colors.ToContext(
33-
ctx,
34-
cmd.OutOrStderr(),
35-
cmd.ErrOrStderr(),
36-
)
37-
q := questionnaire.New(
38-
&question.WorkingDirectory{},
39-
&question.FilesOverwrite{},
40-
&question.Welcome{},
41-
&question.Stack{},
42-
&question.Type{},
43-
&question.DependencyManager{},
44-
&question.Locations{},
45-
&question.Mounts{},
46-
&question.Name{},
47-
&question.ApplicationRoot{},
48-
&question.Environment{},
49-
&question.BuildSteps{},
50-
&question.DeployCommand{},
51-
&question.SocketFamily{},
52-
&question.WebCommand{},
53-
&question.AlmostDone{},
54-
&question.Services{},
55-
)
56-
err := q.AskQuestions(ctx)
57-
if errors.Is(err, questionnaire.ErrSilent) {
58-
return nil
59-
}
31+
return Platformify(cmd.Context(), cmd.OutOrStderr(), cmd.ErrOrStderr(), assets)
32+
},
33+
}
6034

61-
if err != nil {
62-
fmt.Fprintln(cmd.ErrOrStderr(), colors.Colorize(colors.ErrorCode, err.Error()))
63-
return err
64-
}
35+
return cmd
36+
}
6537

66-
input := answers.ToUserInput()
38+
func Platformify(ctx context.Context, stdout, stderr io.Writer, assets *vendorization.VendorAssets) error {
39+
answers := models.NewAnswers()
40+
answers.Flavor, _ = ctx.Value(FlavorKey).(string)
41+
ctx = models.ToContext(ctx, answers)
42+
ctx = colors.ToContext(
43+
ctx,
44+
stdout,
45+
stderr,
46+
)
47+
q := questionnaire.New(
48+
&question.WorkingDirectory{},
49+
&question.FilesOverwrite{},
50+
&question.Welcome{},
51+
&question.Stack{},
52+
&question.Type{},
53+
&question.DependencyManager{},
54+
&question.Locations{},
55+
&question.Mounts{},
56+
&question.Name{},
57+
&question.ApplicationRoot{},
58+
&question.Environment{},
59+
&question.BuildSteps{},
60+
&question.DeployCommand{},
61+
&question.SocketFamily{},
62+
&question.WebCommand{},
63+
&question.AlmostDone{},
64+
&question.Services{},
65+
)
66+
err := q.AskQuestions(ctx)
67+
if errors.Is(err, questionnaire.ErrSilent) {
68+
return nil
69+
}
6770

68-
pfier := platformifier.New(input, assets.ConfigFlavor)
69-
err = pfier.Platformify(ctx)
70-
if err != nil {
71-
fmt.Fprintln(cmd.ErrOrStderr(), colors.Colorize(colors.ErrorCode, err.Error()))
72-
return fmt.Errorf("could not configure project: %w", err)
73-
}
71+
if err != nil {
72+
fmt.Fprintln(stderr, colors.Colorize(colors.ErrorCode, err.Error()))
73+
return err
74+
}
7475

75-
done := question.Done{}
76-
return done.Ask(ctx)
77-
},
76+
input := answers.ToUserInput()
77+
78+
pfier := platformifier.New(input, assets.ConfigFlavor)
79+
err = pfier.Platformify(ctx)
80+
if err != nil {
81+
fmt.Fprintln(stderr, colors.Colorize(colors.ErrorCode, err.Error()))
82+
return fmt.Errorf("could not configure project: %w", err)
7883
}
7984

80-
return cmd
85+
done := question.Done{}
86+
return done.Ask(ctx)
8187
}

0 commit comments

Comments
 (0)