Skip to content

Commit

Permalink
installer/installer.go: Use the new Steps struct
Browse files Browse the repository at this point in the history
  • Loading branch information
livingsilver94 committed Nov 20, 2023
1 parent cab3643 commit cd2dd96
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ type Installer struct {
repository repo.Repo
variables repo.Variables
list List
writer StepWriter
}

func New(repository repo.Repo, options ...Option) Installer {
i := Installer{
repository: repository,
variables: repo.NewVariables(),
list: NewList(),
writer: nil, // TODO: pass a real writer.
}
i.variables.RegisterSolver(service.Datadir, solver.NewDatadir(repository))
for _, option := range options {
Expand All @@ -41,41 +43,48 @@ func (inst *Installer) Install(srv *service.Service) error {
}
for level := depGraph.Depth() - 1; level >= 0; level-- {
depGraph.Level(level).ForEach(func(dep *service.Service) bool {
err = inst.installSingle(dep)
err = inst.InstallSingle(dep)
return err == nil
})
if err != nil {
return err
}
}
return inst.installSingle(srv)
return inst.InstallSingle(srv)
}

func (inst *Installer) installSingle(srv *service.Service) error {
log := slog.Default().WithGroup(srv.Name)
func (inst *Installer) InstallSingle(srv *service.Service) error {
if inst.list.Contains(srv.Name) {
log.Info("Already installed")
slog.Default().WithGroup(srv.Name).Info("Already installed")
return nil
}
err := inst.variables.InsertMany(srv.Name, srv.Variables)
err := inst.runAllSteps(srv)
if err != nil {
return err
}
repl := NewTemplate(srv.Name, inst.variables)
steps := []Step{
Setup{},
OSPackages{},
NewSymlinks(inst.repository, repl),
NewCopies(inst.repository, repl),
NewFinalization(repl),
inst.list.Insert(srv.Name)
return nil
}

func (inst *Installer) Steps(srv *service.Service) Steps {
return NewSteps(srv, inst.writer)
}

func (inst *Installer) runAllSteps(srv *service.Service) error {
steps := inst.Steps(srv)
list := []func() error{
func() error { return steps.Setup() },
func() error { return steps.InstallPackages() },
func() error { return steps.LinkFiles(inst.repository, inst.variables) },
func() error { return steps.CopyFiles(inst.repository, inst.variables) },
func() error { return steps.Finalize(inst.variables) },
}
for _, step := range steps {
err := step.Run(log, srv)
for _, step := range list {
err := step()
if err != nil {
return err
}
}
inst.list.Insert(srv.Name)
return nil
}

Expand All @@ -100,3 +109,9 @@ func WithList(li List) Option {
i.list = li
}
}

func WithStepWriter(sw StepWriter) Option {
return func(i *Installer) {
i.writer = sw
}
}

0 comments on commit cd2dd96

Please sign in to comment.