Skip to content

Commit

Permalink
feat: upgrade support
Browse files Browse the repository at this point in the history
Fixes #56
  • Loading branch information
agaffney committed Mar 5, 2024
1 parent 2311387 commit 8ad30d6
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 63 deletions.
2 changes: 1 addition & 1 deletion cmd/cardano-up/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func installCommand() *cobra.Command {
return errors.New("no package provided")
}
if len(args) > 1 {
return errors.New("only one package may be specified a a time")
return errors.New("only one package may be specified at a time")
}
return nil
},
Expand Down
1 change: 1 addition & 0 deletions cmd/cardano-up/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func main() {
infoCommand(),
installCommand(),
uninstallCommand(),
upgradeCommand(),
)

if err := rootCmd.Execute(); err != nil {
Expand Down
12 changes: 7 additions & 5 deletions cmd/cardano-up/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"errors"
"fmt"
"log/slog"
"os"
Expand All @@ -27,15 +28,16 @@ func uninstallCommand() *cobra.Command {
return &cobra.Command{
Use: "uninstall",
Short: "Uninstall package",
Run: func(cmd *cobra.Command, args []string) {
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
slog.Error("no package provided")
os.Exit(1)
return errors.New("no package provided")
}
if len(args) > 1 {
slog.Error("only one package may be specified a a time")
os.Exit(1)
return errors.New("only one package may be specified at a time")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
pm, err := pkgmgr.NewDefaultPackageManager()
if err != nil {
slog.Error(fmt.Sprintf("failed to create package manager: %s", err))
Expand Down
54 changes: 54 additions & 0 deletions cmd/cardano-up/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"errors"
"fmt"
"log/slog"
"os"

"github.com/blinklabs-io/cardano-up/pkgmgr"
"github.com/spf13/cobra"
)

func upgradeCommand() *cobra.Command {
upgradeCmd := &cobra.Command{
Use: "upgrade",
Short: "Upgrade package",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("no package provided")
}
if len(args) > 1 {
return errors.New("only one package may be specified at a time")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
pm, err := pkgmgr.NewDefaultPackageManager()
if err != nil {
slog.Error(fmt.Sprintf("failed to create package manager: %s", err))
os.Exit(1)
}
// Upgrade requested package
if err := pm.Upgrade(args[0]); err != nil {
slog.Error(err.Error())
os.Exit(1)
}
},
}
return upgradeCmd
}
7 changes: 7 additions & 0 deletions pkgmgr/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,10 @@ func NewPackageUninstallWouldBreakDepsError(uninstallPkgName string, uninstallPk
dependentPkgVersion,
)
}

func NewNoPackageAvailableForUpgrade(pkgSpec string) error {
return fmt.Errorf(
"no package available for upgrade: %s",
pkgSpec,
)
}
4 changes: 4 additions & 0 deletions pkgmgr/installed_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ func NewInstalledPackage(pkg Package, context string, postInstallNotes string) I
PostInstallNotes: postInstallNotes,
}
}

