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: add up/down commands #36

Merged
merged 1 commit into from
Mar 5, 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
31 changes: 31 additions & 0 deletions cmd/cardano-up/down.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

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

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

func downCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "down",
Short: "Stops all Docker containers",
Long: `Stops all running Docker containers for installed packages in the current context.`,
RunE: func(cmd *cobra.Command, args []string) error {
pm, err := pkgmgr.NewDefaultPackageManager()
if err != nil {
slog.Error(fmt.Sprintf("failed to create package manager: %s", err))
os.Exit(1)
}
if err := pm.Down(); err != nil {
slog.Error(err.Error())
os.Exit(1)
}
return nil
},
}
return cmd
}
2 changes: 2 additions & 0 deletions cmd/cardano-up/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func main() {
listAvailableCommand(),
installCommand(),
uninstallCommand(),
upCommand(),
downCommand(),
)

if err := rootCmd.Execute(); err != nil {
Expand Down
31 changes: 31 additions & 0 deletions cmd/cardano-up/up.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

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

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

func upCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "up",
Short: "Starts all Docker containers",
Long: `Starts all stopped Docker containers for installed packages in the current context.`,
RunE: func(cmd *cobra.Command, args []string) error {
pm, err := pkgmgr.NewDefaultPackageManager()
if err != nil {
slog.Error(fmt.Sprintf("failed to create package manager: %s", err))
os.Exit(1)
}
if err := pm.Up(); err != nil {
slog.Error(err.Error())
os.Exit(1)
}
return nil
},
}
return cmd
}
56 changes: 56 additions & 0 deletions pkgmgr/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package pkgmgr
import (
"fmt"
"io/fs"
"log/slog"
"os"
"path/filepath"
"strings"
)

type Package struct {
Expand Down Expand Up @@ -115,6 +117,60 @@ func (p Package) uninstall(cfg Config, context string) error {
return nil
}

func (p Package) startService(cfg Config, context string) error {
pkgName := fmt.Sprintf("%s-%s-%s", p.Name, p.Version, context)

var startErrors []string
for _, step := range p.InstallSteps {
if step.Docker != nil {
dockerService, err := NewDockerServiceFromContainerName(fmt.Sprintf("%s-%s", pkgName, step.Docker.ContainerName), cfg.Logger)
if err != nil {
startErrors = append(startErrors, fmt.Sprintf("error initializing Docker service for container %s: %v", dockerService.ContainerName, err))
continue
}
// Start the Docker container if it's not running
slog.Info(fmt.Sprintf("Starting Docker container %s", dockerService.ContainerName))
if err := dockerService.Start(); err != nil {
startErrors = append(startErrors, fmt.Sprintf("failed to start Docker container %s: %v", dockerService.ContainerName, err))
}
}
}

if len(startErrors) > 0 {
slog.Error(strings.Join(startErrors, "\n"))
return ErrOperationFailed
}

return nil
}

func (p Package) stopService(cfg Config, context string) error {
pkgName := fmt.Sprintf("%s-%s-%s", p.Name, p.Version, context)

var stopErrors []string
for _, step := range p.InstallSteps {
if step.Docker != nil {
dockerService, err := NewDockerServiceFromContainerName(fmt.Sprintf("%s-%s", pkgName, step.Docker.ContainerName), cfg.Logger)
if err != nil {
stopErrors = append(stopErrors, fmt.Sprintf("error initializing Docker service for container %s: %v", dockerService.ContainerName, err))
continue
}
// Stop the Docker container
slog.Info(fmt.Sprintf("Stopping container %s", dockerService.ContainerName))
if err := dockerService.Stop(); err != nil {
stopErrors = append(stopErrors, fmt.Sprintf("failed to stop Docker container %s: %v", dockerService.ContainerName, err))
}
}
}

if len(stopErrors) > 0 {
slog.Error(strings.Join(stopErrors, "\n"))
return ErrOperationFailed
}

return nil
}

type PackageInstallStep struct {
Docker *PackageInstallStepDocker `yaml:"docker,omitempty"`
File *PackageInstallStepFile `yaml:"file,omitempty"`
Expand Down
25 changes: 25 additions & 0 deletions pkgmgr/pkgmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@ func (p *PackageManager) AvailablePackages() []Package {
return p.availablePackages[:]
}

func (p *PackageManager) Up() error {
// Find installed packages
installedPackages := p.InstalledPackages()
for _, tmpPackage := range installedPackages {
err := tmpPackage.Package.startService(p.config, tmpPackage.Context)
if err != nil {
return err
}

}
return nil
}

func (p *PackageManager) Down() error {
// Find installed packages
installedPackages := p.InstalledPackages()
for _, tmpPackage := range installedPackages {
err := tmpPackage.Package.stopService(p.config, tmpPackage.Context)
if err != nil {
return err
}
}
return nil
}

func (p *PackageManager) InstalledPackages() []InstalledPackage {
var ret []InstalledPackage
for _, pkg := range p.state.InstalledPackages {
Expand Down