diff --git a/cmd/cardano-up/info.go b/cmd/cardano-up/info.go new file mode 100644 index 0000000..da2a8b0 --- /dev/null +++ b/cmd/cardano-up/info.go @@ -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) + } + }, + } +} diff --git a/cmd/cardano-up/main.go b/cmd/cardano-up/main.go index d85e6eb..2d4d7f4 100644 --- a/cmd/cardano-up/main.go +++ b/cmd/cardano-up/main.go @@ -67,6 +67,7 @@ func main() { versionCommand(), listCommand(), listAvailableCommand(), + infoCommand(), installCommand(), uninstallCommand(), ) diff --git a/pkgmgr/pkgmgr.go b/pkgmgr/pkgmgr.go index 7a37ec2..ff2e1a1 100644 --- a/pkgmgr/pkgmgr.go +++ b/pkgmgr/pkgmgr.go @@ -26,7 +26,6 @@ type PackageManager struct { config Config state *State availablePackages []Package - // TODO } func NewPackageManager(cfg Config) (*PackageManager, error) { @@ -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 }