diff --git a/cmd/bundle.go b/cmd/bundle.go index 3f83755..af6d1de 100644 --- a/cmd/bundle.go +++ b/cmd/bundle.go @@ -87,7 +87,11 @@ func NewCmdBundle() *cobra.Command { bundleCmd.AddCommand(bundleTemplateCmd) bundleTemplateCmd.AddCommand(bundleTemplateListCmd) bundleTemplateCmd.AddCommand(bundleTemplateRefreshCmd) - + bundleNewCmd.Flags().StringP("name", "n", "", "Name of the new bundle") + bundleNewCmd.Flags().StringP("description", "d", "", "Description of the new bundle") + bundleNewCmd.Flags().StringP("template-type", "t", "", "Name of the bundle template to use") + bundleNewCmd.Flags().StringSliceP("connections", "c", []string{}, "Connections and names to add to the bundle - example: massdriver/vpc=network") + bundleNewCmd.Flags().StringP("output-directory", "o", ".", "Directory to output the new bundle") return bundleCmd } @@ -117,14 +121,113 @@ func runBundleTemplateRefresh(cmd *cobra.Command, args []string) error { return commands.RefreshTemplates(cache) } +func runBundleNewInteractive(outputDir string) (*templatecache.TemplateData, error) { + templateData := &templatecache.TemplateData{ + Access: "private", + // Promptui templates are a nightmare. Need to support multi repos when moving this to bubbletea + TemplateRepo: "/massdriver-cloud/application-templates", + // TODO: unify bundle build and app build outputDir logic and support + OutputDir: outputDir, + } + + err := bundle.RunPromptNew(templateData) + if err != nil { + return nil, err + } + + return templateData, nil +} + +func runBundleNewFlags(cmd *cobra.Command) (*templatecache.TemplateData, error) { + name, err := cmd.Flags().GetString("name") + if err != nil { + return nil, err + } + + description, err := cmd.Flags().GetString("description") + if err != nil { + return nil, err + } + + connections, err := cmd.Flags().GetStringSlice("connections") + if err != nil { + return nil, err + } + + templateName, err := cmd.Flags().GetString("template-type") + if err != nil { + return nil, err + } + + outputDir, err := cmd.Flags().GetString("output-directory") + if err != nil { + return nil, err + } + + connectionData := make([]templatecache.Connection, len(connections)) + for i, conn := range connections { + parts := strings.Split(conn, "=") + if len(parts) != 2 { + return nil, fmt.Errorf("invalid connection argument: %s", conn) + } + connectionData[i] = templatecache.Connection{ + ArtifactDefinition: parts[1], + Name: parts[0], + } + } + + templateData := &templatecache.TemplateData{ + Access: "private", + TemplateRepo: "/massdriver-cloud/application-templates", + OutputDir: outputDir, + Name: name, + Description: description, + TemplateName: templateName, + Connections: connectionData, + } + + return templateData, nil +} + func runBundleNew(cmd *cobra.Command, args []string) error { - var fs = afero.NewOsFs() - cache, _ := templatecache.NewBundleTemplateCache(templatecache.GithubTemplatesFetcher, fs) - err := commands.RefreshTemplates(cache) + fs := afero.NewOsFs() + cache, err := templatecache.NewBundleTemplateCache(templatecache.GithubTemplatesFetcher, fs) if err != nil { return err } + err = commands.RefreshTemplates(cache) + if err != nil { + return err + } + + var ( + name string + templateName string + outputDir string + ) + + // define flag + name, err = cmd.Flags().GetString("name") + if err != nil { + return err + } + + templateName, err = cmd.Flags().GetString("template-type") + if err != nil { + return err + } + + outputDir, err = cmd.Flags().GetString("output-directory") + if err != nil { + return err + } + + // parse flags + if err = cmd.ParseFlags(args); err != nil { + return err + } + c, configErr := config.Get() if configErr != nil { return configErr @@ -148,15 +251,21 @@ func runBundleNew(cmd *cobra.Command, args []string) error { bundle.SetMassdriverArtifactDefinitions(artifacts) - templateData := &templatecache.TemplateData{ - Access: "private", - // Promptui templates are a nightmare. Need to support multi repos when moving this to bubbletea - TemplateRepo: "/massdriver-cloud/application-templates", - // TODO: unify bundle build and app build outputDir logic and support - OutputDir: ".", + var templateData *templatecache.TemplateData + if name == "" || templateName == "" { + // run the interactive prompt + templateData, err = runBundleNewInteractive(outputDir) + if err != nil { + return err + } + } else { + // skip the interactive prompt and use flags + templateData, err = runBundleNewFlags(cmd) + if err != nil { + return err + } } - err = bundle.RunPromptNew(templateData) if err != nil { return err } @@ -165,7 +274,6 @@ func runBundleNew(cmd *cobra.Command, args []string) error { if err != nil { return err } - return nil } diff --git a/docs/generated/mass_bundle_new.md b/docs/generated/mass_bundle_new.md index c465e28..33e3bca 100644 --- a/docs/generated/mass_bundle_new.md +++ b/docs/generated/mass_bundle_new.md @@ -15,7 +15,12 @@ mass bundle new [flags] ### Options ``` - -h, --help help for new + -c, --connections strings Connections and names to add to the bundle - example: massdriver/vpc=network + -d, --description string Description of the new bundle + -h, --help help for new + -n, --name string Name of the new bundle + -o, --output-directory string Directory to output the new bundle (default ".") + -t, --template-type string Name of the bundle template to use ``` ### SEE ALSO