Skip to content

Commit

Permalink
Built out the input list handling
Browse files Browse the repository at this point in the history
  • Loading branch information
codepope committed Aug 3, 2020
1 parent 5b7c9d0 commit 82ca7e7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
30 changes: 21 additions & 9 deletions builtinsupport/builtinsupport.go
Expand Up @@ -11,10 +11,7 @@ type Builtin struct {
var builtins map[string]Builtin

func GetBuiltin(builtinname string) (*Builtin, error) {
if len(builtins) == 0 {
// Load builtins
initBuiltins()
}
initBuiltins()

builtin, ok := builtins[builtinname]

Expand All @@ -25,7 +22,22 @@ func GetBuiltin(builtinname string) (*Builtin, error) {
return &builtin, nil
}

func GetBuiltins() []Builtin {
initBuiltins()

var builtarray []Builtin

for _, v := range builtins {
builtarray = append(builtarray, v)
}

return builtarray
}

func initBuiltins() {
if len(builtins) != 0 {
return
}
builtins = make(map[string]Builtin)

for _, rt := range basicbuiltins {
Expand All @@ -35,7 +47,7 @@ func initBuiltins() {

var basicbuiltins = []Builtin{
Builtin{Name: "node",
Description: "A Nodejs Builtin",
Description: "Builtin Nodejs",
FileText: `
FROM node:current-alpine
WORKDIR /app
Expand All @@ -47,7 +59,7 @@ var basicbuiltins = []Builtin{
CMD [ "npm","start" ]
`},
Builtin{Name: "ruby",
Description: "A Ruby Builtin - runs app.rb",
Description: "Builtin Ruby - runs app.rb",
FileText: `
FROM ruby:2.7
WORKDIR /usr/src/app
Expand All @@ -59,7 +71,7 @@ var basicbuiltins = []Builtin{
CMD ["bundle", "exec", "rackup", "--host", "0.0.0.0", "-p", "8080"]
`},
Builtin{Name: "deno",
Description: "A deno Builtin - runs main.ts, requires deps.ts",
Description: "Builtin Deno - runs main.ts, requires deps.ts",
FileText: `
FROM hayd/alpine-deno:1.2.1
EXPOSE 8080
Expand All @@ -72,7 +84,7 @@ var basicbuiltins = []Builtin{
CMD ["run", "--allow-net", "main.ts"]
`},
Builtin{Name: "go",
Description: "A Go Builtin - Builds app.go uses go modules",
Description: "Builtin Go - Builds app.go uses go modules",
FileText: `
FROM golang:1.13 as builder
WORKDIR /go/src/app
Expand All @@ -87,7 +99,7 @@ var basicbuiltins = []Builtin{
CMD ["/app"]
`},
Builtin{Name: "static",
Description: "A Static Web Site - All files are copied across and served",
Description: "Builtin Static - Static web server. All files are copied across and served",
FileText: `
FROM pierrezemb/gostatic
COPY . /srv/http/
Expand Down
19 changes: 6 additions & 13 deletions cmd/init.go
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"os"
"path"
"strconv"

"github.com/superfly/flyctl/cmdctx"
Expand Down Expand Up @@ -160,26 +159,20 @@ func runInit(commandContext *cmdctx.CmdContext) error {
dockerfile := commandContext.Config.GetBool("dockerfile")

if builder == "" && !dockerfileSet {
builder, err := selectBuildtype(commandContext)
builder, builtin, err := selectBuildtype(commandContext)

switch {
case isInterrupt(err):
return nil
case err != nil || org == nil:
case err != nil || builder == "":
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 flyio/hellofly\n")
newdf.Close()
}
}
if builtin {
builtinname = builder
}
} else if builder != "" {
// If the builder was set and there's not dockerfile setting, write the builder
Expand Down
38 changes: 31 additions & 7 deletions cmd/input.go
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/AlecAivazis/survey/v2"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/builtinsupport"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/helpers"
)
Expand Down Expand Up @@ -101,16 +102,25 @@ var suggestedBuilders = []suggestedBuilder{
},
}

func selectBuildtype(commandContext *cmdctx.CmdContext) (string, error) {
var builtins []builtinsupport.Builtin

func selectBuildtype(commandContext *cmdctx.CmdContext) (string, bool, error) {

dockerfileExists := helpers.FileExists(path.Join(commandContext.WorkingDir, "Dockerfile"))

dockerfileentry := -1

builders := []string{}

builtins = builtinsupport.GetBuiltins()

for _, b := range builtins {
builders = append(builders, fmt.Sprintf("%s\n %s", b.Name, helpers.WrapString(b.Description, 60, 4)))
}

if dockerfileExists {
builders = append(builders, fmt.Sprintf("%s\n (%s)", "Dockerfile", "Use the existing Dockerfile"))
} else {
builders = append(builders, fmt.Sprintf("%s\n (%s)", "Dockerfile", "Create an example Dockerfile"))
dockerfileentry = len(builders) - 1
}

for _, b := range suggestedBuilders {
Expand All @@ -125,11 +135,25 @@ func selectBuildtype(commandContext *cmdctx.CmdContext) (string, error) {
PageSize: 15,
}
if err := survey.AskOne(prompt, &selectedBuilder); err != nil {
return "", err
return "", false, err
}

if selectedBuilder == 0 {
return "Dockerfile", nil
fmt.Println(selectedBuilder, dockerfileentry)
offset := 0 // offset of the builder names

if dockerfileentry != -1 {
offset = offset + 1
if selectedBuilder == dockerfileentry {
return "Dockerfile", false, nil
}
}

if selectedBuilder < len(builtins) {
// Selected a built in
return builtins[selectedBuilder].Name, true, nil
}
return suggestedBuilders[selectedBuilder-1].Image, nil

offset = offset + len(builtins)

return suggestedBuilders[selectedBuilder-offset].Image, false, nil
}

0 comments on commit 82ca7e7

Please sign in to comment.