-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
84 lines (70 loc) · 2.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// A library for auto-generating Dockerfiles from project source code.
package dockerfile
import (
"fmt"
"log/slog"
"os"
"path/filepath"
"github.com/flexstack/new-dockerfile/runtime"
)
// Creates a new Dockerfile generator. If no logger is provided, a default logger is created.
func New(log ...*slog.Logger) *Dockerfile {
var logger *slog.Logger
if len(log) > 0 {
logger = log[0]
} else {
logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
}
return &Dockerfile{
log: logger,
}
}
type Dockerfile struct {
log *slog.Logger
}
// Generates a Dockerfile for the given path and writes it to the same directory.
func (a *Dockerfile) Write(path string) error {
runtime, err := a.MatchRuntime(path)
if err != nil {
return err
}
contents, err := runtime.GenerateDockerfile(path)
if err != nil {
return err
}
// Write the Dockerfile to the same directory
if err = os.WriteFile(filepath.Join(path, "Dockerfile"), contents, 0644); err != nil {
return err
}
// a.log.Info("Auto-generated Dockerfile for project using " + string(lang.Name()) + "\n" + *contents)
a.log.Info("Auto-generated Dockerfile for project using " + string(runtime.Name()))
return nil
}
// Lists all runtimes that the Dockerfile generator can auto-generate.
func (a *Dockerfile) ListRuntimes() []runtime.Runtime {
return []runtime.Runtime{
&runtime.Golang{Log: a.log},
&runtime.Rust{Log: a.log},
&runtime.Ruby{Log: a.log},
&runtime.Python{Log: a.log},
&runtime.PHP{Log: a.log},
&runtime.Java{Log: a.log},
&runtime.Elixir{Log: a.log},
&runtime.NextJS{Log: a.log},
&runtime.Deno{Log: a.log},
&runtime.Bun{Log: a.log},
&runtime.Node{Log: a.log},
&runtime.Static{Log: a.log},
}
}
// Matches the runtime of the project at the given path.
func (a *Dockerfile) MatchRuntime(path string) (runtime.Runtime, error) {
for _, r := range a.ListRuntimes() {
if r.Match(path) {
return r, nil
}
}
return nil, ErrRuntimeNotFound
}
// Error returned when we could not auto-detect the runtime of the project.
var ErrRuntimeNotFound = fmt.Errorf("A Dockerfile was not detected in the project and we could not auto-generate one for you.")