add options, fix input processing
build / build (push) Successful in 1m48s

This commit is contained in:
tchivert committed 2024-01-26 08:43:27 +01:00
1 parent 1acb1fab17
commit 5e049f82c5
2 files changed
+108 -35

No files matched your search

+1 -1
View File
@@ -8,7 +8,7 @@ RUN go mod download
COPY *.go ./
RUN CGO_ENABLED=1 GOOS=linux go build -o runcompose .
RUN GOOS=linux go build -o runcompose .
FROM golang:alpine
+107 -34
View File
@@ -10,32 +10,32 @@ import (
)
type Logging struct {
Driver string
Driver string
Options map[string]string
}
type Service struct {
Name string
Container string
Image string
Ports []string
Volumes []string
Tmpfs []string
Environment []string
Restart string
Network string
Labels []string
CapAdd []string
CapDrop []string
GroupAdd []string
User string
Workdir string
Hostname string
Logging Logging
Name string
Container string
Image string
Ports []string
Volumes []string
Tmpfs []string
Environment []string
Restart string
Network string
Labels []string
CapAdd []string
CapDrop []string
GroupAdd []string
User string
Workdir string
Hostname string
Logging Logging
}
type PageData struct {
Input string
Input string
Result string
}
@@ -70,6 +70,16 @@ func sanitizeInput(input string) string {
input = strings.ReplaceAll(input, "--network=", "--network ")
input = strings.ReplaceAll(input, "--label=", "--label ")
input = strings.ReplaceAll(input, "--log-opt=", "--log-opt ")
input = strings.ReplaceAll(input, "--log-driver=", "--log-driver ")
input = strings.ReplaceAll(input, "--cap-add=", "--cap-add ")
input = strings.ReplaceAll(input, "--cap-drop=", "--cap-drop ")
input = strings.ReplaceAll(input, "--group-add=", "--group-add ")
input = strings.ReplaceAll(input, "--user=", "--user ")
input = strings.ReplaceAll(input, "--workdir=", "--workdir ")
input = strings.ReplaceAll(input, "--hostname=", "--hostname ")
// Replace quotes with double quotes
input = strings.ReplaceAll(input, "'", "\"")
// List of flags with no equivalent in Docker Compose that should be ignored
ignoredFlags := []string{
@@ -115,13 +125,39 @@ func parseFlagValue(parts []string, index *int, flag string) string {
return ""
}
func splitArgs(input string) []string {
var args []string
var current string
var inQuote bool
for _, char := range input {
if char == '"' {
current += string(char)
inQuote = !inQuote
} else if char == ' ' && !inQuote {
args = append(args, current)
current = ""
} else {
current += string(char)
}
}
if current != "" {
args = append(args, current)
}
return args
}
func parseDockerRunCommand(input string) (Service, error) {
var name, container, image, restart, network string
var ports, volumes, tmpfs, environment, labels []string
var capAdd, capDrop, groupAdd []string
var user, workdir, hostname string
var loggingOptions map[string]string
var loggingDriver string
parts := strings.Split(input, " ")
parts := splitArgs(input)
parts = removeKeywords(parts, "docker", "run")
for i := 0; i < len(parts); i++ {
@@ -176,6 +212,30 @@ func parseDockerRunCommand(input string) (Service, error) {
value := parseFlagValue(parts, &i, arg)
loggingDriver = value
flagParsed = true
case "--cap-add":
value := parseFlagValue(parts, &i, arg)
capAdd = append(capAdd, value)
flagParsed = true
case "--cap-drop":
value := parseFlagValue(parts, &i, arg)
capDrop = append(capDrop, value)
flagParsed = true
case "--group-add":
value := parseFlagValue(parts, &i, arg)
groupAdd = append(groupAdd, value)
flagParsed = true
case "-u", "--user":
value := parseFlagValue(parts, &i, arg)
user = value
flagParsed = true
case "-w", "--workdir":
value := parseFlagValue(parts, &i, arg)
workdir = value
flagParsed = true
case "-h", "--hostname":
value := parseFlagValue(parts, &i, arg)
hostname = value
flagParsed = true
}
if !flagParsed && !strings.HasPrefix(arg, "-") && image == "" {
@@ -188,25 +248,38 @@ func parseDockerRunCommand(input string) (Service, error) {
}
service := Service{
Name: name,
Container: container,
Image: image,
Ports: ports,
Volumes: volumes,
Tmpfs: tmpfs,
Environment: environment,
Restart: restart,
Network: network,
Labels: labels,
Logging: Logging{
Driver: loggingDriver,
Options: loggingOptions,
Name: name,
Container: container,
Image: image,
Ports: ports,
Volumes: volumes,
Tmpfs: tmpfs,
Environment: environment,
Restart: restart,
Network: network,
Labels: labels,
CapAdd: capAdd,
CapDrop: capDrop,
GroupAdd: groupAdd,
User: user,
Workdir: workdir,
Hostname: hostname,
Logging: Logging{
Driver: loggingDriver,
Options: loggingOptions,
},
}
return service, nil
}
func htmlFormat(input string) string {
input = strings.ReplaceAll(input, "\n", "<br>")
input = strings.ReplaceAll(input, " ", "&nbsp;")
input = strings.ReplaceAll(input, "\"", "&quot;")
return input
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("form.html"))
@@ -215,7 +288,7 @@ func main() {
if r.Method == "POST" {
dockerRun := r.FormValue("docker_run")
sanitizedInput := sanitizeInput(dockerRun)
pageData.Input = sanitizedInput
pageData.Input = htmlFormat(sanitizedInput)
service, err := parseDockerRunCommand(sanitizedInput)
if err == nil {