github.com/SAP/cloud-mta-build-tool@v1.2.27/cmd/init.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "os" 6 "time" 7 8 "github.com/spf13/cobra" 9 10 "github.com/SAP/cloud-mta-build-tool/internal/artifacts" 11 "github.com/SAP/cloud-mta-build-tool/internal/exec" 12 "github.com/SAP/cloud-mta-build-tool/internal/tpl" 13 ) 14 15 const ( 16 makefile = "Makefile.mta" 17 ) 18 19 // flags of init command 20 var initCmdSrc string 21 var initCmdTrg string 22 var initCmdExtensions []string 23 var initCmdMode string 24 25 // flags of build command 26 var mbtCmdCLI string 27 var buildCmdSrc string 28 var buildCmdTrg string 29 var buildCmdExtensions []string 30 var buildCmdMtar = "*" 31 var buildCmdPlatform string 32 var buildCmdStrict bool 33 var buildCmdMode string 34 var buildCmdJobs int 35 var buildCmdOutputSync bool 36 var buildCmdKeepMakefile bool 37 var buildCmdSBomFilePath string 38 39 func init() { 40 // set flags for init command 41 initCmd.Flags().StringVarP(&initCmdSrc, "source", "s", "", "The path to the MTA project; the current path is set as default") 42 initCmd.Flags().StringVarP(&initCmdTrg, "target", "t", "", "The path to the folder in which the Makefile is generated; the current path is set as default") 43 initCmd.Flags().StringSliceVarP(&initCmdExtensions, "extensions", "e", nil, "The MTA extension descriptors") 44 initCmd.Flags().StringVarP(&initCmdMode, "mode", "m", "", `The mode of the Makefile generation; supported values: "default" and "verbose"`) 45 _ = initCmd.Flags().MarkHidden("mode") 46 initCmd.Flags().BoolP("help", "h", false, `Displays detailed information about the "init" command`) 47 48 // set flags of build command 49 buildCmd.Flags().StringVarP(&buildCmdSrc, "source", "s", "", "The path to the MTA project; the current path is set as default") 50 buildCmd.Flags().StringVarP(&buildCmdTrg, "target", "t", "", `The path to the folder in which the MTAR file is created; the path to the "mta_archives" subfolder of the current folder is set as default`) 51 buildCmd.Flags().StringSliceVarP(&buildCmdExtensions, "extensions", "e", nil, "The MTA extension descriptors") 52 buildCmd.Flags().StringVarP(&buildCmdMtar, "mtar", "", "", "The file name of the generated archive file") 53 buildCmd.Flags().StringVarP(&buildCmdPlatform, "platform", "p", "cf", `The deployment platform; supported platforms: "cf", "xsa", "neo"`) 54 buildCmd.Flags().BoolVarP(&buildCmdStrict, "strict", "", true, `If set to true, duplicated fields and fields not defined in the "mta.yaml" schema are reported as errors; if set to false, they are reported as warnings`) 55 buildCmd.Flags().StringVarP(&buildCmdMode, "mode", "m", "", `(beta) If set to "verbose", Make can run build jobs simultaneously.`) 56 buildCmd.Flags().IntVarP(&buildCmdJobs, "jobs", "j", 0, fmt.Sprintf(`(beta) The number of Make jobs to be executed simultaneously. The default value is the number of available CPUs (maximum %d). Used only in "verbose" mode.`, artifacts.MaxMakeParallel)) 57 buildCmd.Flags().BoolVarP(&buildCmdOutputSync, "output-sync", "o", false, `(beta) Groups the output of each Make job and prints it when the job is complete. Used only in "verbose" mode.`) 58 buildCmd.Flags().BoolVarP(&buildCmdKeepMakefile, "keep-makefile", "k", false, `Don't remove the generated Makefile after the build ends.`) 59 buildCmd.Flags().StringVarP(&buildCmdSBomFilePath, "sbom-file-path", "b", "", `(beta) The path of SBOM file, relative or absoluted; if relative path, it is relative to MTA project root; if value is empty, SBOM file will not be generated.`) 60 _ = buildCmd.Flags().MarkHidden("keep-makefile") 61 // _ = buildCmd.Flags().MarkHidden("sbom-file-path") 62 buildCmd.Flags().BoolP("help", "h", false, `Displays detailed information about the "build" command`) 63 } 64 65 var initCmd = &cobra.Command{ 66 Use: "init", 67 Short: "Generates a GNU Make manifest file that describes the build process of the MTA project", 68 Long: "Generates a GNU Make manifest file that describes the build process of the MTA project", 69 Args: cobra.NoArgs, 70 Run: func(cmd *cobra.Command, args []string) { 71 // Generate build script 72 err := tpl.ExecuteMake(initCmdSrc, initCmdTrg, initCmdExtensions, makefile, initCmdMode, os.Getwd, true) 73 logError(err) 74 }, 75 } 76 77 // Execute MTA project build 78 var buildCmd = &cobra.Command{ 79 Use: "build", 80 Short: "Builds the project modules and generates an MTA archive according to the MTA development descriptor (mta.yaml)", 81 Long: "Builds the project modules and generates an MTA archive according to the MTA development descriptor (mta.yaml)", 82 Args: cobra.NoArgs, 83 RunE: func(cmd *cobra.Command, args []string) error { 84 // Generate temp Makefile with unique id 85 makefileTmp := "Makefile_" + time.Now().Format("20060102150405") + ".mta" 86 // Generate build script 87 // We want to use the current mbt and not the default (globally installed) mbt from the path when running mbt build, to allow users to run mbt build without the need to set the path. 88 // This also supports using multiple versions of the mbt. 89 // However, in some environments we might want to always use the default mbt from the path. This can be set by using environment variable MBT_USE_DEFAULT. 90 useDefaultMbt := os.Getenv("MBT_USE_DEFAULT") == "true" 91 // Note: we can only use the non-default mbt (i.e. the current executable name) from inside the command itself because if this function runs from other places like tests it won't point to the MBT 92 err := artifacts.ExecBuild(makefileTmp, buildCmdSrc, buildCmdTrg, buildCmdExtensions, buildCmdMode, buildCmdMtar, buildCmdPlatform, buildCmdStrict, buildCmdJobs, buildCmdOutputSync, os.Getwd, exec.Execute, useDefaultMbt, buildCmdKeepMakefile, buildCmdSBomFilePath) 93 // output err info to stdout 94 logError(err) 95 return err 96 }, 97 SilenceUsage: true, 98 }