func (i InstalledPackage) IsEmpty() bool {
return i.InstalledTime.IsZero()
}
102 changes: 89 additions & 13 deletions pkgmgr/pkgmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (p *PackageManager) Install(pkgs ...string) error {
resolver, err := NewResolver(
p.InstalledPackages(),
p.AvailablePackages(),
activeContextName,
p.config.Logger,
)
if err != nil {
Expand Down Expand Up @@ -160,6 +161,73 @@ func (p *PackageManager) Install(pkgs ...string) error {
return nil
}

func (p *PackageManager) Upgrade(pkgs ...string) error {
activeContextName, _ := p.ActiveContext()
resolver, err := NewResolver(
p.InstalledPackages(),
p.AvailablePackages(),
activeContextName,
p.config.Logger,
)
if err != nil {
return err
}
upgradePkgs, err := resolver.Upgrade(pkgs...)
if err != nil {
return err
}
var installedPkgs []string
var notesOutput string
for _, upgradePkg := range upgradePkgs {
p.config.Logger.Info(
fmt.Sprintf(
"Upgrading package %s (%s => %s)",
upgradePkg.Installed.Package.Name,
upgradePkg.Installed.Package.Version,
upgradePkg.Upgrade.Version,
),
)
// Uninstall old version
if err := p.uninstallPackage(upgradePkg.Installed); err != nil {
return err
}
// Install new version
notes, err := upgradePkg.Upgrade.install(p.config, activeContextName)
if err != nil {
return err
}
installedPkg := NewInstalledPackage(upgradePkg.Upgrade, activeContextName, notes)
p.state.InstalledPackages = append(p.state.InstalledPackages, installedPkg)
if err := p.state.Save(); err != nil {
return err
}
installedPkgs = append(installedPkgs, upgradePkg.Upgrade.Name)
if notes != "" {
notesOutput += fmt.Sprintf(
"\nPost-install notes for %s (= %s):\n\n%s\n",
upgradePkg.Upgrade.Name,
upgradePkg.Upgrade.Version,
notes,
)
}
if err := p.state.Save(); err != nil {
return err
}
}
// Display post-install notes
if notesOutput != "" {
p.config.Logger.Info(notesOutput)
}
p.config.Logger.Info(
fmt.Sprintf(
"Successfully upgraded/installed package(s) in context %q: %s",
activeContextName,
strings.Join(installedPkgs, ", "),
),
)
return nil
}

func (p *PackageManager) Uninstall(pkgs ...string) error {
// Find installed packages
activeContextName, _ := p.ActiveContext()
Expand All @@ -185,6 +253,7 @@ func (p *PackageManager) Uninstall(pkgs ...string) error {
resolver, err := NewResolver(
p.InstalledPackages(),
p.AvailablePackages(),
activeContextName,
p.config.Logger,
)
if err != nil {
Expand All @@ -194,21 +263,9 @@ func (p *PackageManager) Uninstall(pkgs ...string) error {
return err
}
for _, uninstallPkg := range uninstallPkgs {
// Uninstall package
if err := uninstallPkg.Package.uninstall(p.config, uninstallPkg.Context); err != nil {
if err := p.uninstallPackage(uninstallPkg); err != nil {
return err
}
// Remove package from installed packages
var tmpInstalledPackages []InstalledPackage
for _, tmpInstalledPkg := range p.state.InstalledPackages {
if tmpInstalledPkg.Context == uninstallPkg.Context &&
tmpInstalledPkg.Package.Name == uninstallPkg.Package.Name &&
tmpInstalledPkg.Package.Version == uninstallPkg.Package.Version {
continue
}
tmpInstalledPackages = append(tmpInstalledPackages, tmpInstalledPkg)
}
p.state.InstalledPackages = tmpInstalledPackages[:]
if err := p.state.Save(); err != nil {
return err
}
Expand Down Expand Up @@ -269,6 +326,25 @@ func (p *PackageManager) Info(pkgs ...string) error {
return nil
}

func (p *PackageManager) uninstallPackage(uninstallPkg InstalledPackage) error {
// Uninstall package
if err := uninstallPkg.Package.uninstall(p.config, uninstallPkg.Context); err != nil {
return err
}
// Remove package from installed packages
var tmpInstalledPackages []InstalledPackage
for _, tmpInstalledPkg := range p.state.InstalledPackages {
if tmpInstalledPkg.Context == uninstallPkg.Context &&
tmpInstalledPkg.Package.Name == uninstallPkg.Package.Name &&
tmpInstalledPkg.Package.Version == uninstallPkg.Package.Version {
continue
}
tmpInstalledPackages = append(tmpInstalledPackages, tmpInstalledPkg)
}
p.state.InstalledPackages = tmpInstalledPackages[:]
return nil
}

func (p *PackageManager) Contexts() map[string]Context {
return p.state.Contexts
}
Expand Down
5 changes: 5 additions & 0 deletions pkgmgr/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ docker run --rm -ti ghcr.io/blinklabs-io/mithril-client:0.7.0-1 $@
Version: "1.0.2",
PostInstallNotes: "Notes for {{ .Package.Name }}",
},
{
Name: "test-packageA",
Version: "1.0.3",
PostInstallNotes: "Notes for {{ .Package.Name }}",
},
{
Name: "test-packageA",
Version: "2.1.3",
Expand Down
Loading

0 comments on commit 8ad30d6

Please sign in to comment.