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: set context network on install #39

Merged
merged 1 commit into from
Feb 24, 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
47 changes: 41 additions & 6 deletions cmd/cardano-up/install.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 @@ -23,24 +24,56 @@ import (
"github.com/spf13/cobra"
)

var installFlags = struct {
network string
}{}

func installCommand() *cobra.Command {
return &cobra.Command{
installCmd := &cobra.Command{
Use: "install",
Short: "Install 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 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 installFlags.network != "" {
activeContextName, activeContext := pm.ActiveContext()
if activeContext.Network == "" {
activeContext.Network = installFlags.network
if err := pm.UpdateContext(activeContextName, activeContext); err != nil {
slog.Error(err.Error())
os.Exit(1)
}
slog.Debug(
fmt.Sprintf(
"set active context network to %q",
installFlags.network,
),
)
} else {
if activeContext.Network != installFlags.network {
slog.Error(
fmt.Sprintf(
"active context already has network %q, cannot set to %q",
activeContext.Network,
installFlags.network,
),
)
os.Exit(1)
}
}
}
packages := pm.AvailablePackages()
foundPackage := false
for _, tmpPackage := range packages {
Expand All @@ -60,4 +93,6 @@ func installCommand() *cobra.Command {
slog.Info(fmt.Sprintf("Successfully installed package %s", args[0]))
},
}
installCmd.Flags().StringVarP(&installFlags.network, "network", "n", "preprod", "specifies network for package")
return installCmd
}
11 changes: 11 additions & 0 deletions pkgmgr/pkgmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,14 @@ func (p *PackageManager) SetActiveContext(name string) error {
}
return nil
}

func (p *PackageManager) UpdateContext(name string, context Context) error {
if _, ok := p.state.Contexts[name]; !ok {
return ErrContextNotExist
}
p.state.Contexts[name] = context
if err := p.state.Save(); err != nil {
return err
}
return nil
}