From c3774714fd206a845091071f5b9de5ec699dfd5b Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Thu, 18 May 2023 20:02:09 +0000 Subject: [PATCH] refactor: move distro id to separate file --- distro/distro.go | 62 ----------------------------------------------- distro/id.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 62 deletions(-) create mode 100644 distro/id.go diff --git a/distro/distro.go b/distro/distro.go index fc489c6..7ffb52e 100644 --- a/distro/distro.go +++ b/distro/distro.go @@ -1,67 +1,5 @@ package distro -import ( - "fmt" - "strings" -) - -// ID of the distro -type ID int - -const ( - // Ubuntu https://ubuntu.com/ - Ubuntu ID = iota - // CentOS https://www.centos.org/ - CentOS - // Debian https://www.debian.org/ - Debian - // OracleLinux https://www.oracle.com/linux/ - OracleLinux -) - -var IDs = []ID{ - Ubuntu, CentOS, Debian, OracleLinux, -} - -var nameStrings = [...]string{ - "Ubuntu", - "CentOS", - "Debian", - "OracleLinux", -} - -func NewID(name string) (id ID, err error) { - err = id.UnmarshalTOML([]byte(name)) - return -} - -func (id ID) String() string { - return nameStrings[id] -} - -// UnmarshalTOML is for support github.com/naoina/toml -func (id *ID) UnmarshalTOML(data []byte) (err error) { - name := strings.Trim(string(data), `"`) - if strings.EqualFold(name, "Ubuntu") { - *id = Ubuntu - } else if strings.EqualFold(name, "CentOS") { - *id = CentOS - } else if strings.EqualFold(name, "Debian") { - *id = Debian - } else if strings.EqualFold(name, "OracleLinux") { - *id = OracleLinux - } else { - err = fmt.Errorf("distro %s is unsupported", name) - } - return -} - -// MarshalTOML is for support github.com/naoina/toml -func (id ID) MarshalTOML() (data []byte, err error) { - data = []byte(`"` + id.String() + `"`) - return -} - type Distro struct { ID ID Release string diff --git a/distro/id.go b/distro/id.go new file mode 100644 index 0000000..a91c03a --- /dev/null +++ b/distro/id.go @@ -0,0 +1,63 @@ +package distro + +import ( + "fmt" + "strings" +) + +// ID of the distro +type ID int + +const ( + // Ubuntu https://ubuntu.com/ + Ubuntu ID = iota + // CentOS https://www.centos.org/ + CentOS + // Debian https://www.debian.org/ + Debian + // OracleLinux https://www.oracle.com/linux/ + OracleLinux +) + +var IDs = []ID{ + Ubuntu, CentOS, Debian, OracleLinux, +} + +var nameStrings = [...]string{ + "Ubuntu", + "CentOS", + "Debian", + "OracleLinux", +} + +func NewID(name string) (id ID, err error) { + err = id.UnmarshalTOML([]byte(name)) + return +} + +func (id ID) String() string { + return nameStrings[id] +} + +// UnmarshalTOML is for support github.com/naoina/toml +func (id *ID) UnmarshalTOML(data []byte) (err error) { + name := strings.Trim(string(data), `"`) + if strings.EqualFold(name, "Ubuntu") { + *id = Ubuntu + } else if strings.EqualFold(name, "CentOS") { + *id = CentOS + } else if strings.EqualFold(name, "Debian") { + *id = Debian + } else if strings.EqualFold(name, "OracleLinux") { + *id = OracleLinux + } else { + err = fmt.Errorf("distro %s is unsupported", name) + } + return +} + +// MarshalTOML is for support github.com/naoina/toml +func (id ID) MarshalTOML() (data []byte, err error) { + data = []byte(`"` + id.String() + `"`) + return +}