From f2ce20e53b18a48e6e90c60278646aefdec2ae72 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Tue, 23 May 2023 16:24:01 +0000 Subject: [PATCH] feat: change interface from ID()/Release() to Distro() with both --- distro/centos/centos.go | 12 ++++-------- distro/centos/centos_test.go | 2 -- distro/debian/debian.go | 12 ++++-------- distro/debian/debian_test.go | 2 -- distro/distro.go | 14 +++----------- distro/oraclelinux/oraclelinux.go | 12 ++++-------- distro/oraclelinux/oraclelinux_test.go | 2 -- distro/ubuntu/ubuntu.go | 12 ++++-------- distro/ubuntu/ubuntu_test.go | 3 --- 9 files changed, 19 insertions(+), 52 deletions(-) diff --git a/distro/centos/centos.go b/distro/centos/centos.go index 10fe469..ae98e3e 100644 --- a/distro/centos/centos.go +++ b/distro/centos/centos.go @@ -29,18 +29,14 @@ type CentOS struct { container string } -func (centos CentOS) ID() distro.ID { - return distro.CentOS -} - -func (centos CentOS) Release() string { - return centos.release -} - func (centos CentOS) Equal(d distro.Distro) bool { return centos.release == d.Release && distro.CentOS == d.ID } +func (centos CentOS) Distro() distro.Distro { + return distro.Distro{ID: distro.CentOS, Release: centos.release} +} + func (centos CentOS) Packages() (pkgs []string, err error) { c, err := container.New(centos.container) if err != nil { diff --git a/distro/centos/centos_test.go b/distro/centos/centos_test.go index 5875093..75926f6 100644 --- a/distro/centos/centos_test.go +++ b/distro/centos/centos_test.go @@ -13,8 +13,6 @@ func TestCentOS(t *testing.T) { u := CentOS{release: "7", container: "out_of_tree_centos_7"} - assert.Equal(u.ID(), distro.CentOS) - assert.Equal(u.Release(), "7") assert.True(u.Equal(distro.Distro{Release: "7", ID: distro.CentOS})) diff --git a/distro/debian/debian.go b/distro/debian/debian.go index 22efc03..d058be5 100644 --- a/distro/debian/debian.go +++ b/distro/debian/debian.go @@ -43,14 +43,6 @@ type Debian struct { container string } -func (d Debian) ID() distro.ID { - return distro.Debian -} - -func (d Debian) Release() string { - return d.release.String() -} - func (d Debian) Equal(dd distro.Distro) bool { if dd.ID != distro.Debian { return false @@ -59,6 +51,10 @@ func (d Debian) Equal(dd distro.Distro) bool { return ReleaseFromString(dd.Release) == d.release } +func (d Debian) Distro() distro.Distro { + return distro.Distro{distro.Debian, d.release.String()} +} + func (d Debian) Packages() (packages []string, err error) { c, err := container.New(d.container) if err != nil { diff --git a/distro/debian/debian_test.go b/distro/debian/debian_test.go index 5a15dca..b8b58f2 100644 --- a/distro/debian/debian_test.go +++ b/distro/debian/debian_test.go @@ -48,8 +48,6 @@ func TestDebian(t *testing.T) { u := Debian{release: Wheezy, container: "out_of_tree_debian_7"} - assert.Equal(u.ID(), distro.Debian) - assert.Equal(u.Release(), "wheezy") assert.True(u.Equal(distro.Distro{Release: "wheezy", ID: distro.Debian})) diff --git a/distro/distro.go b/distro/distro.go index 2d31c25..45d8d23 100644 --- a/distro/distro.go +++ b/distro/distro.go @@ -8,8 +8,7 @@ var mu sync.Mutex var distros []distribution type distribution interface { - ID() ID - Release() string + Distro() Distro Equal(Distro) bool Packages() (packages []string, err error) } @@ -22,16 +21,9 @@ func Register(d distribution) { } func List() (dds []Distro) { - mu.Lock() - defer mu.Unlock() - for _, dd := range distros { - dds = append(dds, Distro{ - ID: dd.ID(), - Release: dd.Release(), - }) + dds = append(dds, dd.Distro()) } - return } @@ -42,7 +34,7 @@ type Distro struct { func (d Distro) Packages() (packages []string, err error) { for _, dd := range distros { - if d.ID != None && d.ID != dd.ID() { + if d.ID != None && d.ID != dd.Distro().ID { continue } diff --git a/distro/oraclelinux/oraclelinux.go b/distro/oraclelinux/oraclelinux.go index 6f31e41..f2cd857 100644 --- a/distro/oraclelinux/oraclelinux.go +++ b/distro/oraclelinux/oraclelinux.go @@ -29,18 +29,14 @@ type OracleLinux struct { container string } -func (ol OracleLinux) ID() distro.ID { - return distro.OracleLinux -} - -func (ol OracleLinux) Release() string { - return ol.release -} - func (ol OracleLinux) Equal(d distro.Distro) bool { return ol.release == d.Release && distro.OracleLinux == d.ID } +func (ol OracleLinux) Distro() distro.Distro { + return distro.Distro{ID: distro.OracleLinux, Release: ol.release} +} + func (ol OracleLinux) Packages() (pkgs []string, err error) { c, err := container.New(ol.container) if err != nil { diff --git a/distro/oraclelinux/oraclelinux_test.go b/distro/oraclelinux/oraclelinux_test.go index 7f8ac35..5dce273 100644 --- a/distro/oraclelinux/oraclelinux_test.go +++ b/distro/oraclelinux/oraclelinux_test.go @@ -13,8 +13,6 @@ func TestOracleLinux(t *testing.T) { u := OracleLinux{release: "9", container: "out_of_tree_oraclelinux_9"} - assert.Equal(u.ID(), distro.OracleLinux) - assert.Equal(u.Release(), "9") assert.True(u.Equal(distro.Distro{Release: "9", ID: distro.OracleLinux})) diff --git a/distro/ubuntu/ubuntu.go b/distro/ubuntu/ubuntu.go index c5dc089..690232a 100644 --- a/distro/ubuntu/ubuntu.go +++ b/distro/ubuntu/ubuntu.go @@ -35,18 +35,14 @@ type Ubuntu struct { container string } -func (u Ubuntu) ID() distro.ID { - return distro.Ubuntu -} - -func (u Ubuntu) Release() string { - return u.release -} - func (u Ubuntu) Equal(d distro.Distro) bool { return u.release == d.Release && distro.Ubuntu == d.ID } +func (u Ubuntu) Distro() distro.Distro { + return distro.Distro{ID: distro.Ubuntu, Release: u.release} +} + func (u Ubuntu) Packages() (pkgs []string, err error) { c, err := container.New(u.container) if err != nil { diff --git a/distro/ubuntu/ubuntu_test.go b/distro/ubuntu/ubuntu_test.go index a0ad020..24f74b2 100644 --- a/distro/ubuntu/ubuntu_test.go +++ b/distro/ubuntu/ubuntu_test.go @@ -13,9 +13,6 @@ func TestUbuntu(t *testing.T) { u := Ubuntu{release: "22.04", container: "out_of_tree_ubuntu_22__04"} - assert.Equal(u.ID(), distro.Ubuntu) - assert.Equal(u.Release(), "22.04") - assert.True(u.Equal(distro.Distro{Release: "22.04", ID: distro.Ubuntu})) assert.NotEmpty(u.Packages())