1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Use StrSlice from pkg/stringutils

Signed-off-by: Antonio Murdaca <runcom@linux.com>
This commit is contained in:
Antonio Murdaca 2015-09-01 20:26:48 +02:00
parent 82415adc81
commit 17999c70c3
3 changed files with 40 additions and 131 deletions

View file

@ -6,6 +6,7 @@ import (
"strings"
"github.com/docker/docker/pkg/nat"
"github.com/docker/docker/pkg/stringutils"
"github.com/docker/docker/pkg/ulimit"
)
@ -209,60 +210,6 @@ func NewLxcConfig(values []KeyValuePair) *LxcConfig {
return &LxcConfig{values}
}
// CapList represents the list of capabilities of the container.
type CapList struct {
caps []string
}
// MarshalJSON marshals (or serializes) the CapList into JSON.
func (c *CapList) MarshalJSON() ([]byte, error) {
if c == nil {
return []byte{}, nil
}
return json.Marshal(c.Slice())
}
// UnmarshalJSON unmarshals (or deserializes) the specified byte slices
// from JSON to a CapList.
func (c *CapList) UnmarshalJSON(b []byte) error {
if len(b) == 0 {
return nil
}
var caps []string
if err := json.Unmarshal(b, &caps); err != nil {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
caps = append(caps, s)
}
c.caps = caps
return nil
}
// Len returns the number of specific kernel capabilities.
func (c *CapList) Len() int {
if c == nil {
return 0
}
return len(c.caps)
}
// Slice returns the specific capabilities into a slice of KeyValuePair.
func (c *CapList) Slice() []string {
if c == nil {
return nil
}
return c.caps
}
// NewCapList creates a CapList from a slice of string.
func NewCapList(caps []string) *CapList {
return &CapList{caps}
}
// HostConfig the non-portable Config structure of a container.
// Here, "non-portable" means "dependent of the host we are running on".
// Portable information *should* appear in Config.
@ -294,8 +241,8 @@ type HostConfig struct {
IpcMode IpcMode // IPC namespace to use for the container
PidMode PidMode // PID namespace to use for the container
UTSMode UTSMode // UTS namespace to use for the container
CapAdd *CapList // List of kernel capabilities to add to the container
CapDrop *CapList // List of kernel capabilities to remove from the container
CapAdd *stringutils.StrSlice // List of kernel capabilities to add to the container
CapDrop *stringutils.StrSlice // List of kernel capabilities to remove from the container
GroupAdd []string // List of additional groups that the container process will run as
RestartPolicy RestartPolicy // Restart policy to be used for the container
SecurityOpt []string // List of string values to customize labels for MLS systems, such as SELinux.

View file

@ -4,7 +4,6 @@ package runconfig
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"testing"
@ -267,40 +266,3 @@ func TestDecodeHostConfig(t *testing.T) {
}
}
}
func TestCapListUnmarshalSliceAndString(t *testing.T) {
var cl *CapList
cap0, err := json.Marshal([]string{"CAP_SOMETHING"})
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(cap0, &cl); err != nil {
t.Fatal(err)
}
slice := cl.Slice()
if len(slice) != 1 {
t.Fatalf("expected 1 element after unmarshal: %q", slice)
}
if slice[0] != "CAP_SOMETHING" {
t.Fatalf("expected `CAP_SOMETHING`, got: %q", slice[0])
}
cap1, err := json.Marshal("CAP_SOMETHING")
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(cap1, &cl); err != nil {
t.Fatal(err)
}
slice = cl.Slice()
if len(slice) != 1 {
t.Fatalf("expected 1 element after unmarshal: %q", slice)
}
if slice[0] != "CAP_SOMETHING" {
t.Fatalf("expected `CAP_SOMETHING`, got: %q", slice[0])
}
}

View file

@ -353,8 +353,8 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
PidMode: pidMode,
UTSMode: utsMode,
Devices: deviceMappings,
CapAdd: NewCapList(flCapAdd.GetAll()),
CapDrop: NewCapList(flCapDrop.GetAll()),
CapAdd: stringutils.NewStrSlice(flCapAdd.GetAll()...),
CapDrop: stringutils.NewStrSlice(flCapDrop.GetAll()...),
GroupAdd: flGroupAdd.GetAll(),
RestartPolicy: restartPolicy,
SecurityOpt: flSecurityOpt.GetAll(),