Skip to content

Commit 992e4aa

Browse files
committed
chore: add more logging
1 parent 006947c commit 992e4aa

File tree

9 files changed

+45
-9
lines changed

9 files changed

+45
-9
lines changed

cmd/new-dockerfile/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ func main() {
8787
os.Exit(1)
8888
}
8989

90+
contents = append(
91+
[]byte(fmt.Sprintf(issueStr)),
92+
contents...,
93+
)
94+
9095
if !write {
9196
fmt.Println(string(contents))
9297
return
@@ -100,3 +105,7 @@ func main() {
100105

101106
log.Info(fmt.Sprintf("Auto-generated Dockerfile for project using %s: %s", string(r.Name()), output))
102107
}
108+
109+
const issueStr = `# Auto-generated by the "new-dockerfile" CLI tool
110+
# Please report any issues to: https://github.com/flexstack/new-dockerfile/issues
111+
`

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,5 @@ func (a *Dockerfile) MatchRuntime(path string) (runtime.Runtime, error) {
8080
return nil, ErrRuntimeNotFound
8181
}
8282

83+
// Error returned when we could not auto-detect the runtime of the project.
8384
var ErrRuntimeNotFound = fmt.Errorf("A Dockerfile was not detected in the project and we could not auto-generate one for you.")

runtime/elixir.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (d *Elixir) GenerateDockerfile(path string) ([]byte, error) {
5252
return nil, err
5353
}
5454

55-
BinName, err := findBinName(path)
55+
binName, err := findBinName(path)
5656
if err != nil {
5757
return nil, err
5858
}
@@ -64,14 +64,14 @@ func (d *Elixir) GenerateDockerfile(path string) ([]byte, error) {
6464
Binary name : %s
6565
6666
Docker build arguments can supersede these defaults if provided.
67-
See https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile`, *elixirVersion, *otpVersion, BinName),
67+
See https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile`, *elixirVersion, *otpVersion, binName),
6868
)
6969

7070
var buf bytes.Buffer
7171
if err := tmpl.Option("missingkey=zero").Execute(&buf, map[string]string{
7272
"ElixirVersion": *elixirVersion,
7373
"OTPVersion": strings.Split(*otpVersion, ".")[0],
74-
"BinName": BinName,
74+
"BinName": binName,
7575
}); err != nil {
7676
return nil, fmt.Errorf("Failed to execute template")
7777
}

runtime/golang.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,15 @@ ARG CGO_ENABLED=0
105105
COPY . .
106106
RUN if [ -f go.mod ]; then go mod download; fi
107107
108-
# -trimpath removes the absolute path to the source code in the binary
109-
# -ldflags="-s -w" removes the symbol table and debug information from the binary
110-
# CGO_ENABLED=0 disables the use of cgo
111108
FROM base AS build
112109
WORKDIR /go/src/app
113110
ARG TARGETOS=linux
114111
ARG TARGETARCH=arm64
115112
ARG CGO_ENABLED=0
116113
ARG PACKAGE={{.Package}}
117-
114+
# -trimpath removes the absolute path to the source code in the binary
115+
# -ldflags="-s -w" removes the symbol table and debug information from the binary
116+
# CGO_ENABLED=0 disables the use of cgo
118117
RUN CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -trimpath -ldflags="-s -w" -o /go/bin/app "${PACKAGE}"
119118
120119
FROM debian:stable-slim

