Skip to content

Commit

Permalink
feat: store info about installed packages (#34)
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
agaffney authored Feb 21, 2024
1 parent 8cc4ac3 commit 3836326
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 26 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
19 changes: 19 additions & 0 deletions pkgmgr/installed_package.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
34 changes: 17 additions & 17 deletions pkgmgr/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion pkgmgr/pkgmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
56 changes: 48 additions & 8 deletions pkgmgr/state.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -27,13 +33,25 @@ 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
}

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
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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))
}

0 comments on commit 3836326

Please sign in to comment.