Skip to content

Commit

Permalink
feat: network validation
Browse files Browse the repository at this point in the history
Fixes #44
  • Loading branch information
agaffney committed Feb 27, 2024
1 parent 396310c commit a52a88d
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 50 deletions.
6 changes: 0 additions & 6 deletions cmd/cardano-up/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,6 @@ func contextDeleteCommand() *cobra.Command {
slog.Error(fmt.Sprintf("failed to create package manager: %s", err))
os.Exit(1)
}
activeContext, _ := pm.ActiveContext()
if args[0] == activeContext {
slog.Error("cannot delete active context")
os.Exit(1)
}
if err := pm.DeleteContext(args[0]); err != nil {
slog.Error(fmt.Sprintf("failed to delete context: %s", err))
os.Exit(1)
Expand All @@ -196,6 +191,5 @@ func contextDeleteCommand() *cobra.Command {
)
},
}
cmd.Flags().StringVarP(&contextFlags.description, "description", "d", "", "specifies description for context")
return cmd
}
58 changes: 29 additions & 29 deletions cmd/cardano-up/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import (
"github.com/spf13/cobra"
)

const (
// The default network used when installing into an empty context
defaultNetwork = "preprod"
)

var installFlags = struct {
network string
}{}
Expand All @@ -47,40 +52,35 @@ func installCommand() *cobra.Command {
slog.Error(fmt.Sprintf("failed to create package manager: %s", err))
os.Exit(1)
}
activeContextName, activeContext := pm.ActiveContext()
// Update context network if specified
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)
}
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,
),
)
}
// Check that context network is set
_, activeContext := pm.ActiveContext()
if activeContext.Network == "" {
slog.Error(
"no network specified for context, use -n/--network to specify a network",
activeContext.Network = defaultNetwork
if err := pm.UpdateContext(activeContextName, activeContext); err != nil {
slog.Error(err.Error())
os.Exit(1)
}
slog.Warn(
fmt.Sprintf(
"defaulting to network %q for context %q",
defaultNetwork,
activeContextName,
),
)
os.Exit(1)
}
// Install requested package
if err := pm.Install(args[0]); err != nil {
Expand All @@ -89,6 +89,6 @@ func installCommand() *cobra.Command {
}
},
}
installCmd.Flags().StringVarP(&installFlags.network, "network", "n", "", "specifies network for package")
installCmd.Flags().StringVarP(&installFlags.network, "network", "n", "", fmt.Sprintf("specifies network for package (defaults to %q for empty context)", defaultNetwork))
return installCmd
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/blinklabs-io/cardano-up
go 1.21

require (
github.com/blinklabs-io/gouroboros v0.71.0
github.com/docker/docker v25.0.3+incompatible
github.com/docker/go-connections v0.5.0
github.com/hashicorp/go-version v1.6.0
Expand All @@ -16,23 +17,27 @@ require (
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fxamacker/cbor/v2 v2.5.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.48.0 // indirect
go.opentelemetry.io/otel v1.23.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.23.1 // indirect
go.opentelemetry.io/otel/metric v1.23.1 // indirect
go.opentelemetry.io/otel/sdk v1.23.1 // indirect
go.opentelemetry.io/otel/trace v1.23.1 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/time v0.5.0 // indirect
gotest.tools/v3 v3.5.1 // indirect
)
14 changes: 12 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOEl
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/blinklabs-io/gouroboros v0.71.0 h1:vu7Obgmn/kjFYMiIS4dr9PurzklQhgKVsX/m4hc3eos=
github.com/blinklabs-io/gouroboros v0.71.0/go.mod h1:9UWZu7J050BNTY6TnXioJjgZiQD0pWzpNwM9T9rjEbY=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
Expand All @@ -19,6 +21,8 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fxamacker/cbor/v2 v2.5.0 h1:oHsG0V/Q6E/wqTS2O1Cozzsy69nqCiguo5Q1a1ADivE=
github.com/fxamacker/cbor/v2 v2.5.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
Expand All @@ -36,6 +40,8 @@ github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mO
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/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -64,6 +70,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.48.0 h1:doUP+ExOpH3spVTLS0FcWGLnQrPct/hD/bCPbDRUEAU=
Expand All @@ -85,6 +93,8 @@ go.opentelemetry.io/proto/otlp v1.1.0/go.mod h1:GpBHCBWiqvVLDqmHZsoMM3C5ySeKTC7e
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand All @@ -101,8 +111,8 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
Expand Down
6 changes: 3 additions & 3 deletions pkgmgr/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const (
)

var defaultContext = Context{
Network: "preprod",
Description: "Default context",
}

type Context struct {
Description string `yaml:"description"`
Network string `yaml:"network"`
Description string `yaml:"description"`
Network string `yaml:"network"`
NetworkMagic uint32 `yaml:"networkMagic"`
}
16 changes: 16 additions & 0 deletions pkgmgr/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,25 @@ var ErrContextNotExist = errors.New("context does not exist")
// ErrContextAlreadyExists is returned when creating a context with a name that is already in use
var ErrContextAlreadyExists = errors.New("specified context already exists")

// ErrContextNoChangeNetwork is returned when updating a context with a network different than what was previously configured
var ErrContextNoChangeNetwork = errors.New("cannot change the configured network for a context")

// ErrContextInstallNoNetwork is returned when performing an install with no network specified on the active context
var ErrContextInstallNoNetwork = errors.New("no network specified for context")

// ErrContextNoDeleteActive is returned when attempting to delete the active context
var ErrContextNoDeleteActive = errors.New("cannot delete active context")

// 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 NewUnknownNetworkError(networkName string) error {
return fmt.Errorf(
"unknown network %q",
networkName,
)
}

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",
Expand Down
59 changes: 50 additions & 9 deletions pkgmgr/pkgmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package pkgmgr
import (
"errors"
"fmt"

ouroboros "github.com/blinklabs-io/gouroboros"
)

