1
0
Fork 0

refactor: move distro id to separate file

timestamps
dump_stack() 2023-05-18 20:02:09 +00:00
parent 73f5df2425
commit c3774714fd
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
2 changed files with 63 additions and 62 deletions

View File

@ -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

63
distro/id.go Normal file
View File

@ -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
}