-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
81 lines (69 loc) · 1.79 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package cli
import (
"errors"
"fmt"
"path/filepath"
"github.com/futurice/jalapeno/internal/cli/option"
"github.com/futurice/jalapeno/pkg/recipe"
"github.com/futurice/jalapeno/pkg/recipeutil"
"github.com/spf13/cobra"
)
type createOptions struct {
RecipeName string
option.Common
option.WorkingDirectory
}
func NewCreateCmd() *cobra.Command {
var opts createOptions
var cmd = &cobra.Command{
Use: "create RECIPE_NAME",
Short: "Create a new recipe",
Long: fmt.Sprintf(`Create a new recipe with the given name.
The following files will be created:
%[1]s
my-recipe
├── %[2]s
├── %[3]s
│ └── README.md
└── %[4]s
└── defaults
├── %[5]s
└── %[6]s
└── README.md
%[1]s`,
"```",
recipe.RecipeFileName+recipe.YAMLExtension,
recipe.RecipeTemplatesDirName,
recipe.RecipeTestsDirName,
recipe.RecipeTestMetaFileName+recipe.YAMLExtension,
recipe.RecipeTestFilesDirName,
),
Example: `jalapeno create my-recipe`,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.RecipeName = args[0]
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
err := runCreate(cmd, opts)
return errorHandler(cmd, err)
},
}
if err := option.ApplyFlags(&opts, cmd.Flags()); err != nil {
return nil
}
return cmd
}
func runCreate(cmd *cobra.Command, opts createOptions) error {
re := recipeutil.CreateExampleRecipe(opts.RecipeName)
err := re.Validate()
if err != nil {
return errors.New("placeholder recipe is not valid")
}
err = re.Save(filepath.Join(opts.Dir, opts.RecipeName))
if err != nil {
return fmt.Errorf("can not save recipe to the directory: %w", err)
}
cmd.Printf("Recipe '%s' created!\n", opts.RecipeName)
return nil
}