fix some input errors
This commit is contained in:
1 parent
ad57c8a874
commit
4cbf686987
2 files changed
+62
-26
No files matched your search
@@ -70,7 +70,7 @@
|
||||
<h1>Docker Run to Compose</h1>
|
||||
<form method="POST" action="/">
|
||||
<label for="docker_run">Docker run command:</label>
|
||||
<input type="text" id="docker_run" name="docker_run" size="100" autofocus autocomplete="off">
|
||||
<input type="text" id="docker_run" name="docker_run" size="100" autofocus value="{{.Input}}" autocomplete="off">
|
||||
<input type="submit" value="Convert">
|
||||
<textarea id="result" name="result" readonly>{{.Result}}</textarea>
|
||||
</form>
|
||||
|
||||
+61
-25
@@ -21,6 +21,7 @@ type Service struct {
|
||||
}
|
||||
|
||||
type PageData struct {
|
||||
Input string
|
||||
Result string
|
||||
}
|
||||
|
||||
@@ -29,37 +30,71 @@ func extractImageName(image string) string {
|
||||
return strings.Split(parts[len(parts)-1], ":")[0]
|
||||
}
|
||||
|
||||
func removeKeywords(parts []string, keywords ...string) []string {
|
||||
var filtered []string
|
||||
for _, part := range parts {
|
||||
shouldRemove := false
|
||||
for _, keyword := range keywords {
|
||||
if part == keyword {
|
||||
shouldRemove = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !shouldRemove {
|
||||
filtered = append(filtered, part)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
func parseDockerRunCommand(input string) (Service, error) {
|
||||
parts := strings.Split(input, " ")
|
||||
var name, container, image, restart, network string
|
||||
var ports, volumes, environment, labels []string
|
||||
const maxInputLength = 2048
|
||||
|
||||
for i := 1; i < len(parts); i++ {
|
||||
switch parts[i] {
|
||||
case "-p", "--publish":
|
||||
i++
|
||||
ports = append(ports, parts[i])
|
||||
case "-v", "--volume":
|
||||
i++
|
||||
volumes = append(volumes, parts[i])
|
||||
case "-e", "--env":
|
||||
i++
|
||||
environment = append(environment, parts[i])
|
||||
case "--name":
|
||||
i++
|
||||
name = parts[i]
|
||||
container = parts[i]
|
||||
case "--restart":
|
||||
i++
|
||||
restart = parts[i]
|
||||
case "--network":
|
||||
i++
|
||||
network = parts[i]
|
||||
case "--label":
|
||||
i++
|
||||
labels = append(labels, parts[i])
|
||||
if len(input) > maxInputLength {
|
||||
return Service{}, fmt.Errorf("input exceeds the maximum allowed length")
|
||||
}
|
||||
|
||||
parts = removeKeywords(parts, "docker", "run")
|
||||
|
||||
for i := 0; i < len(parts); i++ {
|
||||
// Split the flag and its value when an equal sign is present
|
||||
partsWithEqual := strings.SplitN(parts[i], "=", 2)
|
||||
flag := partsWithEqual[0]
|
||||
value := ""
|
||||
if len(partsWithEqual) > 1 {
|
||||
value = partsWithEqual[1]
|
||||
}
|
||||
|
||||
switch flag {
|
||||
case "-p", "--publish", "-v", "--volume", "-e", "--env", "--name", "--restart", "--network", "--label":
|
||||
if value == "" {
|
||||
i++
|
||||
value = parts[i]
|
||||
}
|
||||
|
||||
switch flag {
|
||||
case "-p", "--publish":
|
||||
ports = append(ports, value)
|
||||
case "-v", "--volume":
|
||||
volumes = append(volumes, value)
|
||||
case "-e", "--env":
|
||||
environment = append(environment, value)
|
||||
case "--name":
|
||||
name = value
|
||||
container = value
|
||||
case "--restart":
|
||||
restart = value
|
||||
case "--network":
|
||||
network = value
|
||||
case "--label":
|
||||
labels = append(labels, value)
|
||||
}
|
||||
default:
|
||||
if !strings.HasPrefix(parts[i], "-") && i == len(parts)-1 {
|
||||
// Ensure the part doesn't start with a hyphen before assigning it as the image name
|
||||
if image == "" && !strings.HasPrefix(parts[i], "-") {
|
||||
image = parts[i]
|
||||
}
|
||||
}
|
||||
@@ -91,6 +126,7 @@ func main() {
|
||||
|
||||
if r.Method == "POST" {
|
||||
dockerRun := r.FormValue("docker_run")
|
||||
pageData.Input = dockerRun // Store the input command
|
||||
service, err := parseDockerRunCommand(dockerRun)
|
||||
|
||||
if err == nil {
|
||||
|
||||
Reference in new issue
Block a user