Skip to content

Commit

Permalink
Split parse.go, temporarily disable some flags due to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
turtletowerz committed Oct 29, 2024
1 parent 3aaf61e commit 7ba3326
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 94 deletions.
86 changes: 1 addition & 85 deletions parse.go → func.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"slices"
"strconv"
"strings"
Expand All @@ -12,90 +11,6 @@ import (
"github.com/docker/docker/oci/caps"
)

type option interface {
Values() []string
}

// Default generic option
type opt[T comparable] struct {
v T
def T
name string
}

func (o opt[T]) Values() []string {
if o.v == o.def {
return nil
}

// If a flag ends wtih "=" or " " then it needs the value. If not it's probably a boolean flag
if strings.HasSuffix(o.name, "=") || strings.HasSuffix(o.name, " ") {
return []string{o.name + fmt.Sprintf("%v", o.v)}
}
return []string{o.name}
}

// For handling pointers
type optPtr[T comparable] struct {
v *T
def T
name string
}

func (o optPtr[T]) Values() []string {
if o.v == nil {
return nil
}
n := opt[T]{*o.v, o.def, o.name}
return n.Values()
}

// Slice option for handling slices
type optSlice[T comparable] struct {
v []T
def []T
name string
}

// TODO: Some options, like --security-opt, may need to be quoted. Maybe make a separate handler?
func (o optSlice[T]) Values() (ret []string) {
if o.def == nil {
o.def = *new([]T)
}

if o.v != nil {
for _, val := range o.v {
if !slices.Contains(o.def, val) {
ret = append(ret, o.name+strings.ReplaceAll(fmt.Sprintf("%v", val), "\"", "\\\"")) // TODO
}
}
}
return
}

type optMap struct {
v map[string]string
name string
}

// TODO: Some options, like --security-opt, may need to be quoted. Maybe make a separate handler?
func (o optMap) Values() (ret []string) {
for k, v := range o.v {
ret = append(ret, o.name+k+"="+v)
}
return
}

// Allows a custom function to be passed to handle the case.
type optFunc[T any] struct {
v T
f func(T) []string
}

func (o optFunc[T]) Values() []string {
return o.f(o.v)
}

