Skip to content

Commit 96e1a23

Browse files
author
Antonis Kalipetis
committed
Refactor platformify command to support non-interactive mode and improve context handling
1 parent 980c138 commit 96e1a23

File tree

6 files changed

+69
-39
lines changed

6 files changed

+69
-39
lines changed

commands/platformify.go

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"io/fs"
89
"os"
910
"path"
1011

@@ -22,8 +23,10 @@ type contextKey string
2223

2324
var FlavorKey contextKey = "flavor"
2425
var NoInteractionKey contextKey = "no-interaction"
26+
var FSKey contextKey = "fs"
2527

2628
func NewPlatformifyCmd(assets *vendorization.VendorAssets) *cobra.Command {
29+
var noInteraction bool
2730
cmd := &cobra.Command{
2831
Use: assets.Use,
2932
Aliases: []string{"ify"},
@@ -35,24 +38,30 @@ func NewPlatformifyCmd(assets *vendorization.VendorAssets) *cobra.Command {
3538
cmd.Context(),
3639
cmd.OutOrStderr(),
3740
cmd.ErrOrStderr(),
41+
noInteraction,
3842
assets,
3943
)
4044
},
4145
}
4246

47+
cmd.Flags().BoolVar(&noInteraction, "no-interaction", false, "Disable interactive prompts")
4348
return cmd
4449
}
4550

