Maint: alpine package interface rework

- return concrete struct type
- split interface is sub-interfaces
This commit is contained in:
Quentin McGaw (desktop)
2021-07-23 18:51:51 +00:00
parent c5d92ae02c
commit 0b985e8c35
3 changed files with 22 additions and 13 deletions

View File

@@ -2,24 +2,25 @@
package alpine
import (
"context"
"os/user"
)
type Configurator interface {
CreateUser(username string, uid int) (createdUsername string, err error)
Version(ctx context.Context) (version string, err error)
var _ Alpiner = (*Alpine)(nil)
type Alpiner interface {
UserCreater
VersionGetter
}
type configurator struct {
type Alpine struct {
alpineReleasePath string
passwdPath string
lookupID func(uid string) (*user.User, error)
lookup func(username string) (*user.User, error)
}
func NewConfigurator() Configurator {
return &configurator{
func New() *Alpine {
return &Alpine{
alpineReleasePath: "/etc/alpine-release",
passwdPath: "/etc/passwd",
lookupID: user.LookupId,

View File

@@ -12,10 +12,14 @@ var (
ErrUserAlreadyExists = errors.New("user already exists")
)
type UserCreater interface {
CreateUser(username string, uid int) (createdUsername string, err error)
}
// CreateUser creates a user in Alpine with the given UID.
func (c *configurator) CreateUser(username string, uid int) (createdUsername string, err error) {
func (a *Alpine) CreateUser(username string, uid int) (createdUsername string, err error) {
UIDStr := strconv.Itoa(uid)
u, err := c.lookupID(UIDStr)
u, err := a.lookupID(UIDStr)
_, unknownUID := err.(user.UnknownUserIdError)
if err != nil && !unknownUID {
return "", err
@@ -28,7 +32,7 @@ func (c *configurator) CreateUser(username string, uid int) (createdUsername str
return u.Username, nil
}
u, err = c.lookup(username)
u, err = a.lookup(username)
_, unknownUsername := err.(user.UnknownUserError)
if err != nil && !unknownUsername {
return "", err
@@ -39,7 +43,7 @@ func (c *configurator) CreateUser(username string, uid int) (createdUsername str
ErrUserAlreadyExists, username, u.Uid, uid)
}
file, err := os.OpenFile(c.passwdPath, os.O_APPEND|os.O_WRONLY, 0644)
file, err := os.OpenFile(a.passwdPath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return "", err
}

View File

@@ -7,8 +7,12 @@ import (
"strings"
)
func (c *configurator) Version(ctx context.Context) (version string, err error) {
file, err := os.OpenFile(c.alpineReleasePath, os.O_RDONLY, 0)
type VersionGetter interface {
Version(ctx context.Context) (version string, err error)
}
func (a *Alpine) Version(ctx context.Context) (version string, err error) {
file, err := os.OpenFile(a.alpineReleasePath, os.O_RDONLY, 0)
if err != nil {
return "", err
}