Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Error and cleanup function for CreateRunnableCmd failures #135

Merged
merged 5 commits into from
Nov 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
}

// CreateRunnableError wraps errors for CreateRunnableCmd() failures
type CreateRunnableError struct {
Path string // The ouput directory for build command CreateRunnableCmd().
error // The original error.
}

// NewCreateRunnableError cleans up and returns CreateRunnableError for CreateRunnableCmd() failures
func NewCreateRunnableError(path string, err error) CreateRunnableError {
if cleanupErr := os.RemoveAll(path); cleanupErr != nil {
err = errors.Wrap(err, "failed to clean up runnable outputs")
}
return CreateRunnableError{Path: path, error: err}
}

// 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(NewCreateRunnableError(path, err), "🚫 failed to writeDotRunnable")
}

templatesPath, err := template.TemplateFullPath(repo, branch)
if err != nil {
return errors.Wrap(err, "failed to TemplateDir")
return errors.Wrap(NewCreateRunnableError(path, 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(NewCreateRunnableError(path, 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(NewCreateRunnableError(path, 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(NewCreateRunnableError(path, err), "🚫 failed to ExecTmplDir")
}
} else {
return errors.Wrap(err, "🚫 failed to ExecTmplDir")
return errors.Wrap(NewCreateRunnableError(path, err), "🚫 failed to ExecTmplDir")
}
}

Expand Down