Skip to content

Commit

Permalink
chore: add ptr function
Browse files Browse the repository at this point in the history
  • Loading branch information
luantranminh committed Sep 29, 2024
1 parent 1fcc0a7 commit b9a4d52
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (i *InitActionCmd) Run(ctx context.Context, client *githubClient, current r
SchemaScope: i.SchemaScope,
CloudRepo: i.cloudRepo,
}
if i.flow == "declarative" {
if i.flow == "declarative" && i.SetupSchemaApply != nil {
cfg.SetupSchemaApply = *i.SetupSchemaApply
}
if err = repo.AddAtlasYAML(ctx, cfg, branchName, commitMsg, i.Replace); err != nil {
Expand Down
16 changes: 7 additions & 9 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ func TestRunInitActionCmd(t *testing.T) {
}))
require.NoError(t, err)
defaultClient := createGHClient(&mockService{getContentError: &github.ErrorResponse{Message: "Not Found"}}, &mockService{})
truePtr := func() *bool { t := true; return &t }()
falsePtr := func() *bool { f := false; return &f }()
var tests = []struct {
name string
client *githubClient
Expand Down Expand Up @@ -579,7 +577,7 @@ func TestRunInitActionCmd(t *testing.T) {
SchemaScope: true,
cloudRepo: "slug1",
Replace: true,
SetupSchemaApply: truePtr,
SetupSchemaApply: ptr(true),
env: gen.Env{
Name: "local",
Path: "atlas.hcl",
Expand All @@ -595,23 +593,23 @@ func TestRunInitActionCmd(t *testing.T) {
driver: "MYSQL",
Token: "multi-repos",
SchemaScope: true,
SetupSchemaApply: truePtr,
SetupSchemaApply: ptr(true),
},
expected: &InitActionCmd{
From: "schema.hcl",
To: "atlas://slug1",
driver: "POSTGRESQL",
Token: "multi-repos",
SchemaScope: true,
SetupSchemaApply: truePtr,
SetupSchemaApply: ptr(true),
},
},
{
name: "empty driver, select postgresql",
cmd: &InitActionCmd{
Token: "multi-repos",
SchemaScope: true,
SetupSchemaApply: truePtr,
SetupSchemaApply: ptr(true),
},
prompt: "\x1b[B\x1b[B\x1b[B\natlas://d\nfile://schema.hcl\n\x1b[B\n",
expected: &InitActionCmd{
Expand All @@ -621,7 +619,7 @@ func TestRunInitActionCmd(t *testing.T) {
From: "atlas://d",
cloudRepo: "slug3",
SchemaScope: true,
SetupSchemaApply: truePtr,
SetupSchemaApply: ptr(true),
},
},
{
Expand All @@ -639,7 +637,7 @@ func TestRunInitActionCmd(t *testing.T) {
From: "atlas://d",
cloudRepo: "slug3",
SchemaScope: true,
SetupSchemaApply: falsePtr,
SetupSchemaApply: ptr(false),
},
},
{
Expand All @@ -657,7 +655,7 @@ func TestRunInitActionCmd(t *testing.T) {
From: "atlas://d",
cloudRepo: "slug3",
SchemaScope: true,
SetupSchemaApply: truePtr,
SetupSchemaApply: ptr(true),
},
},
}
Expand Down
26 changes: 15 additions & 11 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,22 +472,22 @@ func (i *InitActionCmd) setSetupSchemaApply() error {
// already set by flag
return nil
}
prompt := promptui.Select{
Label: "Do you want to setup the `schema apply` action?",
Stdin: i.stdin,
Items: []string{"yes", "no"},
Templates: &promptui.SelectTemplates{
Active: "{{ if eq . \"no\" }}▸ No{{ else }}▸ Yes{{ end }}",
Inactive: "{{ if eq . \"no\" }} No{{ else }} Yes{{ end }}",
Selected: fmt.Sprintf(`{{ "%s" | green }} {{ "Setup schema apply: " | faint }} {{ . }}`, promptui.IconGood),
prompt := promptui.Prompt{
Label: "Do you want to setup the `schema apply` action?",
Stdin: i.stdin,
IsConfirm: true,
Templates: &promptui.PromptTemplates{
Success: fmt.Sprintf(`{{ "%s" | green }} {{ "Setup schema apply: " | faint }} {{ . }}`, promptui.IconGood),
},
}
_, setup, err := prompt.Run()
_, err := prompt.Run()
if err != nil {
if errors.Is(err, promptui.ErrAbort) {
return nil
}
return err
}
s := setup == "yes"
i.SetupSchemaApply = &s
i.SetupSchemaApply = ptr(true)
return nil
}

Expand All @@ -510,3 +510,7 @@ func (i *InitActionCmd) validateParams() error {
}
return nil
}

func ptr[T any](v T) *T {
return &v
}

0 comments on commit b9a4d52

Please sign in to comment.