From 38363262e5ccbd541067368dc5aa81a4c19433ea Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Wed, 21 Feb 2024 17:28:41 -0600 Subject: [PATCH] feat: store info about installed packages (#34) Fixes #6 --- go.mod | 1 + go.sum | 1 + pkgmgr/installed_package.go | 19 +++++++++++++ pkgmgr/package.go | 34 +++++++++++----------- pkgmgr/pkgmgr.go | 10 ++++++- pkgmgr/state.go | 56 +++++++++++++++++++++++++++++++------ 6 files changed, 95 insertions(+), 26 deletions(-) create mode 100644 pkgmgr/installed_package.go diff --git a/go.mod b/go.mod index d17cffe..b44f3b6 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/docker/docker v25.0.3+incompatible github.com/docker/go-connections v0.5.0 github.com/spf13/cobra v1.8.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( diff --git a/go.sum b/go.sum index 605d6b1..00060bc 100644 --- a/go.sum +++ b/go.sum @@ -123,6 +123,7 @@ google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0= google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs= google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkgmgr/installed_package.go b/pkgmgr/installed_package.go new file mode 100644 index 0000000..8d48095 --- /dev/null +++ b/pkgmgr/installed_package.go @@ -0,0 +1,19 @@ +package pkgmgr + +import ( + "time" +) + +type InstalledPackage struct { + Package Package + InstalledTime time.Time + Context string +} + +func NewInstalledPackage(pkg Package, context string) InstalledPackage { + return InstalledPackage{ + Package: pkg, + InstalledTime: time.Now(), + Context: context, + } +} diff --git a/pkgmgr/package.go b/pkgmgr/package.go index 50e6319..ceab3a7 100644 --- a/pkgmgr/package.go +++ b/pkgmgr/package.go @@ -22,10 +22,10 @@ import ( ) type Package struct { - Name string - Version string - Description string - InstallSteps []PackageInstallStep + Name string `yaml:"name"` + Version string `yaml:"version"` + Description string `yaml:"description"` + InstallSteps []PackageInstallStep `yaml:"installSteps"` } func (p Package) install(cfg Config) error { @@ -52,18 +52,18 @@ func (p Package) install(cfg Config) error { } type PackageInstallStep struct { - Docker *PackageInstallStepDocker - File *PackageInstallStepFile + Docker *PackageInstallStepDocker `yaml:"docker,omitempty"` + File *PackageInstallStepFile `yaml:"file,omitempty"` } type PackageInstallStepDocker struct { - ContainerName string - Image string - Env map[string]string - Command []string - Args []string - Binds []string - Ports []string + ContainerName string `yaml:"containerName"` + Image string `yaml:"image,omitempty"` + Env map[string]string `yaml:"env,omitempty"` + Command []string `yaml:"command,omitempty"` + Args []string `yaml:"args,omitempty"` + Binds []string `yaml:"binds,omitempty"` + Ports []string `yaml:"ports,omitempty"` } func (p *PackageInstallStepDocker) install(cfg Config, pkgName string) error { @@ -88,10 +88,10 @@ func (p *PackageInstallStepDocker) install(cfg Config, pkgName string) error { } type PackageInstallStepFile struct { - Filename string - Content string - Template bool - Mode fs.FileMode + Filename string `yaml:"filename"` + Content string `yaml:"content"` + Template bool `yaml:"template"` + Mode fs.FileMode `yaml:"mode,omitempty"` } func (p *PackageInstallStepFile) install(cfg Config, pkgName string) error { diff --git a/pkgmgr/pkgmgr.go b/pkgmgr/pkgmgr.go index a2fa4c7..f8ffc50 100644 --- a/pkgmgr/pkgmgr.go +++ b/pkgmgr/pkgmgr.go @@ -68,5 +68,13 @@ func (p *PackageManager) AvailablePackages() []Package { } func (p *PackageManager) Install(pkg Package) error { - return pkg.install(p.config) + if err := pkg.install(p.config); err != nil { + return err + } + installedPkg := NewInstalledPackage(pkg, p.state.ActiveContext) + p.state.InstalledPackages = append(p.state.InstalledPackages, installedPkg) + if err := p.state.Save(); err != nil { + return err + } + return nil } diff --git a/pkgmgr/state.go b/pkgmgr/state.go index caf647a..f590cf9 100644 --- a/pkgmgr/state.go +++ b/pkgmgr/state.go @@ -1,19 +1,25 @@ package pkgmgr import ( - "encoding/json" "os" "path/filepath" + + "gopkg.in/yaml.v3" ) const ( - environmentsFilename = "environments.json" + defaultContext = "default" + + environmentsFilename = "environments.yaml" + activeContextFilename = "active_context.yaml" + installedPackagesFilename = "installed_packages.yaml" ) type State struct { - config Config - Environments []Environment - // TODO: installed packages + config Config + ActiveContext string + Environments []Environment + InstalledPackages []InstalledPackage } func NewState(cfg Config) *State { @@ -27,6 +33,12 @@ func (s *State) Load() error { if err := s.loadEnvironments(); err != nil { return err } + if err := s.loadActiveContext(); err != nil { + return err + } + if err := s.loadInstalledPackages(); err != nil { + return err + } return nil } @@ -34,6 +46,12 @@ func (s *State) Save() error { if err := s.saveEnvironments(); err != nil { return err } + if err := s.saveActiveContext(); err != nil { + return err + } + if err := s.saveInstalledPackages(); err != nil { + return err + } return nil } @@ -54,7 +72,7 @@ func (s *State) loadFile(filename string, dest any) error { if err != nil { return err } - if err := json.Unmarshal(content, dest); err != nil { + if err := yaml.Unmarshal(content, dest); err != nil { return err } return nil @@ -73,11 +91,11 @@ func (s *State) saveFile(filename string, src any) error { s.config.ConfigDir, filename, ) - jsonContent, err := json.Marshal(src) + yamlContent, err := yaml.Marshal(src) if err != nil { return err } - if err := os.WriteFile(tmpPath, jsonContent, os.ModePerm); err != nil { + if err := os.WriteFile(tmpPath, yamlContent, os.ModePerm); err != nil { return err } return nil @@ -90,3 +108,25 @@ func (s *State) loadEnvironments() error { func (s *State) saveEnvironments() error { return s.saveFile(environmentsFilename, &(s.Environments)) } + +func (s *State) loadActiveContext() error { + if err := s.loadFile(activeContextFilename, &(s.ActiveContext)); err != nil { + return err + } + if s.ActiveContext == "" { + s.ActiveContext = defaultContext + } + return nil +} + +func (s *State) saveActiveContext() error { + return s.saveFile(activeContextFilename, &(s.ActiveContext)) +} + +func (s *State) loadInstalledPackages() error { + return s.loadFile(installedPackagesFilename, &(s.InstalledPackages)) +} + +func (s *State) saveInstalledPackages() error { + return s.saveFile(installedPackagesFilename, &(s.InstalledPackages)) +}