type twoOf[T any] struct {
first T
second T
Expand Down Expand Up @@ -138,6 +53,7 @@ func handleIsolation(iso container.Isolation) []string {
return nil
}

// TODO: May need more work: https://docs.docker.com/reference/cli/docker/container/run/#network
func handleNetworkMode(n container.NetworkMode) []string {
if !(n.IsDefault() || n.IsBridge()) {
return []string{"--network=" + string(n)}
Expand Down
18 changes: 9 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func parseFromJSON(cli *client.Client, ct *types.ContainerJSON) ([]string, error
return nil, errors.Wrap(err, "getting container image")
}

// TODO: --platform, --disable-content-trust (untrusted), --pull, --quiet, --detach, --sig-proxy, and --detach-keys are not stored in the container config so we cannot inspect them, how should this be solved?
// TODO: --sig-proxy, and are not stored in the container config so we cannot inspect them, how should this be solved?

namesplit := strings.Split(ct.Name, "/")
flags := []string{"docker run", "--name=" + namesplit[len(namesplit)-1]}
Expand Down Expand Up @@ -60,18 +60,18 @@ func parseFromJSON(cli *client.Client, ct *types.ContainerJSON) ([]string, error
// Networking stuff
// TODO: Put hostname, MAC address and other network settings behind an optional network flag?
optFunc[*types.ContainerJSON]{ct, handlePorts},
//opt[string]{ct.Config.Hostname, "", "--hostname="}
opt[string]{ct.Config.Hostname, "", "--hostname="},
//opt[string]{ct.NetworkSettings.MacAddress, "", "--mac-address="},
optFunc[container.NetworkMode]{ct.HostConfig.NetworkMode, handleNetworkMode},
//optFunc[container.NetworkMode]{ct.HostConfig.NetworkMode, handleNetworkMode},
optSlice[string]{ct.HostConfig.ExtraHosts, nil, "--add-host "},
optSlice[string]{ct.HostConfig.DNS, nil, "--dns="},
optSlice[string]{ct.HostConfig.DNSOptions, nil, "--dns-option="},
optSlice[string]{ct.HostConfig.DNSSearch, nil, "--dns-search="},
opt[string]{ct.Config.Domainname, "", "--domainname="},

opt[bool]{ct.Config.AttachStdin, false, "--attach stdin"},
opt[bool]{ct.Config.AttachStdout, false, "-attach stdout"},
opt[bool]{ct.Config.AttachStderr, false, "-attach stderr"},
opt[bool]{ct.Config.AttachStdout, false, "--attach stdout"},
opt[bool]{ct.Config.AttachStderr, false, "--attach stderr"},
opt[string]{ct.HostConfig.ContainerIDFile, "", "--cidfile "},

optFunc[*container.HealthConfig]{ct.Config.Healthcheck, handleHealthcheck},
Expand Down Expand Up @@ -123,10 +123,10 @@ func parseFromJSON(cli *client.Client, ct *types.ContainerJSON) ([]string, error

opt[uint16]{ct.HostConfig.BlkioWeight, 0, "--blkio-weight="},
optSlice[*blkiodev.WeightDevice]{ct.HostConfig.BlkioWeightDevice, nil, "--blkio-weight-device="},
optSlice[*blkiodev.ThrottleDevice]{ct.HostConfig.BlkioDeviceReadBps, nil, "--blkio-read-bps="},
optSlice[*blkiodev.ThrottleDevice]{ct.HostConfig.BlkioDeviceReadIOps, nil, "--blkio-read-iops="},
optSlice[*blkiodev.ThrottleDevice]{ct.HostConfig.BlkioDeviceWriteBps, nil, "--blkio-write-bps="},
optSlice[*blkiodev.ThrottleDevice]{ct.HostConfig.BlkioDeviceWriteIOps, nil, "--blkio-write-iops="},
optSlice[*blkiodev.ThrottleDevice]{ct.HostConfig.BlkioDeviceReadBps, nil, "--device-read-bps="},
optSlice[*blkiodev.ThrottleDevice]{ct.HostConfig.BlkioDeviceReadIOps, nil, "--device-read-iops="},
optSlice[*blkiodev.ThrottleDevice]{ct.HostConfig.BlkioDeviceWriteBps, nil, "--device-write-bps="},
optSlice[*blkiodev.ThrottleDevice]{ct.HostConfig.BlkioDeviceWriteIOps, nil, "--device-write-iops="},

opt[string]{ct.Config.StopSignal, "", "--stop-signal="},
optPtr[int]{ct.Config.StopTimeout, -1, "--stop-timeout="},
Expand Down
91 changes: 91 additions & 0 deletions opt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"fmt"
"slices"
"strings"
)

type option interface {
Values() []string
}

// Default generic option
type opt[T comparable] struct {
v T
def T
name string
}

func (o opt[T]) Values() []string {
if o.v == o.def {
return nil
}

// If a flag ends wtih "=" or " " then it needs the value. If not it's probably a boolean flag
if strings.HasSuffix(o.name, "=") || strings.HasSuffix(o.name, " ") {
return []string{o.name + fmt.Sprintf("%v", o.v)}
}
return []string{o.name}
}

// For handling pointers
type optPtr[T comparable] struct {
v *T
def T
name string
}

func (o optPtr[T]) Values() []string {
if o.v == nil {
return nil
}
n := opt[T]{*o.v, o.def, o.name}
return n.Values()
}

// Slice option for handling slices
type optSlice[T comparable] struct {
v []T
def []T
name string
}

// TODO: Some options, like --security-opt, may need to be quoted. Maybe make a separate handler?
func (o optSlice[T]) Values() (ret []string) {
if o.def == nil {
o.def = *new([]T)
}

if o.v != nil {
for _, val := range o.v {
if !slices.Contains(o.def, val) {
ret = append(ret, o.name+strings.ReplaceAll(fmt.Sprintf("%v", val), "\"", "\\\"")) // TODO
}
}
}
return
}

type optMap struct {
v map[string]string
name string
}

// TODO: Some options, like --security-opt, may need to be quoted. Maybe make a separate handler?
func (o optMap) Values() (ret []string) {
for k, v := range o.v {
ret = append(ret, o.name+k+"="+v)
}
return
}

// Allows a custom function to be passed to handle the case.
type optFunc[T any] struct {
v T
f func(T) []string
}

func (o optFunc[T]) Values() []string {
return o.f(o.v)
}

0 comments on commit 7ba3326

Please sign in to comment.