runtime/node.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func (d *Node) GenerateDockerfile(path string) ([]byte, error) {
8989
for _, cmd := range startCommands {
9090
if _, ok := scripts[cmd].(string); ok {
9191
startCMD = fmt.Sprintf("%s run %s", packageManager, cmd)
92+
d.Log.Info("Detected start command in package.json: " + startCMD)
9293
break
9394
}
9495
}
@@ -99,6 +100,7 @@ func (d *Node) GenerateDockerfile(path string) ([]byte, error) {
99100

100101
if ok && startScriptRe.MatchString(value) {
101102
startCMD = fmt.Sprintf("%s run %s", packageManager, name)
103+
d.Log.Info("Detected start command in package.json via regex pattern: " + startCMD)
102104
break
103105
}
104106
}
@@ -108,6 +110,7 @@ func (d *Node) GenerateDockerfile(path string) ([]byte, error) {
108110
for _, cmd := range buildCommands {
109111
if _, ok := scripts[cmd].(string); ok {
110112
buildCMD = fmt.Sprintf("%s run %s", packageManager, cmd)
113+
d.Log.Info("Detected build command in package.json: " + buildCMD)
111114
break
112115
}
113116
}

runtime/php.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (d *PHP) GenerateDockerfile(path string) ([]byte, error) {
5353
startCMD := "apache2-foreground"
5454
installCMD := ""
5555
if _, err := os.Stat(filepath.Join(path, "composer.json")); err == nil {
56+
d.Log.Info("Detected composer.json file")
5657
installCMD = "composer update && composer install --prefer-dist --no-dev --optimize-autoloader --no-interaction"
5758
}
5859

@@ -73,6 +74,7 @@ func (d *PHP) GenerateDockerfile(path string) ([]byte, error) {
7374
}
7475

7576
if npmInstallCMD != "" {
77+
d.Log.Info("Detected package-lock.json, pnpm-lock.yaml, yarn.lock or bun.lockb file")
7678
if installCMD == "" {
7779
installCMD = npmInstallCMD
7880
} else {
@@ -101,6 +103,7 @@ func (d *PHP) GenerateDockerfile(path string) ([]byte, error) {
101103
for _, cmd := range buildCommands {
102104
if _, ok := scripts[cmd].(string); ok {
103105
buildCMD = fmt.Sprintf("%s run %s", packageManager, cmd)
106+
d.Log.Info("Detected build command in package.json: " + buildCMD)
104107
break
105108
}
106109
}

runtime/python.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,19 @@ func (d *Python) GenerateDockerfile(path string) ([]byte, error) {
6464

6565
installCMD := ""
6666
if _, err := os.Stat(filepath.Join(path, "requirements.txt")); err == nil {
67+
d.Log.Info("Detected requirements.txt file")
6768
installCMD = "pip install -r requirements.txt"
6869
} else if _, err := os.Stat(filepath.Join(path, "poetry.lock")); err == nil {
70+
d.Log.Info("Detected a poetry project")
6971
installCMD = "poetry install --no-dev --no-interactive --no-ansi"
7072
} else if _, err := os.Stat(filepath.Join(path, "Pipfile.lock")); err == nil {
73+
d.Log.Info("Detected a pipenv project")
7174
installCMD = "PIPENV_VENV_IN_PROJECT=1 pipenv install --deploy"
7275
} else if _, err := os.Stat(filepath.Join(path, "pdm.lock")); err == nil {
76+
d.Log.Info("Detected a pdm project")
7377
installCMD = "pdm install --prod"
7478
} else if _, err := os.Stat(filepath.Join(path, "pyproject.toml")); err == nil {
79+
d.Log.Info("Detected a pyproject.toml file")
7580
installCMD = "pip install --upgrade build setuptools && pip install ."
7681
}
7782

@@ -99,9 +104,14 @@ func (d *Python) GenerateDockerfile(path string) ([]byte, error) {
99104
}
100105
}
101106

102-
startCMD = fmt.Sprintf(`python -m %s`, projectName)
107+
if projectName != "" {
108+
startCMD = fmt.Sprintf(`python -m %s`, projectName)
109+
d.Log.Info("Detected start command via pyproject.toml")
110+
}
103111
}
104-
} else {
112+
}
113+
114+
if startCMD == "" {
105115
mainFiles := []string{
106116
"main.py",
107117
"app.py",
@@ -121,6 +131,7 @@ func (d *Python) GenerateDockerfile(path string) ([]byte, error) {
121131
}
122132

123133
startCMD = fmt.Sprintf(`python %s`, fn)
134+
d.Log.Info("Detected start command via main file: " + startCMD)
124135
break
125136
}
126137
}

runtime/ruby.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,15 @@ func (d *Ruby) GenerateDockerfile(path string) ([]byte, error) {
6969
installCMD = installCMD + " && bun install"
7070
}
7171

72+
if packageManager != "" {
73+
d.Log.Info("Detected Node.js package manager: " + packageManager)
74+
}
75+
7276
isRails := isRailsProject(path)
7377
buildCMD := ""
7478
startCMD := ""
7579
if isRails {
80+
d.Log.Info("Detected Rails project")
7681
buildCMD = "bundle exec rake assets:precompile"
7782
startCMD = "bundle exec rails server -b 0.0.0.0 -p ${PORT}"
7883
} else {
@@ -86,10 +91,13 @@ func (d *Ruby) GenerateDockerfile(path string) ([]byte, error) {
8691

8792
switch fn {
8893
case "config.ru":
94+
d.Log.Info("Detected Rack project")
8995
startCMD = "bundle exec rackup config.ru -p ${PORT}"
9096
case "config/environment.rb":
97+
d.Log.Info("Detected Rails project")
9198
startCMD = "bundle exec ruby script/server"
9299
case "Rakefile":
100+
d.Log.Info("Detected Rake project")
93101
startCMD = "bundle exec rake"
94102
}
95103

runtime/rust.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ func (d *Rust) GenerateDockerfile(path string) ([]byte, error) {
6868
if bin == "bin" {
6969
if pkgs, ok := cargoTOML[bin].([]map[string]interface{}); ok {
7070
if len(pkgs) > 0 {
71+
d.Log.Info("Detected binary in Cargo.toml via [[bin]]")
7172
pkg = pkgs[0]
7273
break
7374
}
7475
}
7576
} else if pkg, ok = cargoTOML[bin].(map[string]interface{}); ok {
77+
d.Log.Info("Detected binary in Cargo.toml via [" + bin + "]")
7678
break
7779
}
7880
}

0 commit comments

Comments
 (0)