Skip to content

Commit

Permalink
Add a new Error and cleanup function for build command CreateRunnable…
Browse files Browse the repository at this point in the history
…Cmd failures

Based on the conv in suborbital#119,  we want to delete the output dir of `sudo create runnable` for any failure that occurs in `CreateRunnableCmd`.

`CreateRunnableError`s `Error` will also act as a cleanup function for these failures. atm, it's only deleting the output dir.

Ideally, cobra.Command would have a error callback feature... maybe we'll get one for Xmas. spf13/cobra#914
  • Loading branch information
denopink committed Nov 23, 2021
1 parent 750b9c9 commit 81ea21a
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions subo/command/create_runnable.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ var langAliases = map[string]string{
"gr": "grain",
}

// Error for build command CreateRunnableCmd failures
type CreateRunnableError struct {
Path string // The ouput directory for build command CreateRunnableCmd.
Err error // The original error.
}

// Error acts as a cleanup function for CreateRunnableError
func (err CreateRunnableError) Error() string {
if cleanup_err := os.RemoveAll(err.Path); cleanup_err != nil {
fmt.Println("🚫 failed to clean up runnable outputs")
}
return err.Err.Error()
}

// CreateRunnableCmd returns the build command
func CreateRunnableCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -79,18 +93,18 @@ func CreateRunnableCmd() *cobra.Command {

runnable, err := writeDotRunnable(bctx.Cwd, name, lang, namespace)
if err != nil {
return errors.Wrap(err, "🚫 failed to writeDotRunnable")
return errors.Wrap(CreateRunnableError{Path: path, Err: err}, "🚫 failed to writeDotRunnable")
}

templatesPath, err := template.TemplateFullPath(repo, branch)
if err != nil {
return errors.Wrap(err, "failed to TemplateDir")
return errors.Wrap(CreateRunnableError{Path: path, Err: err}, "failed to TemplateDir")
}

if update, _ := cmd.Flags().GetBool(updateTemplatesFlag); update {
templatesPath, err = template.UpdateTemplates(repo, branch)
if err != nil {
return errors.Wrap(err, "🚫 failed to UpdateTemplates")
return errors.Wrap(CreateRunnableError{Path: path, Err: err}, "🚫 failed to UpdateTemplates")
}
}

Expand All @@ -99,14 +113,14 @@ func CreateRunnableCmd() *cobra.Command {
if err == template.ErrTemplateMissing {
templatesPath, err = template.UpdateTemplates(repo, branch)
if err != nil {
return errors.Wrap(err, "🚫 failed to UpdateTemplates")
return errors.Wrap(CreateRunnableError{Path: path, Err: err}, "🚫 failed to UpdateTemplates")
}

if err := template.ExecRunnableTmpl(bctx.Cwd, name, templatesPath, runnable); err != nil {
return errors.Wrap(err, "🚫 failed to ExecTmplDir")
return errors.Wrap(CreateRunnableError{Path: path, Err: err}, "🚫 failed to ExecTmplDir")
}
} else {
return errors.Wrap(err, "🚫 failed to ExecTmplDir")
return errors.Wrap(CreateRunnableError{Path: path, Err: err}, "🚫 failed to ExecTmplDir")
}
}

Expand Down

0 comments on commit 81ea21a

Please sign in to comment.