Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dependency resolver #50

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions cmd/cardano-up/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,11 @@ func installCommand() *cobra.Command {
)
os.Exit(1)
}
packages := pm.AvailablePackages()
foundPackage := false
for _, tmpPackage := range packages {
if tmpPackage.Name == args[0] {
foundPackage = true
if err := pm.Install(tmpPackage); err != nil {
slog.Error(err.Error())
os.Exit(1)
}
break
}
}
if !foundPackage {
slog.Error(fmt.Sprintf("no such package: %s", args[0]))
// Install requested package
if err := pm.Install(args[0]); err != nil {
slog.Error(err.Error())
os.Exit(1)
}
slog.Info(fmt.Sprintf("Successfully installed package %s", args[0]))
},
}
installCmd.Flags().StringVarP(&installFlags.network, "network", "n", "", "specifies network for package")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/docker/docker v25.0.3+incompatible
github.com/docker/go-connections v0.5.0
github.com/hashicorp/go-version v1.6.0
github.com/spf13/cobra v1.8.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 h1:Wqo399gCIufwto+VfwCSvsnfGpF/w5E9CNxSwbpD6No=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0/go.mod h1:qmOFXW2epJhM0qSnUUYpldc7gVz2KMQwJ/QYCDIa7XU=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down
28 changes: 27 additions & 1 deletion pkgmgr/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package pkgmgr

import "errors"
import (
"errors"
"fmt"
)

// ErrOperationFailed is a placeholder error for operations that directly log errors.
// It's used to signify when an operation has failed when the actual error message is
Expand All @@ -37,3 +40,26 @@ var ErrContextAlreadyExists = errors.New("specified context already exists")

// ErrContainerAlreadyExists is returned when creating a new container with a name that is already in use
var ErrContainerAlreadyExists = errors.New("specified container already exists")

func NewResolverPackageAlreadyInstalledError(pkgName string) error {
return fmt.Errorf(
"the package %q is already installed in the current context\n\nYou can use 'cardano-up context create' to create an empty context to install another instance of the package",
pkgName,
)
}

func NewResolverNoAvailablePackageDependencyError(depSpec string) error {
return fmt.Errorf(
"no available package found for dependency: %s",
depSpec,
)
}

func NewResolverInstalledPackageNoMatchVersionSpecError(pkgName string, pkgVersion string, depSpec string) error {
return fmt.Errorf(
"installed package \"%s = %s\" does not match dependency: %s",
pkgName,
pkgVersion,
depSpec,
)
}
1 change: 1 addition & 0 deletions pkgmgr/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Package struct {
Version string `yaml:"version"`
Description string `yaml:"description"`
InstallSteps []PackageInstallStep `yaml:"installSteps"`
Dependencies []string `yaml:"dependencies"`
}

func (p Package) install(cfg Config, context string) error {
Expand Down
43 changes: 38 additions & 5 deletions pkgmgr/pkgmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,55 @@ func (p *PackageManager) AvailablePackages() []Package {
}

func (p *PackageManager) InstalledPackages() []InstalledPackage {
var ret []InstalledPackage
for _, pkg := range p.state.InstalledPackages {
if pkg.Context == p.state.ActiveContext {
ret = append(ret, pkg)
}
}
return ret
}

func (p *PackageManager) InstalledPackagesAllContexts() []InstalledPackage {
return p.state.InstalledPackages
}

func (p *PackageManager) Install(pkg Package) error {
if err := pkg.install(p.config, p.state.ActiveContext); err != nil {
func (p *PackageManager) Install(pkgs ...string) error {
resolver, err := NewResolver(
p.InstalledPackages(),
p.AvailablePackages(),
p.config.Logger,
)
if err != nil {
return err
}
installedPkg := NewInstalledPackage(pkg, p.state.ActiveContext)
p.state.InstalledPackages = append(p.state.InstalledPackages, installedPkg)
if err := p.state.Save(); err != nil {
installPkgs, err := resolver.Install(pkgs...)
if err != nil {
return err
}
for _, installPkg := range installPkgs {
if err := installPkg.install(p.config, p.state.ActiveContext); err != nil {
return err
}
installedPkg := NewInstalledPackage(installPkg, p.state.ActiveContext)
p.state.InstalledPackages = append(p.state.InstalledPackages, installedPkg)
if err := p.state.Save(); err != nil {
return err
}
p.config.Logger.Info(
fmt.Sprintf(
"Successfully installed package %s (= %s) in context %q",
installPkg.Name,
installPkg.Version,
p.state.ActiveContext,
),
)
}
return nil
}

func (p *PackageManager) Uninstall(installedPkg InstalledPackage) error {
// TODO: resolve dependencies
if err := installedPkg.Package.uninstall(p.config, installedPkg.Context); err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions pkgmgr/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,21 @@ docker run --rm -ti ghcr.io/blinklabs-io/mithril-client:0.5.17 $@
},
},
},

// Test packages
{
Name: "test-packageA",
Version: "1.0.2",
},
{
Name: "test-packageA",
Version: "2.1.3",
},
{
Name: "test-packageB",
Version: "0.1.0",
Dependencies: []string{
"test-packageA < 2.0.0, >= 1.0.2",
},
},
}
Loading