From f0c82f928943aea32283944d6e57c34e4b20bba6 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Thu, 18 May 2023 22:02:41 +0000 Subject: [PATCH] feat: implement list of available distros --- distro.go | 14 +++++++++++++- distro/centos/centos.go | 4 ++++ distro/debian/debian.go | 4 ++++ distro/distro.go | 15 +++++++++++++++ distro/oraclelinux/oraclelinux.go | 4 ++++ distro/ubuntu/ubuntu.go | 4 ++++ main.go | 2 +- 7 files changed, 45 insertions(+), 2 deletions(-) diff --git a/distro.go b/distro.go index b340b21..48e6a0a 100644 --- a/distro.go +++ b/distro.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "regexp" + "strings" "time" "github.com/cavaliergopher/grab/v3" @@ -21,7 +22,9 @@ import ( ) type DistroCmd struct { - Debian DebianCmd `cmd:""` + List DistroListCmd `cmd:"" help:"list available distros"` + + Debian DebianCmd `cmd:"" hidden:""` } type DebianCmd struct { @@ -183,3 +186,12 @@ func (cmd *DebianFetchCmd) Run(dcmd *DebianCmd) (err error) { } return } + +type DistroListCmd struct{} + +func (cmd *DistroListCmd) Run() (err error) { + for _, d := range distro.List() { + fmt.Println(d.ID, strings.Title(d.Release)) + } + return +} diff --git a/distro/centos/centos.go b/distro/centos/centos.go index f6a84d8..cb256a0 100644 --- a/distro/centos/centos.go +++ b/distro/centos/centos.go @@ -34,6 +34,10 @@ 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 } diff --git a/distro/debian/debian.go b/distro/debian/debian.go index 113ba06..126a633 100644 --- a/distro/debian/debian.go +++ b/distro/debian/debian.go @@ -47,6 +47,10 @@ 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 diff --git a/distro/distro.go b/distro/distro.go index 218836c..2d31c25 100644 --- a/distro/distro.go +++ b/distro/distro.go @@ -9,6 +9,7 @@ var distros []distribution type distribution interface { ID() ID + Release() string Equal(Distro) bool Packages() (packages []string, err error) } @@ -20,6 +21,20 @@ func Register(d distribution) { distros = append(distros, d) } +func List() (dds []Distro) { + mu.Lock() + defer mu.Unlock() + + for _, dd := range distros { + dds = append(dds, Distro{ + ID: dd.ID(), + Release: dd.Release(), + }) + } + + return +} + type Distro struct { ID ID Release string diff --git a/distro/oraclelinux/oraclelinux.go b/distro/oraclelinux/oraclelinux.go index 6678165..d3c17d4 100644 --- a/distro/oraclelinux/oraclelinux.go +++ b/distro/oraclelinux/oraclelinux.go @@ -34,6 +34,10 @@ 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 } diff --git a/distro/ubuntu/ubuntu.go b/distro/ubuntu/ubuntu.go index 8f635b9..f41bc8c 100644 --- a/distro/ubuntu/ubuntu.go +++ b/distro/ubuntu/ubuntu.go @@ -40,6 +40,10 @@ 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 } diff --git a/main.go b/main.go index bc7b5e0..d05cbef 100644 --- a/main.go +++ b/main.go @@ -53,7 +53,7 @@ type CLI struct { Gen GenCmd `cmd:"" help:"generate .out-of-tree.toml skeleton"` Image ImageCmd `cmd:"" help:"manage images"` Container ContainerCmd `cmd:"" help:"manage containers"` - Distro DistroCmd `cmd:"" help:"distro-related helpers" hidden:""` + Distro DistroCmd `cmd:"" help:"distro-related helpers"` Version VersionFlag `name:"version" help:"print version information and quit"`