46-
func Platformify(ctx context.Context, stdout, stderr io.Writer, assets *vendorization.VendorAssets) error {
47-
answers := models.NewAnswers()
48-
answers.Flavor, _ = ctx.Value(FlavorKey).(string)
49-
answers.NoInteraction, _ = ctx.Value(NoInteractionKey).(bool)
50-
ctx = models.ToContext(ctx, answers)
51-
ctx = colors.ToContext(
52-
ctx,
53-
stdout,
54-
stderr,
55-
)
51+
func Discover(
52+
ctx context.Context,
53+
flavor string,
54+
noInteraction bool,
55+
fileSystem fs.FS,
56+
) (*platformifier.UserInput, error) {
57+
answers, _ := models.FromContext(ctx)
58+
if answers == nil {
59+
answers = models.NewAnswers()
60+
ctx = models.ToContext(ctx, answers)
61+
}
62+
answers.Flavor = flavor
63+
answers.NoInteraction = noInteraction
64+
answers.WorkingDirectory = fileSystem
5665
q := questionnaire.New(
5766
&question.WorkingDirectory{},
5867
&question.Welcome{},
@@ -73,20 +82,32 @@ func Platformify(ctx context.Context, stdout, stderr io.Writer, assets *vendoriz
7382
)
7483
err := q.AskQuestions(ctx)
7584
if errors.Is(err, questionnaire.ErrSilent) {
76-
return nil
85+
return nil, nil
7786
}
7887

7988
if err != nil {
80-
fmt.Fprintln(stderr, colors.Colorize(colors.ErrorCode, err.Error()))
81-
return err
89+
return nil, err
8290
}
8391

84-
input := answers.ToUserInput()
92+
return answers.ToUserInput(), nil
93+
}
8594

95+
func Platformify(
96+
ctx context.Context,
97+
stdout, stderr io.Writer,
98+
noInteraction bool,
99+
assets *vendorization.VendorAssets,
100+
) error {
101+
ctx = colors.ToContext(ctx, stdout, stderr)
102+
ctx = models.ToContext(ctx, models.NewAnswers())
103+
input, err := Discover(ctx, assets.ConfigFlavor, noInteraction, nil)
104+
if err != nil {
105+
return err
106+
}
107+
answers, _ := models.FromContext(ctx)
86108
pfier := platformifier.New(input, assets.ConfigFlavor)
87109
configFiles, err := pfier.Platformify(ctx)
88110
if err != nil {
89-
fmt.Fprintln(stderr, colors.Colorize(colors.ErrorCode, err.Error()))
90111
return fmt.Errorf("could not configure project: %w", err)
91112
}
92113

internal/colors/context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ func ToContext(ctx context.Context, out, err io.Writer) context.Context {
2323
func FromContext(ctx context.Context) (out, err io.Writer, ok bool) {
2424
out, ok = ctx.Value(outKey).(io.Writer)
2525
if !ok {
26-
return nil, nil, false
26+
return io.Discard, io.Discard, false
2727
}
2828
err, ok = ctx.Value(errKey).(io.Writer)
2929
if !ok {
30-
return nil, nil, false
30+
return out, io.Discard, false
3131
}
3232
return out, err, true
3333
}

internal/question/name.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func (q *Name) Ask(ctx context.Context) error {
2525
return nil
2626
}
2727
defaultName := slugify(path.Base(answers.Cwd))
28+
if defaultName == "" {
29+
defaultName = "app"
30+
}
2831
if answers.NoInteraction {
2932
answers.Name = defaultName
3033
}

internal/question/type.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ func (q *Type) Ask(ctx context.Context) error {
2828
return
2929
}
3030

31-
fmt.Fprintf(
32-
stderr,
33-
"%s %s\n",
34-
colors.Colorize(colors.GreenCode, "✓"),
35-
colors.Colorize(
36-
colors.BrandCode,
37-
fmt.Sprintf("Detected runtime: %s", answers.Type.Runtime.Title()),
38-
),
39-
)
31+
if answers.Type.Runtime.Title() != "" {
32+
fmt.Fprintf(
33+
stderr,
34+
"%s %s\n",
35+
colors.Colorize(colors.GreenCode, "✓"),
36+
colors.Colorize(
37+
colors.BrandCode,
38+
fmt.Sprintf("Detected runtime: %s", answers.Type.Runtime.Title()),
39+
),
40+
)
41+
}
4042
}()
4143

4244
typ, err := answers.Discoverer.Type()

internal/question/working_directory.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,20 @@ import (
1919
type WorkingDirectory struct{}
2020

2121
func (q *WorkingDirectory) Ask(ctx context.Context) error {
22-
_, stderr, ok := colors.FromContext(ctx)
23-
if !ok {
24-
return nil
25-
}
26-
22+
_, stderr, _ := colors.FromContext(ctx)
2723
answers, ok := models.FromContext(ctx)
2824
if !ok {
2925
return nil
3026
}
31-
cwd, err := os.Getwd()
32-
if err != nil {
33-
return err
27+
if answers.WorkingDirectory == nil {
28+
cwd, err := os.Getwd()
29+
if err != nil {
30+
return err
31+
}
32+
answers.WorkingDirectory = os.DirFS(cwd)
33+
answers.Cwd = cwd
34+
answers.HasGit = false
3435
}
35-
answers.WorkingDirectory = os.DirFS(cwd)
36-
answers.Cwd = cwd
37-
answers.HasGit = false
3836
answers.Discoverer = discovery.New(answers.WorkingDirectory)
3937
if answers.NoInteraction {
4038
return nil
@@ -44,7 +42,7 @@ func (q *WorkingDirectory) Ask(ctx context.Context) error {
4442
cmd := exec.CommandContext(ctx, "git", "rev-parse", "--git-dir")
4543
cmd.Stdout = &outBuf
4644
cmd.Stderr = &errBuf
47-
err = cmd.Run()
45+
err := cmd.Run()
4846
if err != nil {
4947
fmt.Fprintln(
5048
stderr,

internal/utils/utils.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"cmp"
77
"encoding/json"
8+
"fmt"
89
"io"
910
"io/fs"
1011
"os"
@@ -176,7 +177,12 @@ func GetTOMLValue(
176177

177178
func CountFiles(fileSystem fs.FS) (map[string]int, error) {
178179
fileCounter := make(map[string]int)
179-
err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, _ error) error {
180+
err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
181+
if err != nil {
182+
fmt.Println(err)
183+
return err
184+
}
185+
180186
if d.IsDir() {
181187
if slices.Contains(skipDirs, d.Name()) {
182188
return filepath.SkipDir

0 commit comments

Comments
 (0)