Skip to content

Commit

Permalink
feat: info command (#75)
Browse files Browse the repository at this point in the history
Fixes #58
  • Loading branch information
agaffney authored Mar 5, 2024
1 parent 16ba23a commit 2311387
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
53 changes: 53 additions & 0 deletions cmd/cardano-up/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 infoCommand() *cobra.Command {
return &cobra.Command{
Use: "info",
Aliases: []string{"status"},
Short: "Show info for an installed 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 a 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)
}
if err := pm.Info(args[0]); err != nil {
slog.Error(err.Error())
os.Exit(1)
}
},
}
}
1 change: 1 addition & 0 deletions cmd/cardano-up/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func main() {
versionCommand(),
listCommand(),
listAvailableCommand(),
infoCommand(),
installCommand(),
uninstallCommand(),
)
Expand Down
46 changes: 45 additions & 1 deletion pkgmgr/pkgmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type PackageManager struct {
config Config
state *State
availablePackages []Package
// TODO
}

func NewPackageManager(cfg Config) (*PackageManager, error) {
Expand Down Expand Up @@ -225,6 +224,51 @@ func (p *PackageManager) Uninstall(pkgs ...string) error {
return nil
}

func (p *PackageManager) Info(pkgs ...string) error {
// Find installed packages
activeContextName, _ := p.ActiveContext()
installedPackages := p.InstalledPackages()
var infoPkgs []InstalledPackage
for _, pkg := range pkgs {
foundPackage := false
for _, tmpPackage := range installedPackages {
if tmpPackage.Package.Name == pkg {
foundPackage = true
infoPkgs = append(
infoPkgs,
tmpPackage,
)
break
}
}
if !foundPackage {
return NewPackageNotInstalledError(pkg, activeContextName)
}
}
var infoOutput string
for idx, infoPkg := range infoPkgs {
infoOutput += fmt.Sprintf(
"Name: %s\nVersion: %s\nContext: %s",
infoPkg.Package.Name,
infoPkg.Package.Version,
activeContextName,
)
if infoPkg.PostInstallNotes != "" {
infoOutput += fmt.Sprintf(
"\n\nPost-install notes:\n\n%s",
infoPkg.PostInstallNotes,
)
}
// TODO: list services
// TODO: list container ports
if idx < len(infoPkgs)-1 {
infoOutput += "\n\n---\n\n"
}
}
p.config.Logger.Info(infoOutput)
return nil
}

func (p *PackageManager) Contexts() map[string]Context {
return p.state.Contexts
}
Expand Down

0 comments on commit 2311387

Please sign in to comment.