From eb0531b89b6ae63b903555bca94caa434b2e389c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Fri, 3 Feb 2023 13:42:26 +0100 Subject: [PATCH] blueprint: move `FilesystemCustomization` code to separate file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move the `FilesystemCustomization` structure and its custom unmarshallers to a dedicated file. This makes `customizations.go` easier to read. Signed-off-by: Tomáš Hozza --- internal/blueprint/customizations.go | 66 ----------------- .../blueprint/filesystem_customizations.go | 71 +++++++++++++++++++ 2 files changed, 71 insertions(+), 66 deletions(-) create mode 100644 internal/blueprint/filesystem_customizations.go diff --git a/internal/blueprint/customizations.go b/internal/blueprint/customizations.go index c1ae0843d..89221f401 100644 --- a/internal/blueprint/customizations.go +++ b/internal/blueprint/customizations.go @@ -1,12 +1,9 @@ package blueprint import ( - "encoding/json" "fmt" "reflect" "strings" - - "github.com/osbuild/osbuild-composer/internal/common" ) type Customizations struct { @@ -106,74 +103,11 @@ type ServicesCustomization struct { Disabled []string `json:"disabled,omitempty" toml:"disabled,omitempty"` } -type FilesystemCustomization struct { - Mountpoint string `json:"mountpoint,omitempty" toml:"mountpoint,omitempty"` - MinSize uint64 `json:"minsize,omitempty" toml:"size,omitempty"` -} - type OpenSCAPCustomization struct { DataStream string `json:"datastream,omitempty" toml:"datastream,omitempty"` ProfileID string `json:"profile_id,omitempty" toml:"profile_id,omitempty"` } -func (fsc *FilesystemCustomization) UnmarshalTOML(data interface{}) error { - d, _ := data.(map[string]interface{}) - - switch d["mountpoint"].(type) { - case string: - fsc.Mountpoint = d["mountpoint"].(string) - default: - return fmt.Errorf("TOML unmarshal: mountpoint must be string, got %v of type %T", d["mountpoint"], d["mountpoint"]) - } - - switch d["size"].(type) { - case int64: - fsc.MinSize = uint64(d["size"].(int64)) - case string: - size, err := common.DataSizeToUint64(d["size"].(string)) - if err != nil { - return fmt.Errorf("TOML unmarshal: size is not valid filesystem size (%w)", err) - } - fsc.MinSize = size - default: - return fmt.Errorf("TOML unmarshal: size must be integer or string, got %v of type %T", d["size"], d["size"]) - } - - return nil -} - -func (fsc *FilesystemCustomization) UnmarshalJSON(data []byte) error { - var v interface{} - if err := json.Unmarshal(data, &v); err != nil { - return err - } - d, _ := v.(map[string]interface{}) - - switch d["mountpoint"].(type) { - case string: - fsc.Mountpoint = d["mountpoint"].(string) - default: - return fmt.Errorf("JSON unmarshal: mountpoint must be string, got %v of type %T", d["mountpoint"], d["mountpoint"]) - } - - // The JSON specification only mentions float64 and Go defaults to it: https://go.dev/blog/json - switch d["minsize"].(type) { - case float64: - // Note that it uses different key than the TOML version - fsc.MinSize = uint64(d["minsize"].(float64)) - case string: - size, err := common.DataSizeToUint64(d["minsize"].(string)) - if err != nil { - return fmt.Errorf("JSON unmarshal: size is not valid filesystem size (%w)", err) - } - fsc.MinSize = size - default: - return fmt.Errorf("JSON unmarshal: minsize must be float64 number or string, got %v of type %T", d["minsize"], d["minsize"]) - } - - return nil -} - type CustomizationError struct { Message string } diff --git a/internal/blueprint/filesystem_customizations.go b/internal/blueprint/filesystem_customizations.go new file mode 100644 index 000000000..02bc36439 --- /dev/null +++ b/internal/blueprint/filesystem_customizations.go @@ -0,0 +1,71 @@ +package blueprint + +import ( + "encoding/json" + "fmt" + + "github.com/osbuild/osbuild-composer/internal/common" +) + +type FilesystemCustomization struct { + Mountpoint string `json:"mountpoint,omitempty" toml:"mountpoint,omitempty"` + MinSize uint64 `json:"minsize,omitempty" toml:"size,omitempty"` +} + +func (fsc *FilesystemCustomization) UnmarshalTOML(data interface{}) error { + d, _ := data.(map[string]interface{}) + + switch d["mountpoint"].(type) { + case string: + fsc.Mountpoint = d["mountpoint"].(string) + default: + return fmt.Errorf("TOML unmarshal: mountpoint must be string, got %v of type %T", d["mountpoint"], d["mountpoint"]) + } + + switch d["size"].(type) { + case int64: + fsc.MinSize = uint64(d["size"].(int64)) + case string: + size, err := common.DataSizeToUint64(d["size"].(string)) + if err != nil { + return fmt.Errorf("TOML unmarshal: size is not valid filesystem size (%w)", err) + } + fsc.MinSize = size + default: + return fmt.Errorf("TOML unmarshal: size must be integer or string, got %v of type %T", d["size"], d["size"]) + } + + return nil +} + +func (fsc *FilesystemCustomization) UnmarshalJSON(data []byte) error { + var v interface{} + if err := json.Unmarshal(data, &v); err != nil { + return err + } + d, _ := v.(map[string]interface{}) + + switch d["mountpoint"].(type) { + case string: + fsc.Mountpoint = d["mountpoint"].(string) + default: + return fmt.Errorf("JSON unmarshal: mountpoint must be string, got %v of type %T", d["mountpoint"], d["mountpoint"]) + } + + // The JSON specification only mentions float64 and Go defaults to it: https://go.dev/blog/json + switch d["minsize"].(type) { + case float64: + // Note that it uses different key than the TOML version + fsc.MinSize = uint64(d["minsize"].(float64)) + case string: + size, err := common.DataSizeToUint64(d["minsize"].(string)) + if err != nil { + return fmt.Errorf("JSON unmarshal: size is not valid filesystem size (%w)", err) + } + fsc.MinSize = size + default: + return fmt.Errorf("JSON unmarshal: minsize must be float64 number or string, got %v of type %T", d["minsize"], d["minsize"]) + } + + return nil +}