github.com/pluralsh/plural-cli@v0.9.5/pkg/scaffold/creator.go (about)

     1  package scaffold
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/AlecAivazis/survey/v2"
     8  	"github.com/pluralsh/plural-cli/pkg/api"
     9  	"github.com/pluralsh/plural-cli/pkg/utils"
    10  	"github.com/pluralsh/plural-cli/pkg/utils/pathing"
    11  	"helm.sh/helm/v3/pkg/chartutil"
    12  )
    13  
    14  var categories = []string{
    15  	"data",
    16  	"productivity",
    17  	"devops",
    18  	"database",
    19  	"messaging",
    20  	"security",
    21  	"network",
    22  }
    23  
    24  var scaffoldSurvey = []*survey.Question{
    25  	{
    26  		Name:     "application",
    27  		Prompt:   &survey.Input{Message: "Enter the name of your application:"},
    28  		Validate: utils.ValidateAlphaNumeric,
    29  	},
    30  	{
    31  		Name:     "publisher",
    32  		Prompt:   &survey.Input{Message: "Enter the name of your publisher:"},
    33  		Validate: survey.Required,
    34  	},
    35  	{
    36  		Name: "category",
    37  		Prompt: &survey.Select{
    38  			Message: "Enter the category for your application:",
    39  			Options: categories,
    40  		},
    41  		Validate: survey.Required,
    42  	},
    43  	{
    44  		Name:     "postgres",
    45  		Prompt:   &survey.Confirm{Message: "Will your application need a postgres database?"},
    46  		Validate: survey.Required,
    47  	},
    48  	{
    49  		Name:     "ingress",
    50  		Prompt:   &survey.Confirm{Message: "Does your application need an ingress?"},
    51  		Validate: survey.Required,
    52  	},
    53  }
    54  
    55  func ApplicationScaffold(client api.Client) error {
    56  	input := api.ScaffoldInputs{}
    57  	if err := survey.Ask(scaffoldSurvey, &input); err != nil {
    58  		return err
    59  	}
    60  
    61  	scaffolds, err := client.Scaffolds(&input)
    62  	if err != nil {
    63  		return api.GetErrorResponse(err, "Scaffolds")
    64  	}
    65  
    66  	app := input.Application
    67  	helmPath := pathing.SanitizeFilepath(filepath.Join(app, "helm"))
    68  	pwd, err := os.Getwd()
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	if err := os.MkdirAll(helmPath, 0755); err != nil {
    74  		return err
    75  	}
    76  
    77  	if err := os.Chdir(helmPath); err != nil {
    78  		return err
    79  	}
    80  
    81  	if err := createHelm(app); err != nil {
    82  		return err
    83  	}
    84  
    85  	if err := os.Chdir(pathing.SanitizeFilepath(filepath.Join(pwd, app))); err != nil {
    86  		return err
    87  	}
    88  
    89  	for _, scaffold := range scaffolds {
    90  		if err := utils.WriteFile(scaffold.Path, []byte(scaffold.Content)); err != nil {
    91  			return err
    92  		}
    93  	}
    94  
    95  	return nil
    96  }
    97  
    98  func createHelm(name string) error {
    99  	chartname := filepath.Base(name)
   100  	_, err := chartutil.Create(chartname, filepath.Dir(name))
   101  	return err
   102  }