Skip to content

Commit

Permalink
Handle final error checks during install
Browse files Browse the repository at this point in the history
  • Loading branch information
evanpurkhiser committed Feb 22, 2020
1 parent c850989 commit f2d1a2f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
19 changes: 15 additions & 4 deletions cmd/dots/install.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"

"github.com/spf13/cobra"

"go.evanpurkhiser.com/dots/installer"
Expand Down Expand Up @@ -48,11 +50,20 @@ var installCmd = cobra.Command{
defer installLogger.LogEvents()()

installed := installer.InstallDotfiles(prepared, installConfig)
installer.RunInstallScripts(prepared, installConfig)
installer.FinalizeInstall(installed, installConfig)
executedScripts := installer.RunInstallScripts(prepared, installConfig)
finalizeErr := installer.FinalizeInstall(installed, installConfig)

if installed.HadError() {
return fmt.Errorf("some dotfiles failed to install")
}

// TODO Collect errors from installation, script execution, and
// finalization to determine exit code.
if executedScripts.HadError() {
return fmt.Errorf("some dotfiles scripts had errors")
}

if finalizeErr != nil {
return fmt.Errorf("finalization error: %s", finalizeErr)
}

return nil
},
Expand Down
18 changes: 16 additions & 2 deletions installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ type InstalledDotfile struct {
InstallError error
}

// InstalledDotfiles represents a set of installed dotfiles.
type InstalledDotfiles []*InstalledDotfile

// HadError indicates if any dotfiles had errors while preparing or installing
func (i *InstalledDotfiles) HadError() bool {
for _, dotfile := range *i {
if dotfile.PrepareError != nil || dotfile.InstallError != nil {
return true
}
}

return false
}

// WillInstallDotfile indicates weather the dotfile will be installed if
// InstallDotfile is given the prepared dotfile. This does not guarentee that
// errors will not occur during installation.
Expand Down Expand Up @@ -114,11 +128,11 @@ func InstallDotfile(dotfile *PreparedDotfile, config InstallConfig) error {

// InstallDotfiles asynchronously calls InstalledDotfile on all passed
// PreparedDotfiles.
func InstallDotfiles(install PreparedInstall, config InstallConfig) []*InstalledDotfile {
func InstallDotfiles(install PreparedInstall, config InstallConfig) InstalledDotfiles {
waitGroup := sync.WaitGroup{}
waitGroup.Add(len(install.Dotfiles))

installed := make([]*InstalledDotfile, len(install.Dotfiles))
installed := make(InstalledDotfiles, len(install.Dotfiles))

doInstall := func(i int, dotfile *PreparedDotfile) {
err := InstallDotfile(dotfile, config)
Expand Down
18 changes: 16 additions & 2 deletions installer/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ type ExecutedScript struct {
ExecutionError error
}

// ExecutedScripts represents a set of executed scripts.
type ExecutedScripts []*ExecutedScript

// HadError indicates if any dotfiles had errors while preparing or installing
func (i *ExecutedScripts) HadError() bool {
for _, script := range *i {
if script.PrepareError != nil || script.ExecutionError != nil {
return true
}
}

return false
}

// RunInstallScript executes a single InstallScript.
func RunInstallScript(script *InstallScript, config InstallConfig) error {
if !script.ShouldInstall() && !config.ForceReinstall {
Expand Down Expand Up @@ -51,8 +65,8 @@ func RunInstallScript(script *InstallScript, config InstallConfig) error {
// RunInstallScripts executes the installation scripts of a PreparedInstall for
// files that have changed (unless ForceReinstall is enabled, in which case
// *all* scripts will be run).
func RunInstallScripts(install PreparedInstall, config InstallConfig) []*ExecutedScript {
executedScripts := make([]*ExecutedScript, len(install.InstallScripts))
func RunInstallScripts(install PreparedInstall, config InstallConfig) ExecutedScripts {
executedScripts := make(ExecutedScripts, len(install.InstallScripts))

config.EventLogger <- events.Event{
Type: events.ScriptExecStarted,
Expand Down

0 comments on commit f2d1a2f

Please sign in to comment.