type PackageManager struct {
Expand Down Expand Up @@ -65,8 +67,9 @@ func (p *PackageManager) initTemplate() {
activeContextName, activeContext := p.ActiveContext()
tmplVars := map[string]any{
"Context": map[string]any{
"Name": activeContextName,
"Network": activeContext.Network,
"Name": activeContextName,
"Network": activeContext.Network,
"NetworkMagic": activeContext.NetworkMagic,
},
}
tmpConfig := p.config
Expand Down Expand Up @@ -97,6 +100,11 @@ func (p *PackageManager) InstalledPackagesAllContexts() []InstalledPackage {
}

func (p *PackageManager) Install(pkgs ...string) error {
// Check context for network
activeContextName, activeContext := p.ActiveContext()
if activeContext.Network == "" {
return ErrContextInstallNoNetwork
}
resolver, err := NewResolver(
p.InstalledPackages(),
p.AvailablePackages(),
Expand All @@ -110,10 +118,10 @@ func (p *PackageManager) Install(pkgs ...string) error {
return err
}
for _, installPkg := range installPkgs {
if err := installPkg.install(p.config, p.state.ActiveContext); err != nil {
if err := installPkg.install(p.config, activeContextName); err != nil {
return err
}
installedPkg := NewInstalledPackage(installPkg, p.state.ActiveContext)
installedPkg := NewInstalledPackage(installPkg, activeContextName)
p.state.InstalledPackages = append(p.state.InstalledPackages, installedPkg)
if err := p.state.Save(); err != nil {
return err
Expand All @@ -123,7 +131,7 @@ func (p *PackageManager) Install(pkgs ...string) error {
"Successfully installed package %s (= %s) in context %q",
installPkg.Name,
installPkg.Version,
p.state.ActiveContext,
activeContextName,
),
)
}
Expand Down Expand Up @@ -164,14 +172,19 @@ func (p *PackageManager) AddContext(name string, context Context) error {
if _, ok := p.state.Contexts[name]; ok {
return ErrContextAlreadyExists
}
p.state.Contexts[name] = context
if err := p.state.Save(); err != nil {
// Create dummy context entry
p.state.Contexts[name] = Context{}
// Update dummy context
if err := p.updateContext(name, context); err != nil {
return err
}
return nil
}

func (p *PackageManager) DeleteContext(name string) error {
if name == p.state.ActiveContext {
return ErrContextNoDeleteActive
}
if _, ok := p.state.Contexts[name]; !ok {
return ErrContextNotExist
}
Expand All @@ -190,16 +203,44 @@ func (p *PackageManager) SetActiveContext(name string) error {
if err := p.state.Save(); err != nil {
return err
}
// Update templating values
p.initTemplate()
return nil
}

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

func (p *PackageManager) updateContext(name string, newContext Context) error {
// Get current state of named context
curContext, ok := p.state.Contexts[name]
if !ok {
return ErrContextNotExist
}
p.state.Contexts[name] = context
if curContext.Network != "" {
// Check that we're not changing the network once configured
if newContext.Network != curContext.Network {
return ErrContextNoChangeNetwork
}
} else {
// Check network name if setting it for new/empty context
if newContext.Network != "" {
tmpNetwork := ouroboros.NetworkByName(newContext.Network)
if tmpNetwork == ouroboros.NetworkInvalid {
return NewUnknownNetworkError(newContext.Network)
}
newContext.NetworkMagic = tmpNetwork.NetworkMagic
}
}
p.state.Contexts[name] = newContext
if err := p.state.Save(); err != nil {
return err
}
// Update templating values
p.initTemplate()
return nil
}

0 comments on commit a52a88d

Please sign in to comment.