Skip to content

Commit

Permalink
New init path misses scenario where user *will* create a Dockerfile
Browse files Browse the repository at this point in the history
Fixes #209
  • Loading branch information
codepope committed Jul 8, 2020
1 parent 95fc380 commit c9af2db
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
33 changes: 29 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"path"
"strconv"

"github.com/superfly/flyctl/cmdctx"
Expand Down Expand Up @@ -47,6 +48,12 @@ func newInitCommand() *Command {
Description: `The Cloud Native Buildpacks builder to use when deploying the app`,
})

cmd.AddBoolFlag(BoolFlagOpts{
Name: "dockerfile",
Description: `Use a dockerfile when deploying the app`,
Default: false,
})

return cmd
}

Expand Down Expand Up @@ -121,9 +128,12 @@ func runInit(commandContext *cmdctx.CmdContext) error {

fmt.Println()

if builder, _ := commandContext.Config.GetString("builder"); builder != "" {
newAppConfig.Build = &flyctl.Build{Builder: builder}
} else {
dockerfile := commandContext.Config.GetBool("dockerfile")
dockerfileset := commandContext.Config.IsSet("dockerfile")
builder, _ := commandContext.Config.GetString("builder")

// If no builder has been set and no dockerfile setting - ask
if builder == "" && !dockerfileset {
builder, err := selectBuildtype(commandContext)

switch {
Expand All @@ -132,16 +142,31 @@ func runInit(commandContext *cmdctx.CmdContext) error {
case err != nil || org == nil:
return fmt.Errorf("Error setting builder: %s", err)
}

if builder != "Dockerfile" {
newAppConfig.Build = &flyctl.Build{Builder: builder}
} else {
dockerfileExists := helpers.FileExists(path.Join(commandContext.WorkingDir, "Dockerfile"))
if !dockerfileExists {
newdf, err := os.Create(path.Join(commandContext.WorkingDir, "Dockerfile"))
if err != nil {
return fmt.Errorf("Error writing example Dockerfile: %s", err)
}
fmt.Fprintf(newdf, "FROM flydotio/getting-started\n\nEXPOSE 8080\n")
newdf.Close()
}
}
} else if builder != "" {
// If the builder was set and there's not dockerfile setting, write the builder
if !dockerfile {
newAppConfig.Build = &flyctl.Build{Builder: builder}
}
}

app, err := commandContext.Client.API().CreateApp(name, org.ID)
if err != nil {
return err
}

newAppConfig.AppName = app.Name
newAppConfig.Definition = app.Config.Definition

Expand Down
12 changes: 5 additions & 7 deletions cmd/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ func selectBuildtype(commandContext *cmdctx.CmdContext) (string, error) {

if dockerfileExists {
builders = append(builders, fmt.Sprintf("%s (%s)", "Dockerfile", "Use the existing Dockerfile"))
} else {
builders = append(builders, fmt.Sprintf("%s (%s)", "Dockerfile", "Create a example Dockerfile"))
}

for _, b := range suggestedBuilders {
Expand All @@ -123,14 +125,10 @@ func selectBuildtype(commandContext *cmdctx.CmdContext) (string, error) {
return "", err
}

if dockerfileExists {
if selectedBuilder == 0 {
return "Dockerfile", nil
}
return suggestedBuilders[selectedBuilder-1].Image, nil
if selectedBuilder == 0 {
return "Dockerfile", nil
}

return suggestedBuilders[selectedBuilder].Image, nil
return suggestedBuilders[selectedBuilder-1].Image, nil
}

func confirmFileOverwrite(filename string) bool {
Expand Down

0 comments on commit c9af2db

Please sign in to comment.