From 887aab1d72914c674dff8c427a4e9d14442fab8b Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Wed, 17 Jun 2020 11:00:55 +0200 Subject: [PATCH] util: add MountOptionsAdd() to add mount options Signed-off-by: Niels de Vos --- internal/util/util.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/internal/util/util.go b/internal/util/util.go index b8e1b387a..a4c6f05a1 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -205,3 +205,35 @@ func Mount(source, target, fstype string, options []string) error { dummyMount := mount.New("") return dummyMount.Mount(source, target, fstype, options) } + +// MountOptionsAdd adds the `add` mount options to the `options` and returns a +// new string. In case `add` is already present in the `options`, `add` is not +// added again. +func MountOptionsAdd(options string, add ...string) string { + opts := strings.Split(options, ",") + newOpts := []string{} + // clean original options from empty strings + for _, opt := range opts { + if opt != "" { + newOpts = append(newOpts, opt) + } + } + + for _, opt := range add { + if opt != "" && !contains(newOpts, opt) { + newOpts = append(newOpts, opt) + } + } + + return strings.Join(newOpts, ",") +} + +func contains(s []string, key string) bool { + for _, v := range s { + if v == key { + return true + } + } + + return false +}