Skip to content

Commit

Permalink
chore(alpm): depend interface (#60)
Browse files Browse the repository at this point in the history
* add interface for dep list

* fix tests

* fix golangci and goimports

* pin golangci version up
  • Loading branch information
Jguer authored Mar 6, 2023
1 parent 44fb031 commit 134dbc7
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 64 deletions.
15 changes: 6 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ linters-settings:
gocyclo:
min-complexity: 15
goimports:
local-prefixes: github.com/Jguer/yay/v10
local-prefixes: github.com/Jguer/go-alpm/v2
gomnd:
settings:
mnd:
Expand All @@ -40,10 +40,12 @@ linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
- errorlint
- exportloopref
- gochecknoinits
- gocritic
- gofmt
Expand All @@ -56,20 +58,15 @@ linters:
- lll
- misspell
- nakedret
- prealloc
- revive
- rowserrcheck
- noctx
- nolintlint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
- wsl
- godot

run:

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ WORKDIR /app
COPY go.mod .

RUN pacman -Syu --overwrite=* --needed --noconfirm go && \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.43.0 && \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.51.2 && \
go mod download && \
rm -rfv /var/cache/pacman/* /var/lib/pacman/sync/*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export GO111MODULE=on
GO ?= go

SOURCES ?= $(shell find . -name "*.go")
GOFLAGS += $(shell pacman -T 'pacman-git' && echo "-tags next")
GOFLAGS += $(shell pacman -T 'pacman-git' > /dev/null && echo "-tags next")

.PHONY: test
test:
Expand Down
3 changes: 2 additions & 1 deletion alpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ package alpm_test
import (
"testing"

alpm "github.com/Jguer/go-alpm/v2"
"github.com/stretchr/testify/assert"

alpm "github.com/Jguer/go-alpm/v2"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type (
)

var (
logCallbackContextPool callbackContextPool = callbackContextPool{}
questionCallbackContextPool callbackContextPool = callbackContextPool{}
logCallbackContextPool = callbackContextPool{}
questionCallbackContextPool = callbackContextPool{}
)

func DefaultLogCallback(ctx interface{}, lvl LogLevel, s string) {
Expand Down
42 changes: 22 additions & 20 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package alpm
package alpm_test

import (
"testing"

"github.com/Morganamilo/go-pacmanconf"
"github.com/stretchr/testify/assert"

alpm "github.com/Jguer/go-alpm/v2"
)

type AlpmExecutor struct {
handle *Handle
localDB IDB
syncDB IDBList
syncDBsCache []IDB
type alpmExecutor struct {
handle *alpm.Handle
localDB alpm.IDB
syncDB alpm.IDBList
syncDBsCache []alpm.IDB
conf *pacmanconf.Config
}

func (ae *AlpmExecutor) RefreshHandle() error {
func (ae *alpmExecutor) RefreshHandle() error {
if ae.handle != nil {
if errRelease := ae.handle.Release(); errRelease != nil {
return errRelease
}
}

alpmHandle, err := Initialize(ae.conf.RootDir, ae.conf.DBPath)
alpmHandle, err := alpm.Initialize(ae.conf.RootDir, ae.conf.DBPath)
if err != nil {
return err
}
Expand All @@ -44,32 +46,32 @@ func (ae *AlpmExecutor) RefreshHandle() error {
return err
}

func toUsage(usages []string) Usage {
func toUsage(usages []string) alpm.Usage {
if len(usages) == 0 {
return UsageAll
return alpm.UsageAll
}

var ret Usage
var ret alpm.Usage

for _, usage := range usages {
switch usage {
case "Sync":
ret |= UsageSync
ret |= alpm.UsageSync
case "Search":
ret |= UsageSearch
ret |= alpm.UsageSearch
case "Install":
ret |= UsageInstall
ret |= alpm.UsageInstall
case "Upgrade":
ret |= UsageUpgrade
ret |= alpm.UsageUpgrade
case "All":
ret |= UsageAll
ret |= alpm.UsageAll
}
}

return ret
}

func configureAlpm(pacmanConf *pacmanconf.Config, alpmHandle *Handle) error {
func configureAlpm(pacmanConf *pacmanconf.Config, alpmHandle *alpm.Handle) error {
for _, repo := range pacmanConf.Repos {
// TODO: set SigLevel
alpmDB, err := alpmHandle.RegisterSyncDB(repo.Name, 0)
Expand Down Expand Up @@ -127,8 +129,8 @@ func configureAlpm(pacmanConf *pacmanconf.Config, alpmHandle *Handle) error {
return alpmHandle.SetCheckSpace(pacmanConf.CheckSpace)
}

func NewExecutor(pacmanConf *pacmanconf.Config) (*AlpmExecutor, error) {
ae := &AlpmExecutor{conf: pacmanConf}
func NewExecutor(pacmanConf *pacmanconf.Config) (*alpmExecutor, error) {
ae := &alpmExecutor{conf: pacmanConf}

err := ae.RefreshHandle()
if err != nil {
Expand Down Expand Up @@ -241,7 +243,7 @@ func TestAlpmExecutor(t *testing.T) {
assert.Equal(t, true, check)
}

func alpmTestGetArch(h *Handle) ([]string, error) {
func alpmTestGetArch(h *alpm.Handle) ([]string, error) {
architectures, err := h.GetArchitectures()

return architectures.Slice(), err
Expand Down
6 changes: 3 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (l DBList) ForEach(f func(IDB) error) error {
})
}

// Slice converst DB list to DB slice.
// Slice converts DB list to DB slice.
func (l DBList) Slice() []IDB {
slice := []IDB{}
_ = l.ForEach(func(db IDB) error {
Expand Down Expand Up @@ -165,8 +165,8 @@ func (db *DB) PkgCache() IPackageList {
// In case of error the Package List will be nil
func (db *DB) Search(targets []string) IPackageList {
var (
needles *C.alpm_list_t = nil
ret *C.alpm_list_t = nil
needles *C.alpm_list_t
ret *C.alpm_list_t
)

for _, str := range targets {
Expand Down
2 changes: 1 addition & 1 deletion handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (h *Handle) RemoveIgnoreGroup(dir string) (bool, error) {
}*/

// use alpm_depend_t
func (h *Handle) AssumeInstalled() (DependList, error) {
func (h *Handle) AssumeInstalled() (IDependList, error) {
alpmList := C.alpm_option_get_assumeinstalled(h.ptr)
depList := DependList{(*list)(unsafe.Pointer(alpmList))}

Expand Down
21 changes: 14 additions & 7 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ type IPackage interface {
// BuildDate returns the BuildDate of the package.
BuildDate() time.Time
// Conflicts returns the conflicts of the package as a DependList.
Conflicts() DependList
Conflicts() IDependList
// DB returns the package's origin database.
DB() IDB
// Depends returns the package's dependency list.
Depends() DependList
Depends() IDependList
// Depends returns the package's optional dependency list.
OptionalDepends() DependList
OptionalDepends() IDependList
// Depends returns the package's check dependency list.
CheckDepends() DependList
CheckDepends() IDependList
// Depends returns the package's make dependency list.
MakeDepends() DependList
MakeDepends() IDependList
// Description returns the package's description.
Description() string
// Files returns the file list of the package.
Expand All @@ -51,13 +51,13 @@ type IPackage interface {
// Packager returns package packager name.
Packager() string
// Provides returns DependList of packages provides by package.
Provides() DependList
Provides() IDependList
// Reason returns package install reason.
Reason() PkgReason
// Origin returns package origin.
Origin() PkgFrom
// Replaces returns a DependList with the packages this package replaces.
Replaces() DependList
Replaces() IDependList
// Size returns the packed package size.
Size() int64
// URL returns the upstream URL of the package.
Expand Down Expand Up @@ -90,6 +90,13 @@ type IPackageList interface {
FindSatisfier(string) (IPackage, error)
}

type IDependList interface {
// ForEach executes an action on each package of the DependList.
ForEach(func(*Depend) error) error
// Slice converts the DependList to a Depend Slice.
Slice() []Depend
}

// IDB is an interface type for alpm.DB.
type IDB interface {
Unregister() error
Expand Down
20 changes: 10 additions & 10 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (l PackageList) Slice() []IPackage {
type DependList struct{ *list }

// ForEach executes an action on each package of the DependList.
func (l DependList) ForEach(f func(Depend) error) error {
func (l DependList) ForEach(f func(*Depend) error) error {
return l.forEach(func(p unsafe.Pointer) error {
dep := convertDepend((*C.alpm_depend_t)(p))
return f(dep)
Expand All @@ -60,8 +60,8 @@ func (l DependList) ForEach(f func(Depend) error) error {
// Slice converts the DependList to a Depend Slice.
func (l DependList) Slice() []Depend {
slice := []Depend{}
_ = l.ForEach(func(dep Depend) error {
slice = append(slice, dep)
_ = l.ForEach(func(dep *Depend) error {
slice = append(slice, *dep)
return nil
})

Expand Down Expand Up @@ -102,7 +102,7 @@ func (pkg *Package) BuildDate() time.Time {
}

// Conflicts returns the conflicts of the package as a DependList.
func (pkg *Package) Conflicts() DependList {
func (pkg *Package) Conflicts() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_conflicts(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
Expand All @@ -118,25 +118,25 @@ func (pkg *Package) DB() IDB {
}

// Depends returns the package's dependency list.
func (pkg *Package) Depends() DependList {
func (pkg *Package) Depends() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_depends(pkg.pmpkg))
return DependList{(*list)(ptr)}
}

// Depends returns the package's optional dependency list.
func (pkg *Package) OptionalDepends() DependList {
func (pkg *Package) OptionalDepends() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_optdepends(pkg.pmpkg))
return DependList{(*list)(ptr)}
}

// Depends returns the package's check dependency list.
func (pkg *Package) CheckDepends() DependList {
func (pkg *Package) CheckDepends() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_checkdepends(pkg.pmpkg))
return DependList{(*list)(ptr)}
}

// Depends returns the package's make dependency list.
func (pkg *Package) MakeDepends() DependList {
func (pkg *Package) MakeDepends() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_makedepends(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func (pkg *Package) Packager() string {
}

// Provides returns DependList of packages provides by package.
func (pkg *Package) Provides() DependList {
func (pkg *Package) Provides() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_provides(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
Expand All @@ -220,7 +220,7 @@ func (pkg *Package) Origin() PkgFrom {
}

// Replaces returns a DependList with the packages this package replaces.
func (pkg *Package) Replaces() DependList {
func (pkg *Package) Replaces() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_replaces(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
Expand Down
2 changes: 2 additions & 0 deletions testdata/examples/updates/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"log"

paconf "github.com/Morganamilo/go-pacmanconf"

"github.com/Jguer/go-alpm/v2"
)

func human(size int64) string {
Expand Down
Loading

0 comments on commit 134dbc7

Please sign in to comment.