github.com/tilotech/tilores-cli@v0.28.0/cmd/init.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/spf13/cobra"
     5  	"github.com/tilotech/tilores-cli/internal/pkg"
     6  	"github.com/tilotech/tilores-cli/internal/pkg/step"
     7  	"github.com/tilotech/tilores-cli/templates"
     8  )
     9  
    10  var (
    11  	modulePath   string
    12  	deployPrefix string
    13  )
    14  
    15  const (
    16  	goPluginVersion  = "v0.1.1"
    17  	pluginAPIVersion = "v0.14.0"
    18  	gqlgenVersion    = "v0.17.44"
    19  	insightsVersion  = "v0.3.0"
    20  )
    21  
    22  // initCmd represents the init command
    23  var initCmd = &cobra.Command{
    24  	Use:   "init [path]",
    25  	Short: "Initializes a new " + applicationName + " application",
    26  	Long: `Initalize (` + toolName + ` init) will create a new ` + applicationName + ` application and
    27  the appropriate structure.`,
    28  	Run: func(_ *cobra.Command, args []string) {
    29  		err := initializeProject(args)
    30  		cobra.CheckErr(err)
    31  	},
    32  }
    33  
    34  func init() {
    35  	rootCmd.AddCommand(initCmd)
    36  
    37  	initCmd.Flags().StringVarP(&modulePath, "module-path", "m", "", "The go module path for the generated go.mod file, defaults to the project folder name.")
    38  	initCmd.Flags().StringVar(&deployPrefix, "deploy-prefix", "", "The initial prefix for resources created during the deploy phase, defaults to a random eight character string and can be changed later in the generated files.")
    39  }
    40  
    41  func initializeProject(args []string) error {
    42  	path := "."
    43  	if len(args) > 0 {
    44  		path = args[0]
    45  	}
    46  
    47  	finalModulePath := modulePath
    48  
    49  	finalDeployPrefix := deployPrefix
    50  	if finalDeployPrefix == "" {
    51  		finalDeployPrefix = randLowerCaseString(8)
    52  	}
    53  
    54  	variables := templateVariables{
    55  		ApplicationName: applicationName,
    56  		GeneratedMsg:    generatedMsg,
    57  		ModulePath:      &finalModulePath,
    58  		DeployPrefix:    finalDeployPrefix,
    59  	}
    60  
    61  	steps := []step.Step{
    62  		step.InitProjectPath(path, &finalModulePath),
    63  		step.ModInit(&finalModulePath),
    64  		step.RenderTemplates(templates.InitPreGenerate, "init", variables),
    65  		step.GetDependencies([]string{
    66  			"github.com/tilotech/go-plugin@" + goPluginVersion,
    67  			"github.com/tilotech/tilores-plugin-api/dispatcher@" + pluginAPIVersion,
    68  			"github.com/99designs/gqlgen@" + gqlgenVersion,
    69  			"github.com/tilotech/tilores-insights/record@" + insightsVersion,
    70  		}),
    71  		step.ModVendor,
    72  		step.Generate,
    73  		step.RenderTemplates(templates.InitPostGenerate, "init", variables),
    74  		step.ModTidy,
    75  		step.ModVendor,
    76  		step.BuildVerify,
    77  	}
    78  
    79  	err := step.Execute(steps)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	version, err := pkg.LatestUpgradeVersion()
    85  	if err != nil {
    86  		return err
    87  	}
    88  	config := pkg.DefaultConfig()
    89  	config.Version = version
    90  	return pkg.SaveConfig(config)
    91  }
    92  
    93  type templateVariables struct {
    94  	ApplicationName string
    95  	GeneratedMsg    string
    96  	ModulePath      *string
    97  	DeployPrefix    string
    98  }