diff options
Diffstat (limited to 'pkg')
104 files changed, 1433 insertions, 2429 deletions
diff --git a/pkg/api/handlers/compat/containers.go b/pkg/api/handlers/compat/containers.go index b41987800..86508f938 100644 --- a/pkg/api/handlers/compat/containers.go +++ b/pkg/api/handlers/compat/containers.go @@ -233,8 +233,8 @@ func KillContainer(w http.ResponseWriter, r *http.Request) { return } if sig == 0 || syscall.Signal(sig) == syscall.SIGKILL { - var opts entities.WaitOptions - if _, err := containerEngine.ContainerWait(r.Context(), []string{name}, opts); err != nil { + if _, err := utils.WaitContainer(w, r); err != nil { + utils.Error(w, "Something went wrong.", http.StatusInternalServerError, err) return } diff --git a/pkg/api/handlers/compat/resize.go b/pkg/api/handlers/compat/resize.go index cc8c6ef0a..a769ae1b5 100644 --- a/pkg/api/handlers/compat/resize.go +++ b/pkg/api/handlers/compat/resize.go @@ -84,5 +84,5 @@ func ResizeTTY(w http.ResponseWriter, r *http.Request) { // reasons. status = http.StatusCreated } - utils.WriteResponse(w, status, "") + w.WriteHeader(status) } diff --git a/pkg/api/handlers/libpod/pods.go b/pkg/api/handlers/libpod/pods.go index 2409d3a20..2c35dd191 100644 --- a/pkg/api/handlers/libpod/pods.go +++ b/pkg/api/handlers/libpod/pods.go @@ -139,19 +139,20 @@ func PodStop(w http.ResponseWriter, r *http.Request) { logrus.Errorf("Error cleaning up pod %s container %s: %v", pod.ID(), id, err) } } - var errs []error //nolint + + report := entities.PodStopReport{Id: pod.ID()} for id, err := range responses { - errs = append(errs, errors.Wrapf(err, "error stopping container %s", id)) + report.Errs = append(report.Errs, errors.Wrapf(err, "error stopping container %s", id)) } - report := entities.PodStopReport{ - Errs: errs, - Id: pod.ID(), + + code := http.StatusOK + if len(report.Errs) > 0 { + code = http.StatusConflict } - utils.WriteResponse(w, http.StatusOK, report) + utils.WriteResponse(w, code, report) } func PodStart(w http.ResponseWriter, r *http.Request) { - var errs []error //nolint runtime := r.Context().Value("runtime").(*libpod.Runtime) name := utils.GetName(r) pod, err := runtime.LookupPod(name) @@ -168,19 +169,23 @@ func PodStart(w http.ResponseWriter, r *http.Request) { utils.WriteResponse(w, http.StatusNotModified, "") return } + responses, err := pod.Start(r.Context()) if err != nil && errors.Cause(err) != define.ErrPodPartialFail { - utils.Error(w, "Something went wrong", http.StatusInternalServerError, err) + utils.Error(w, "Something went wrong", http.StatusConflict, err) return } + + report := entities.PodStartReport{Id: pod.ID()} for id, err := range responses { - errs = append(errs, errors.Wrapf(err, "error starting container %s", id)) + report.Errs = append(report.Errs, errors.Wrapf(err, "error starting container "+id)) } - report := entities.PodStartReport{ - Errs: errs, - Id: pod.ID(), + + code := http.StatusOK + if len(report.Errs) > 0 { + code = http.StatusConflict } - utils.WriteResponse(w, http.StatusOK, report) + utils.WriteResponse(w, code, report) } func PodDelete(w http.ResponseWriter, r *http.Request) { @@ -209,14 +214,11 @@ func PodDelete(w http.ResponseWriter, r *http.Request) { utils.Error(w, "Something went wrong", http.StatusInternalServerError, err) return } - report := entities.PodRmReport{ - Id: pod.ID(), - } + report := entities.PodRmReport{Id: pod.ID()} utils.WriteResponse(w, http.StatusOK, report) } func PodRestart(w http.ResponseWriter, r *http.Request) { - var errs []error //nolint runtime := r.Context().Value("runtime").(*libpod.Runtime) name := utils.GetName(r) pod, err := runtime.LookupPod(name) @@ -229,14 +231,17 @@ func PodRestart(w http.ResponseWriter, r *http.Request) { utils.Error(w, "Something went wrong", http.StatusInternalServerError, err) return } + + report := entities.PodRestartReport{Id: pod.ID()} for id, err := range responses { - errs = append(errs, errors.Wrapf(err, "error restarting container %s", id)) + report.Errs = append(report.Errs, errors.Wrapf(err, "error restarting container %s", id)) } - report := entities.PodRestartReport{ - Errs: errs, - Id: pod.ID(), + + code := http.StatusOK + if len(report.Errs) > 0 { + code = http.StatusConflict } - utils.WriteResponse(w, http.StatusOK, report) + utils.WriteResponse(w, code, report) } func PodPrune(w http.ResponseWriter, r *http.Request) { @@ -267,7 +272,6 @@ func PodPruneHelper(r *http.Request) ([]*entities.PodPruneReport, error) { } func PodPause(w http.ResponseWriter, r *http.Request) { - var errs []error //nolint runtime := r.Context().Value("runtime").(*libpod.Runtime) name := utils.GetName(r) pod, err := runtime.LookupPod(name) @@ -280,18 +284,20 @@ func PodPause(w http.ResponseWriter, r *http.Request) { utils.Error(w, "Something went wrong", http.StatusInternalServerError, err) return } + + report := entities.PodPauseReport{Id: pod.ID()} for id, v := range responses { - errs = append(errs, errors.Wrapf(v, "error pausing container %s", id)) + report.Errs = append(report.Errs, errors.Wrapf(v, "error pausing container %s", id)) } - report := entities.PodPauseReport{ - Errs: errs, - Id: pod.ID(), + + code := http.StatusOK + if len(report.Errs) > 0 { + code = http.StatusConflict } - utils.WriteResponse(w, http.StatusOK, report) + utils.WriteResponse(w, code, report) } func PodUnpause(w http.ResponseWriter, r *http.Request) { - var errs []error //nolint runtime := r.Context().Value("runtime").(*libpod.Runtime) name := utils.GetName(r) pod, err := runtime.LookupPod(name) @@ -304,14 +310,17 @@ func PodUnpause(w http.ResponseWriter, r *http.Request) { utils.Error(w, "failed to pause pod", http.StatusInternalServerError, err) return } + + report := entities.PodUnpauseReport{Id: pod.ID()} for id, v := range responses { - errs = append(errs, errors.Wrapf(v, "error unpausing container %s", id)) + report.Errs = append(report.Errs, errors.Wrapf(v, "error unpausing container %s", id)) } - report := entities.PodUnpauseReport{ - Errs: errs, - Id: pod.ID(), + + code := http.StatusOK + if len(report.Errs) > 0 { + code = http.StatusConflict } - utils.WriteResponse(w, http.StatusOK, &report) + utils.WriteResponse(w, code, &report) } func PodTop(w http.ResponseWriter, r *http.Request) { @@ -361,7 +370,6 @@ func PodKill(w http.ResponseWriter, r *http.Request) { runtime = r.Context().Value("runtime").(*libpod.Runtime) decoder = r.Context().Value("decoder").(*schema.Decoder) signal = "SIGKILL" - errs []error //nolint ) query := struct { Signal string `schema:"signal"` @@ -413,16 +421,18 @@ func PodKill(w http.ResponseWriter, r *http.Request) { return } + report := &entities.PodKillReport{Id: pod.ID()} for _, v := range responses { if v != nil { - errs = append(errs, v) + report.Errs = append(report.Errs, v) } } - report := &entities.PodKillReport{ - Errs: errs, - Id: pod.ID(), + + code := http.StatusOK + if len(report.Errs) > 0 { + code = http.StatusConflict } - utils.WriteResponse(w, http.StatusOK, report) + utils.WriteResponse(w, code, report) } func PodExists(w http.ResponseWriter, r *http.Request) { diff --git a/pkg/api/handlers/utils/handler.go b/pkg/api/handlers/utils/handler.go index 4bab1c93f..b3c674788 100644 --- a/pkg/api/handlers/utils/handler.go +++ b/pkg/api/handlers/utils/handler.go @@ -1,16 +1,17 @@ package utils import ( - "encoding/json" "fmt" "io" "net/http" "net/url" "os" "strings" + "unsafe" "github.com/blang/semver" "github.com/gorilla/mux" + jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -144,6 +145,50 @@ func WriteResponse(w http.ResponseWriter, code int, value interface{}) { } } +func init() { + jsoniter.RegisterTypeEncoderFunc("error", MarshalErrorJSON, MarshalErrorJSONIsEmpty) + jsoniter.RegisterTypeEncoderFunc("[]error", MarshalErrorSliceJSON, MarshalErrorSliceJSONIsEmpty) +} + +var json = jsoniter.ConfigCompatibleWithStandardLibrary + +// MarshalErrorJSON writes error to stream as string +func MarshalErrorJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) { + p := *((*error)(ptr)) + if p == nil { + stream.WriteNil() + } else { + stream.WriteString(p.Error()) + } +} + +// MarshalErrorSliceJSON writes []error to stream as []string JSON blob +func MarshalErrorSliceJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) { + a := *((*[]error)(ptr)) + switch { + case len(a) == 0: + stream.WriteNil() + default: + stream.WriteArrayStart() + for i, e := range a { + if i > 0 { + stream.WriteMore() + } + stream.WriteString(e.Error()) + } + stream.WriteArrayEnd() + } +} + +func MarshalErrorJSONIsEmpty(_ unsafe.Pointer) bool { + return false +} + +func MarshalErrorSliceJSONIsEmpty(_ unsafe.Pointer) bool { + return false +} + +// WriteJSON writes an interface value encoded as JSON to w func WriteJSON(w http.ResponseWriter, code int, value interface{}) { // FIXME: we don't need to write the header in all/some circumstances. w.Header().Set("Content-Type", "application/json") diff --git a/pkg/api/server/register_pods.go b/pkg/api/server/register_pods.go index 105de4ee7..4873eb926 100644 --- a/pkg/api/server/register_pods.go +++ b/pkg/api/server/register_pods.go @@ -43,6 +43,11 @@ func (s *APIServer) registerPodsHandlers(r *mux.Router) error { // $ref: "#/definitions/IdResponse" // 400: // $ref: "#/responses/BadParamError" + // 409: + // description: status conflict + // schema: + // type: string + // description: message describing error // 500: // $ref: "#/responses/InternalError" r.Handle(VersionedPath("/libpod/pods/create"), s.APIHandler(libpod.PodCreate)).Methods(http.MethodPost) @@ -149,7 +154,7 @@ func (s *APIServer) registerPodsHandlers(r *mux.Router) error { // 404: // $ref: "#/responses/NoSuchPod" // 409: - // $ref: "#/responses/ConflictError" + // $ref: "#/responses/PodKillReport" // 500: // $ref: "#/responses/InternalError" r.Handle(VersionedPath("/libpod/pods/{name}/kill"), s.APIHandler(libpod.PodKill)).Methods(http.MethodPost) @@ -170,6 +175,8 @@ func (s *APIServer) registerPodsHandlers(r *mux.Router) error { // $ref: '#/responses/PodPauseReport' // 404: // $ref: "#/responses/NoSuchPod" + // 409: + // $ref: '#/responses/PodPauseReport' // 500: // $ref: "#/responses/InternalError" r.Handle(VersionedPath("/libpod/pods/{name}/pause"), s.APIHandler(libpod.PodPause)).Methods(http.MethodPost) @@ -189,6 +196,8 @@ func (s *APIServer) registerPodsHandlers(r *mux.Router) error { // $ref: '#/responses/PodRestartReport' // 404: // $ref: "#/responses/NoSuchPod" + // 409: + // $ref: "#/responses/PodRestartReport" // 500: // $ref: "#/responses/InternalError" r.Handle(VersionedPath("/libpod/pods/{name}/restart"), s.APIHandler(libpod.PodRestart)).Methods(http.MethodPost) @@ -210,6 +219,8 @@ func (s *APIServer) registerPodsHandlers(r *mux.Router) error { // $ref: "#/responses/PodAlreadyStartedError" // 404: // $ref: "#/responses/NoSuchPod" + // 409: + // $ref: '#/responses/PodStartReport' // 500: // $ref: "#/responses/InternalError" r.Handle(VersionedPath("/libpod/pods/{name}/start"), s.APIHandler(libpod.PodStart)).Methods(http.MethodPost) @@ -237,6 +248,8 @@ func (s *APIServer) registerPodsHandlers(r *mux.Router) error { // $ref: "#/responses/BadParamError" // 404: // $ref: "#/responses/NoSuchPod" + // 409: + // $ref: "#/responses/PodStopReport" // 500: // $ref: "#/responses/InternalError" r.Handle(VersionedPath("/libpod/pods/{name}/stop"), s.APIHandler(libpod.PodStop)).Methods(http.MethodPost) @@ -256,6 +269,8 @@ func (s *APIServer) registerPodsHandlers(r *mux.Router) error { // $ref: '#/responses/PodUnpauseReport' // 404: // $ref: "#/responses/NoSuchPod" + // 409: + // $ref: '#/responses/PodUnpauseReport' // 500: // $ref: "#/responses/InternalError" r.Handle(VersionedPath("/libpod/pods/{name}/unpause"), s.APIHandler(libpod.PodUnpause)).Methods(http.MethodPost) diff --git a/pkg/bindings/containers/attach.go b/pkg/bindings/containers/attach.go index 69ae7a32f..586cdec8c 100644 --- a/pkg/bindings/containers/attach.go +++ b/pkg/bindings/containers/attach.go @@ -348,7 +348,7 @@ func attachHandleResize(ctx, winCtx context.Context, winChange chan os.Signal, i resizeErr = ResizeContainerTTY(ctx, id, new(ResizeTTYOptions).WithHeight(h).WithWidth(w)) } if resizeErr != nil { - logrus.Warnf("failed to resize TTY: %v", err) + logrus.Warnf("failed to resize TTY: %v", resizeErr) } } } diff --git a/pkg/bindings/containers/types_attach_options.go b/pkg/bindings/containers/types_attach_options.go index 6d8c1cb01..ab5a1615c 100644 --- a/pkg/bindings/containers/types_attach_options.go +++ b/pkg/bindings/containers/types_attach_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *AttachOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *AttachOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_checkpoint_options.go b/pkg/bindings/containers/types_checkpoint_options.go index ec766de4a..d239c476f 100644 --- a/pkg/bindings/containers/types_checkpoint_options.go +++ b/pkg/bindings/containers/types_checkpoint_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *CheckpointOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *CheckpointOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_commit_options.go b/pkg/bindings/containers/types_commit_options.go index b745bebe2..061f16e25 100644 --- a/pkg/bindings/containers/types_commit_options.go +++ b/pkg/bindings/containers/types_commit_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *CommitOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *CommitOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_create_options.go b/pkg/bindings/containers/types_create_options.go index 4b9574cf1..8cde11335 100644 --- a/pkg/bindings/containers/types_create_options.go +++ b/pkg/bindings/containers/types_create_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *CreateOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *CreateOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_diff_options.go b/pkg/bindings/containers/types_diff_options.go index 55fa6930d..e912bf041 100644 --- a/pkg/bindings/containers/types_diff_options.go +++ b/pkg/bindings/containers/types_diff_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *DiffOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *DiffOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_execinspect_options.go b/pkg/bindings/containers/types_execinspect_options.go index c5d1f931a..b870db46b 100644 --- a/pkg/bindings/containers/types_execinspect_options.go +++ b/pkg/bindings/containers/types_execinspect_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ExecInspectOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ExecInspectOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_execstart_options.go b/pkg/bindings/containers/types_execstart_options.go index 9ecb70a3e..95f97b1d7 100644 --- a/pkg/bindings/containers/types_execstart_options.go +++ b/pkg/bindings/containers/types_execstart_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ExecStartOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ExecStartOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_execstartandattach_options.go b/pkg/bindings/containers/types_execstartandattach_options.go index a5a691e35..1981c319a 100644 --- a/pkg/bindings/containers/types_execstartandattach_options.go +++ b/pkg/bindings/containers/types_execstartandattach_options.go @@ -5,9 +5,9 @@ import ( "io" "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -45,33 +45,19 @@ func (o *ExecStartAndAttachOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -85,6 +71,7 @@ func (o *ExecStartAndAttachOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_exists_options.go b/pkg/bindings/containers/types_exists_options.go index f0d8885b2..a52777600 100644 --- a/pkg/bindings/containers/types_exists_options.go +++ b/pkg/bindings/containers/types_exists_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ExistsOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ExistsOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_export_options.go b/pkg/bindings/containers/types_export_options.go index 55e413c72..3943a5a3b 100644 --- a/pkg/bindings/containers/types_export_options.go +++ b/pkg/bindings/containers/types_export_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ExportOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ExportOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_healthcheck_options.go b/pkg/bindings/containers/types_healthcheck_options.go index 9d8b25bf4..a548232cd 100644 --- a/pkg/bindings/containers/types_healthcheck_options.go +++ b/pkg/bindings/containers/types_healthcheck_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *HealthCheckOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *HealthCheckOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_init_options.go b/pkg/bindings/containers/types_init_options.go index 6fb5795c0..92e8a6c17 100644 --- a/pkg/bindings/containers/types_init_options.go +++ b/pkg/bindings/containers/types_init_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *InitOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *InitOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_inspect_options.go b/pkg/bindings/containers/types_inspect_options.go index 722372414..fdb84bda8 100644 --- a/pkg/bindings/containers/types_inspect_options.go +++ b/pkg/bindings/containers/types_inspect_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *InspectOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *InspectOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_kill_options.go b/pkg/bindings/containers/types_kill_options.go index c5d5a3c6a..45bd790a4 100644 --- a/pkg/bindings/containers/types_kill_options.go +++ b/pkg/bindings/containers/types_kill_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *KillOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *KillOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_list_options.go b/pkg/bindings/containers/types_list_options.go index c363dcd32..3293320ec 100644 --- a/pkg/bindings/containers/types_list_options.go +++ b/pkg/bindings/containers/types_list_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ListOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ListOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_log_options.go b/pkg/bindings/containers/types_log_options.go index 364f29de4..e78eb7bd0 100644 --- a/pkg/bindings/containers/types_log_options.go +++ b/pkg/bindings/containers/types_log_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *LogOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *LogOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_mount_options.go b/pkg/bindings/containers/types_mount_options.go index 6f4349b73..cc8df1255 100644 --- a/pkg/bindings/containers/types_mount_options.go +++ b/pkg/bindings/containers/types_mount_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *MountOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *MountOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_mountedcontainerpaths_options.go b/pkg/bindings/containers/types_mountedcontainerpaths_options.go index 0d8b69654..78fa2fca0 100644 --- a/pkg/bindings/containers/types_mountedcontainerpaths_options.go +++ b/pkg/bindings/containers/types_mountedcontainerpaths_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *MountedContainerPathsOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *MountedContainerPathsOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_pause_options.go b/pkg/bindings/containers/types_pause_options.go index 0cc65f64e..55f14bef0 100644 --- a/pkg/bindings/containers/types_pause_options.go +++ b/pkg/bindings/containers/types_pause_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *PauseOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *PauseOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_prune_options.go b/pkg/bindings/containers/types_prune_options.go index 10adf0a2a..000c7c0bd 100644 --- a/pkg/bindings/containers/types_prune_options.go +++ b/pkg/bindings/containers/types_prune_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *PruneOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *PruneOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_remove_options.go b/pkg/bindings/containers/types_remove_options.go index ffe1488c1..dfb5367eb 100644 --- a/pkg/bindings/containers/types_remove_options.go +++ b/pkg/bindings/containers/types_remove_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *RemoveOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *RemoveOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_rename_options.go b/pkg/bindings/containers/types_rename_options.go index b7a723f7a..f4f5d1426 100644 --- a/pkg/bindings/containers/types_rename_options.go +++ b/pkg/bindings/containers/types_rename_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *RenameOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *RenameOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_resizeexectty_options.go b/pkg/bindings/containers/types_resizeexectty_options.go index 0212adeb2..e63d965eb 100644 --- a/pkg/bindings/containers/types_resizeexectty_options.go +++ b/pkg/bindings/containers/types_resizeexectty_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ResizeExecTTYOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ResizeExecTTYOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_resizetty_options.go b/pkg/bindings/containers/types_resizetty_options.go index cee607902..3170f4053 100644 --- a/pkg/bindings/containers/types_resizetty_options.go +++ b/pkg/bindings/containers/types_resizetty_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ResizeTTYOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ResizeTTYOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_restart_options.go b/pkg/bindings/containers/types_restart_options.go index 8dcc6b5b7..d59176e67 100644 --- a/pkg/bindings/containers/types_restart_options.go +++ b/pkg/bindings/containers/types_restart_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *RestartOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *RestartOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_restore_options.go b/pkg/bindings/containers/types_restore_options.go index 491d678a5..e9f14fc47 100644 --- a/pkg/bindings/containers/types_restore_options.go +++ b/pkg/bindings/containers/types_restore_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *RestoreOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *RestoreOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_shouldrestart_options.go b/pkg/bindings/containers/types_shouldrestart_options.go index 30ab618c7..49f943460 100644 --- a/pkg/bindings/containers/types_shouldrestart_options.go +++ b/pkg/bindings/containers/types_shouldrestart_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ShouldRestartOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ShouldRestartOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_start_options.go b/pkg/bindings/containers/types_start_options.go index 4050a8993..a0f0b3077 100644 --- a/pkg/bindings/containers/types_start_options.go +++ b/pkg/bindings/containers/types_start_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *StartOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *StartOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_stats_options.go b/pkg/bindings/containers/types_stats_options.go index 74f419913..79e35ba62 100644 --- a/pkg/bindings/containers/types_stats_options.go +++ b/pkg/bindings/containers/types_stats_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *StatsOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *StatsOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_stop_options.go b/pkg/bindings/containers/types_stop_options.go index 940ec5832..f221b16e8 100644 --- a/pkg/bindings/containers/types_stop_options.go +++ b/pkg/bindings/containers/types_stop_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *StopOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *StopOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_top_options.go b/pkg/bindings/containers/types_top_options.go index 5f2717c28..570dd4e90 100644 --- a/pkg/bindings/containers/types_top_options.go +++ b/pkg/bindings/containers/types_top_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *TopOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *TopOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_unmount_options.go b/pkg/bindings/containers/types_unmount_options.go index 060327c4a..24249073e 100644 --- a/pkg/bindings/containers/types_unmount_options.go +++ b/pkg/bindings/containers/types_unmount_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *UnmountOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *UnmountOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_unpause_options.go b/pkg/bindings/containers/types_unpause_options.go index e02bf2c95..3b1d75001 100644 --- a/pkg/bindings/containers/types_unpause_options.go +++ b/pkg/bindings/containers/types_unpause_options.go @@ -3,9 +3,9 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *UnpauseOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *UnpauseOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/containers/types_wait_options.go b/pkg/bindings/containers/types_wait_options.go index 2f5aa983e..005cc38cb 100644 --- a/pkg/bindings/containers/types_wait_options.go +++ b/pkg/bindings/containers/types_wait_options.go @@ -3,10 +3,10 @@ package containers import ( "net/url" "reflect" - "strconv" "strings" "github.com/containers/podman/v2/libpod/define" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -44,33 +44,19 @@ func (o *WaitOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -84,6 +70,7 @@ func (o *WaitOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/generate/types_kube_options.go b/pkg/bindings/generate/types_kube_options.go index 5fb965c9f..218d308e1 100644 --- a/pkg/bindings/generate/types_kube_options.go +++ b/pkg/bindings/generate/types_kube_options.go @@ -1,13 +1,13 @@ package generate import ( + "errors" "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" - "github.com/pkg/errors" ) /* @@ -43,33 +43,19 @@ func (o *KubeOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *KubeOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/generate/types_systemd_options.go b/pkg/bindings/generate/types_systemd_options.go index ce7286b3a..faf981d1b 100644 --- a/pkg/bindings/generate/types_systemd_options.go +++ b/pkg/bindings/generate/types_systemd_options.go @@ -1,13 +1,13 @@ package generate import ( + "errors" "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" - "github.com/pkg/errors" ) /* @@ -43,33 +43,19 @@ func (o *SystemdOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *SystemdOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/generator/generator.go b/pkg/bindings/generator/generator.go index 6a7f600a8..dad154166 100644 --- a/pkg/bindings/generator/generator.go +++ b/pkg/bindings/generator/generator.go @@ -54,33 +54,19 @@ func (o *{{.StructName}}) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -94,6 +80,7 @@ func (o *{{.StructName}}) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } @@ -144,7 +131,7 @@ func main() { panic(err) } // always add reflect - imports := []string{"\"reflect\""} + imports := []string{"\"reflect\"", "\"github.com/containers/podman/v2/pkg/bindings/util\""} for _, imp := range f.Imports { imports = append(imports, imp.Path.Value) } diff --git a/pkg/bindings/images/types_diff_options.go b/pkg/bindings/images/types_diff_options.go index 34a5bf2df..edfc7bfa2 100644 --- a/pkg/bindings/images/types_diff_options.go +++ b/pkg/bindings/images/types_diff_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *DiffOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *DiffOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_exists_options.go b/pkg/bindings/images/types_exists_options.go index f0d4be6ce..649be4862 100644 --- a/pkg/bindings/images/types_exists_options.go +++ b/pkg/bindings/images/types_exists_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ExistsOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ExistsOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_export_options.go b/pkg/bindings/images/types_export_options.go index 172cb2b5c..ebd053165 100644 --- a/pkg/bindings/images/types_export_options.go +++ b/pkg/bindings/images/types_export_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ExportOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ExportOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_get_options.go b/pkg/bindings/images/types_get_options.go index c91ddb170..33ebe2611 100644 --- a/pkg/bindings/images/types_get_options.go +++ b/pkg/bindings/images/types_get_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *GetOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *GetOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_history_options.go b/pkg/bindings/images/types_history_options.go index bd4224cd8..b2c37acea 100644 --- a/pkg/bindings/images/types_history_options.go +++ b/pkg/bindings/images/types_history_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *HistoryOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *HistoryOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_import_options.go b/pkg/bindings/images/types_import_options.go index 81eda946e..e2aed0866 100644 --- a/pkg/bindings/images/types_import_options.go +++ b/pkg/bindings/images/types_import_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ImportOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ImportOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_list_options.go b/pkg/bindings/images/types_list_options.go index 5dc4242fc..e194474b9 100644 --- a/pkg/bindings/images/types_list_options.go +++ b/pkg/bindings/images/types_list_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ListOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ListOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_load_options.go b/pkg/bindings/images/types_load_options.go index 7bbd56c09..7e15d4e03 100644 --- a/pkg/bindings/images/types_load_options.go +++ b/pkg/bindings/images/types_load_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *LoadOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *LoadOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_prune_options.go b/pkg/bindings/images/types_prune_options.go index c290bb379..f86676d53 100644 --- a/pkg/bindings/images/types_prune_options.go +++ b/pkg/bindings/images/types_prune_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *PruneOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *PruneOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_pull_options.go b/pkg/bindings/images/types_pull_options.go index 5452560fb..59e2b6354 100644 --- a/pkg/bindings/images/types_pull_options.go +++ b/pkg/bindings/images/types_pull_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *PullOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *PullOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_push_options.go b/pkg/bindings/images/types_push_options.go index b7d8a6f2d..85f1ffee5 100644 --- a/pkg/bindings/images/types_push_options.go +++ b/pkg/bindings/images/types_push_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *PushOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *PushOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_remove_options.go b/pkg/bindings/images/types_remove_options.go index 66a6bea7d..d79186565 100644 --- a/pkg/bindings/images/types_remove_options.go +++ b/pkg/bindings/images/types_remove_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *RemoveOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *RemoveOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_search_options.go b/pkg/bindings/images/types_search_options.go index 299d27505..a55c9ac89 100644 --- a/pkg/bindings/images/types_search_options.go +++ b/pkg/bindings/images/types_search_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *SearchOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *SearchOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_tag_options.go b/pkg/bindings/images/types_tag_options.go index 40cd4a35b..b323ea41c 100644 --- a/pkg/bindings/images/types_tag_options.go +++ b/pkg/bindings/images/types_tag_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *TagOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *TagOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_tree_options.go b/pkg/bindings/images/types_tree_options.go index a671fa4e0..8e1b16c5c 100644 --- a/pkg/bindings/images/types_tree_options.go +++ b/pkg/bindings/images/types_tree_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *TreeOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *TreeOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/images/types_untag_options.go b/pkg/bindings/images/types_untag_options.go index e38c5f18e..b28670134 100644 --- a/pkg/bindings/images/types_untag_options.go +++ b/pkg/bindings/images/types_untag_options.go @@ -3,9 +3,9 @@ package images import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *UntagOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *UntagOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/manifests/types_add_options.go b/pkg/bindings/manifests/types_add_options.go index 1e588c668..61314c479 100644 --- a/pkg/bindings/manifests/types_add_options.go +++ b/pkg/bindings/manifests/types_add_options.go @@ -1,13 +1,13 @@ package manifests import ( + "errors" "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" - "github.com/pkg/errors" ) /* @@ -43,33 +43,19 @@ func (o *AddOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *AddOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/manifests/types_create_options.go b/pkg/bindings/manifests/types_create_options.go index 3a564a92b..4c7c1397a 100644 --- a/pkg/bindings/manifests/types_create_options.go +++ b/pkg/bindings/manifests/types_create_options.go @@ -1,13 +1,13 @@ package manifests import ( + "errors" "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" - "github.com/pkg/errors" ) /* @@ -43,33 +43,19 @@ func (o *CreateOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *CreateOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/manifests/types_exists_options.go b/pkg/bindings/manifests/types_exists_options.go index fd2cd3ee9..2a4962ae6 100644 --- a/pkg/bindings/manifests/types_exists_options.go +++ b/pkg/bindings/manifests/types_exists_options.go @@ -1,13 +1,13 @@ package manifests import ( + "errors" "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" - "github.com/pkg/errors" ) /* @@ -43,33 +43,19 @@ func (o *ExistsOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ExistsOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/manifests/types_inspect_options.go b/pkg/bindings/manifests/types_inspect_options.go index 2af4190d4..0b82fc3cf 100644 --- a/pkg/bindings/manifests/types_inspect_options.go +++ b/pkg/bindings/manifests/types_inspect_options.go @@ -1,13 +1,13 @@ package manifests import ( + "errors" "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" - "github.com/pkg/errors" ) /* @@ -43,33 +43,19 @@ func (o *InspectOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *InspectOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/manifests/types_remove_options.go b/pkg/bindings/manifests/types_remove_options.go index 3b35c38b8..6ed0fd329 100644 --- a/pkg/bindings/manifests/types_remove_options.go +++ b/pkg/bindings/manifests/types_remove_options.go @@ -1,13 +1,13 @@ package manifests import ( + "errors" "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" - "github.com/pkg/errors" ) /* @@ -43,33 +43,19 @@ func (o *RemoveOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *RemoveOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/network/types_connect_options.go b/pkg/bindings/network/types_connect_options.go index b6081ba57..4440bbed4 100644 --- a/pkg/bindings/network/types_connect_options.go +++ b/pkg/bindings/network/types_connect_options.go @@ -3,9 +3,9 @@ package network import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ConnectOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ConnectOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/network/types_create_options.go b/pkg/bindings/network/types_create_options.go index 5b0abe870..5fbdce93a 100644 --- a/pkg/bindings/network/types_create_options.go +++ b/pkg/bindings/network/types_create_options.go @@ -4,9 +4,9 @@ import ( "net" "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -44,33 +44,19 @@ func (o *CreateOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -84,6 +70,7 @@ func (o *CreateOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/network/types_disconnect_options.go b/pkg/bindings/network/types_disconnect_options.go index 8b2a9cb71..947f2f114 100644 --- a/pkg/bindings/network/types_disconnect_options.go +++ b/pkg/bindings/network/types_disconnect_options.go @@ -3,9 +3,9 @@ package network import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *DisconnectOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *DisconnectOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/network/types_exists_options.go b/pkg/bindings/network/types_exists_options.go index 8076a18e8..2fad34789 100644 --- a/pkg/bindings/network/types_exists_options.go +++ b/pkg/bindings/network/types_exists_options.go @@ -3,9 +3,9 @@ package network import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ExistsOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ExistsOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/network/types_inspect_options.go b/pkg/bindings/network/types_inspect_options.go index cec5ef7b2..144ccbfae 100644 --- a/pkg/bindings/network/types_inspect_options.go +++ b/pkg/bindings/network/types_inspect_options.go @@ -3,9 +3,9 @@ package network import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *InspectOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *InspectOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/network/types_list_options.go b/pkg/bindings/network/types_list_options.go index 6a33fb7b6..60632ce33 100644 --- a/pkg/bindings/network/types_list_options.go +++ b/pkg/bindings/network/types_list_options.go @@ -3,9 +3,9 @@ package network import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ListOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ListOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/network/types_remove_options.go b/pkg/bindings/network/types_remove_options.go index 861fe1f2c..4ad4a2301 100644 --- a/pkg/bindings/network/types_remove_options.go +++ b/pkg/bindings/network/types_remove_options.go @@ -3,9 +3,9 @@ package network import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *RemoveOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *RemoveOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/play/types_kube_options.go b/pkg/bindings/play/types_kube_options.go index 5aec4b479..ea3872aae 100644 --- a/pkg/bindings/play/types_kube_options.go +++ b/pkg/bindings/play/types_kube_options.go @@ -1,13 +1,13 @@ package play import ( + "errors" "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" - "github.com/pkg/errors" ) /* @@ -43,33 +43,19 @@ func (o *KubeOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *KubeOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_create_options.go b/pkg/bindings/pods/types_create_options.go index b501d1151..cfa29c6be 100644 --- a/pkg/bindings/pods/types_create_options.go +++ b/pkg/bindings/pods/types_create_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *CreateOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *CreateOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_exists_options.go b/pkg/bindings/pods/types_exists_options.go index 7221afe74..6149ab1cc 100644 --- a/pkg/bindings/pods/types_exists_options.go +++ b/pkg/bindings/pods/types_exists_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ExistsOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ExistsOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_inspect_options.go b/pkg/bindings/pods/types_inspect_options.go index a2eb25fef..281717ff1 100644 --- a/pkg/bindings/pods/types_inspect_options.go +++ b/pkg/bindings/pods/types_inspect_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *InspectOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *InspectOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_kill_options.go b/pkg/bindings/pods/types_kill_options.go index f9cad3579..4c310d50c 100644 --- a/pkg/bindings/pods/types_kill_options.go +++ b/pkg/bindings/pods/types_kill_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *KillOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *KillOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_list_options.go b/pkg/bindings/pods/types_list_options.go index 02e7adf2d..20f3229e5 100644 --- a/pkg/bindings/pods/types_list_options.go +++ b/pkg/bindings/pods/types_list_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ListOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ListOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_pause_options.go b/pkg/bindings/pods/types_pause_options.go index 2e4fdb4a6..0f0f5bd97 100644 --- a/pkg/bindings/pods/types_pause_options.go +++ b/pkg/bindings/pods/types_pause_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *PauseOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *PauseOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_prune_options.go b/pkg/bindings/pods/types_prune_options.go index 616ad6dc9..ef8aae17f 100644 --- a/pkg/bindings/pods/types_prune_options.go +++ b/pkg/bindings/pods/types_prune_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *PruneOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *PruneOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_remove_options.go b/pkg/bindings/pods/types_remove_options.go index 6960d8839..f51f67129 100644 --- a/pkg/bindings/pods/types_remove_options.go +++ b/pkg/bindings/pods/types_remove_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *RemoveOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *RemoveOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_restart_options.go b/pkg/bindings/pods/types_restart_options.go index 427833044..ec05e9fc9 100644 --- a/pkg/bindings/pods/types_restart_options.go +++ b/pkg/bindings/pods/types_restart_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *RestartOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *RestartOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_start_options.go b/pkg/bindings/pods/types_start_options.go index e98798459..ec9f5b1de 100644 --- a/pkg/bindings/pods/types_start_options.go +++ b/pkg/bindings/pods/types_start_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *StartOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *StartOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_stats_options.go b/pkg/bindings/pods/types_stats_options.go index 845a534a3..8be7d175d 100644 --- a/pkg/bindings/pods/types_stats_options.go +++ b/pkg/bindings/pods/types_stats_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *StatsOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *StatsOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_stop_options.go b/pkg/bindings/pods/types_stop_options.go index 86000eb57..fa3577e72 100644 --- a/pkg/bindings/pods/types_stop_options.go +++ b/pkg/bindings/pods/types_stop_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *StopOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *StopOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_top_options.go b/pkg/bindings/pods/types_top_options.go index ada0b1e25..c3c701dad 100644 --- a/pkg/bindings/pods/types_top_options.go +++ b/pkg/bindings/pods/types_top_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *TopOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *TopOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/pods/types_unpause_options.go b/pkg/bindings/pods/types_unpause_options.go index 6a9ee8fcd..281f0ea8d 100644 --- a/pkg/bindings/pods/types_unpause_options.go +++ b/pkg/bindings/pods/types_unpause_options.go @@ -3,9 +3,9 @@ package pods import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *UnpauseOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *UnpauseOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/system/types_disk_options.go b/pkg/bindings/system/types_disk_options.go index c5eb2f94c..6f0c3735a 100644 --- a/pkg/bindings/system/types_disk_options.go +++ b/pkg/bindings/system/types_disk_options.go @@ -3,9 +3,9 @@ package system import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *DiskOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *DiskOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/system/types_events_options.go b/pkg/bindings/system/types_events_options.go index 2e95339e6..401a9807e 100644 --- a/pkg/bindings/system/types_events_options.go +++ b/pkg/bindings/system/types_events_options.go @@ -3,9 +3,9 @@ package system import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *EventsOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *EventsOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/system/types_info_options.go b/pkg/bindings/system/types_info_options.go index 263513b0f..7c07b5081 100644 --- a/pkg/bindings/system/types_info_options.go +++ b/pkg/bindings/system/types_info_options.go @@ -3,9 +3,9 @@ package system import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *InfoOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *InfoOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/system/types_prune_options.go b/pkg/bindings/system/types_prune_options.go index a9a6a6cda..c677ccca6 100644 --- a/pkg/bindings/system/types_prune_options.go +++ b/pkg/bindings/system/types_prune_options.go @@ -3,9 +3,9 @@ package system import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *PruneOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *PruneOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/system/types_version_options.go b/pkg/bindings/system/types_version_options.go index be07581fa..60ebfced9 100644 --- a/pkg/bindings/system/types_version_options.go +++ b/pkg/bindings/system/types_version_options.go @@ -3,9 +3,9 @@ package system import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *VersionOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *VersionOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/util/util.go b/pkg/bindings/util/util.go new file mode 100644 index 000000000..403846355 --- /dev/null +++ b/pkg/bindings/util/util.go @@ -0,0 +1,30 @@ +package util + +import ( + "reflect" + "strconv" +) + +func IsSimpleType(f reflect.Value) bool { + switch f.Kind() { + case reflect.Bool, reflect.Int, reflect.Int64, reflect.Uint, reflect.Uint64, reflect.String: + return true + } + return false +} + +func SimpleTypeToParam(f reflect.Value) string { + switch f.Kind() { + case reflect.Bool: + return strconv.FormatBool(f.Bool()) + case reflect.Int, reflect.Int64: + // f.Int() is always an int64 + return strconv.FormatInt(f.Int(), 10) + case reflect.Uint, reflect.Uint64: + // f.Uint() is always an uint64 + return strconv.FormatUint(f.Uint(), 10) + case reflect.String: + return f.String() + } + panic("the input parameter is not a simple type") +} diff --git a/pkg/bindings/volumes/types_create_options.go b/pkg/bindings/volumes/types_create_options.go index 171090afe..2254f8c13 100644 --- a/pkg/bindings/volumes/types_create_options.go +++ b/pkg/bindings/volumes/types_create_options.go @@ -3,9 +3,9 @@ package volumes import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *CreateOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *CreateOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/volumes/types_exists_options.go b/pkg/bindings/volumes/types_exists_options.go index c66586a23..9840b1bcd 100644 --- a/pkg/bindings/volumes/types_exists_options.go +++ b/pkg/bindings/volumes/types_exists_options.go @@ -3,9 +3,9 @@ package volumes import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ExistsOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ExistsOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/volumes/types_inspect_options.go b/pkg/bindings/volumes/types_inspect_options.go index 3a1d396a7..51ac2d348 100644 --- a/pkg/bindings/volumes/types_inspect_options.go +++ b/pkg/bindings/volumes/types_inspect_options.go @@ -3,9 +3,9 @@ package volumes import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *InspectOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *InspectOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/volumes/types_list_options.go b/pkg/bindings/volumes/types_list_options.go index 56033a575..c96e647b0 100644 --- a/pkg/bindings/volumes/types_list_options.go +++ b/pkg/bindings/volumes/types_list_options.go @@ -3,9 +3,9 @@ package volumes import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *ListOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *ListOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/volumes/types_prune_options.go b/pkg/bindings/volumes/types_prune_options.go index c043d69d0..06d16b659 100644 --- a/pkg/bindings/volumes/types_prune_options.go +++ b/pkg/bindings/volumes/types_prune_options.go @@ -3,9 +3,9 @@ package volumes import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *PruneOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *PruneOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/bindings/volumes/types_remove_options.go b/pkg/bindings/volumes/types_remove_options.go index 1f8ba4e22..4b0037234 100644 --- a/pkg/bindings/volumes/types_remove_options.go +++ b/pkg/bindings/volumes/types_remove_options.go @@ -3,9 +3,9 @@ package volumes import ( "net/url" "reflect" - "strconv" "strings" + "github.com/containers/podman/v2/pkg/bindings/util" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) @@ -43,33 +43,19 @@ func (o *RemoveOptions) ToParams() (url.Values, error) { if reflect.Ptr == f.Kind() { f = f.Elem() } - switch f.Kind() { - case reflect.Bool: - params.Set(fieldName, strconv.FormatBool(f.Bool())) - case reflect.String: - params.Set(fieldName, f.String()) - case reflect.Int, reflect.Int64: - // f.Int() is always an int64 - params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) - case reflect.Uint, reflect.Uint64: - // f.Uint() is always an uint64 - params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) - case reflect.Slice: - typ := reflect.TypeOf(f.Interface()).Elem() - switch typ.Kind() { - case reflect.String: - sl := f.Slice(0, f.Len()) - s, ok := sl.Interface().([]string) - if !ok { - return nil, errors.New("failed to convert to string slice") + switch { + case util.IsSimpleType(f): + params.Set(fieldName, util.SimpleTypeToParam(f)) + case f.Kind() == reflect.Slice: + for i := 0; i < f.Len(); i++ { + elem := f.Index(i) + if util.IsSimpleType(elem) { + params.Add(fieldName, util.SimpleTypeToParam(elem)) + } else { + return nil, errors.New("slices must contain only simple types") } - for _, val := range s { - params.Add(fieldName, val) - } - default: - return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) } - case reflect.Map: + case f.Kind() == reflect.Map: lowerCaseKeys := make(map[string][]string) iter := f.MapRange() for iter.Next() { @@ -83,6 +69,7 @@ func (o *RemoveOptions) ToParams() (url.Values, error) { params.Set(fieldName, s) } + } return params, nil } diff --git a/pkg/specgen/generate/pod_create.go b/pkg/specgen/generate/pod_create.go index 43caf0fe9..645bf7a47 100644 --- a/pkg/specgen/generate/pod_create.go +++ b/pkg/specgen/generate/pod_create.go @@ -102,6 +102,9 @@ func createPodOptions(p *specgen.PodSpecGenerator, rt *libpod.Runtime) ([]libpod case specgen.Slirp: logrus.Debugf("Pod will use slirp4netns") options = append(options, libpod.WithPodSlirp4netns(p.NetworkOptions)) + case specgen.NoNetwork: + logrus.Debugf("Pod will not use networking") + options = append(options, libpod.WithPodNoNetwork()) default: return nil, errors.Errorf("pods presently do not support network mode %s", p.NetNS.NSMode) } diff --git a/pkg/systemd/generate/common.go b/pkg/systemd/generate/common.go index de6751a17..e9902319c 100644 --- a/pkg/systemd/generate/common.go +++ b/pkg/systemd/generate/common.go @@ -60,13 +60,21 @@ func filterPodFlags(command []string) []string { return processed } -// quoteArguments makes sure that all arguments with at least one whitespace +// escapeSystemdArguments makes sure that all arguments with at least one whitespace // are quoted to make sure those are interpreted as one argument instead of -// multiple ones. -func quoteArguments(command []string) []string { +// multiple ones. Also make sure to escape all characters which have a special +// meaning to systemd -> $,% and \ +// see: https://www.freedesktop.org/software/systemd/man/systemd.service.html#Command%20lines +func escapeSystemdArguments(command []string) []string { for i := range command { + command[i] = strings.ReplaceAll(command[i], "$", "$$") + command[i] = strings.ReplaceAll(command[i], "%", "%%") if strings.ContainsAny(command[i], " \t") { command[i] = strconv.Quote(command[i]) + } else if strings.Contains(command[i], `\`) { + // strconv.Quote also escapes backslashes so + // we should replace only if strconv.Quote was not used + command[i] = strings.ReplaceAll(command[i], `\`, `\\`) } } return command diff --git a/pkg/systemd/generate/common_test.go b/pkg/systemd/generate/common_test.go index d0ec5637c..a0691d1ad 100644 --- a/pkg/systemd/generate/common_test.go +++ b/pkg/systemd/generate/common_test.go @@ -29,7 +29,7 @@ func TestFilterPodFlags(t *testing.T) { } } -func TestQuoteArguments(t *testing.T) { +func TestEscapeSystemdArguments(t *testing.T) { tests := []struct { input []string output []string @@ -46,10 +46,46 @@ func TestQuoteArguments(t *testing.T) { []string{"foo", "bar=\"arg with\ttab\""}, []string{"foo", "\"bar=\\\"arg with\\ttab\\\"\""}, }, + { + []string{"$"}, + []string{"$$"}, + }, + { + []string{"foo", "command with dollar sign $"}, + []string{"foo", "\"command with dollar sign $$\""}, + }, + { + []string{"foo", "command with two dollar signs $$"}, + []string{"foo", "\"command with two dollar signs $$$$\""}, + }, + { + []string{"%"}, + []string{"%%"}, + }, + { + []string{"foo", "command with percent sign %"}, + []string{"foo", "\"command with percent sign %%\""}, + }, + { + []string{"foo", "command with two percent signs %%"}, + []string{"foo", "\"command with two percent signs %%%%\""}, + }, + { + []string{`\`}, + []string{`\\`}, + }, + { + []string{"foo", `command with backslash \`}, + []string{"foo", `"command with backslash \\"`}, + }, + { + []string{"foo", `command with two backslashs \\`}, + []string{"foo", `"command with two backslashs \\\\"`}, + }, } for _, test := range tests { - quoted := quoteArguments(test.input) + quoted := escapeSystemdArguments(test.input) assert.Equal(t, test.output, quoted) } } diff --git a/pkg/systemd/generate/containers.go b/pkg/systemd/generate/containers.go index 5f52b0a77..abe159812 100644 --- a/pkg/systemd/generate/containers.go +++ b/pkg/systemd/generate/containers.go @@ -204,7 +204,7 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst startCommand := []string{info.Executable} if index > 2 { // include root flags - info.RootFlags = strings.Join(quoteArguments(info.CreateCommand[1:index-1]), " ") + info.RootFlags = strings.Join(escapeSystemdArguments(info.CreateCommand[1:index-1]), " ") startCommand = append(startCommand, info.CreateCommand[1:index-1]...) } startCommand = append(startCommand, @@ -279,7 +279,7 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst } } startCommand = append(startCommand, remainingCmd...) - startCommand = quoteArguments(startCommand) + startCommand = escapeSystemdArguments(startCommand) info.ExecStartPre = "/bin/rm -f {{{{.PIDFile}}}} {{{{.ContainerIDFile}}}}" info.ExecStart = strings.Join(startCommand, " ") diff --git a/pkg/systemd/generate/containers_test.go b/pkg/systemd/generate/containers_test.go index 96d95644b..be14e4c28 100644 --- a/pkg/systemd/generate/containers_test.go +++ b/pkg/systemd/generate/containers_test.go @@ -352,6 +352,30 @@ Type=forking [Install] WantedBy=multi-user.target default.target ` + + goodNewWithSpecialChars := `# jadda-jadda.service +# autogenerated by Podman CI + +[Unit] +Description=Podman jadda-jadda.service +Documentation=man:podman-generate-systemd(1) +Wants=network.target +After=network-online.target + +[Service] +Environment=PODMAN_SYSTEMD_UNIT=%n +Restart=always +TimeoutStopSec=70 +ExecStartPre=/bin/rm -f %t/jadda-jadda.pid %t/jadda-jadda.ctr-id +ExecStart=/usr/bin/podman run --conmon-pidfile %t/jadda-jadda.pid --cidfile %t/jadda-jadda.ctr-id --cgroups=no-conmon -d --replace --name test awesome-image:latest sh -c "kill $$$$ && echo %%\\" +ExecStop=/usr/bin/podman stop --ignore --cidfile %t/jadda-jadda.ctr-id -t 10 +ExecStopPost=/usr/bin/podman rm --ignore -f --cidfile %t/jadda-jadda.ctr-id +PIDFile=%t/jadda-jadda.pid +Type=forking + +[Install] +WantedBy=multi-user.target default.target +` tests := []struct { name string info containerInfo @@ -647,6 +671,22 @@ WantedBy=multi-user.target default.target true, false, }, + {"good with special chars", + containerInfo{ + Executable: "/usr/bin/podman", + ServiceName: "jadda-jadda", + ContainerNameOrID: "jadda-jadda", + RestartPolicy: "always", + PIDFile: "/var/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid", + StopTimeout: 10, + PodmanVersion: "CI", + CreateCommand: []string{"I'll get stripped", "create", "--name", "test", "awesome-image:latest", "sh", "-c", "kill $$ && echo %\\"}, + EnvVariable: EnvVariable, + }, + goodNewWithSpecialChars, + true, + false, + }, } for _, tt := range tests { test := tt diff --git a/pkg/systemd/generate/pods.go b/pkg/systemd/generate/pods.go index c7e3aa955..d6ede19af 100644 --- a/pkg/systemd/generate/pods.go +++ b/pkg/systemd/generate/pods.go @@ -269,7 +269,7 @@ func executePodTemplate(info *podInfo, options entities.GenerateSystemdOptions) return "", errors.Errorf("pod does not appear to be created via `podman pod create`: %v", info.CreateCommand) } podRootArgs = info.CreateCommand[1 : podCreateIndex-1] - info.RootFlags = strings.Join(quoteArguments(podRootArgs), " ") + info.RootFlags = strings.Join(escapeSystemdArguments(podRootArgs), " ") podCreateArgs = filterPodFlags(info.CreateCommand[podCreateIndex+1:]) } // We're hard-coding the first five arguments and append the @@ -306,7 +306,7 @@ func executePodTemplate(info *podInfo, options entities.GenerateSystemdOptions) } startCommand = append(startCommand, podCreateArgs...) - startCommand = quoteArguments(startCommand) + startCommand = escapeSystemdArguments(startCommand) info.ExecStartPre1 = "/bin/rm -f {{{{.PIDFile}}}} {{{{.PodIDFile}}}}" info.ExecStartPre2 = strings.Join(startCommand, " ") |