func generate()

in cli/bpmetadata/cmd.go [67:138]


func generate(cmd *cobra.Command, args []string) error {
	wdPath, err := os.Getwd()
	if err != nil {
		return fmt.Errorf("error getting working dir: %w", err)
	}

	// validate metadata if there is an argument passed into the command
	if mdFlags.validate {
		if err := validateMetadata(mdFlags.path, wdPath); err != nil {
			return err
		}

		return nil
	}

	currBpPath := mdFlags.path
	if !path.IsAbs(mdFlags.path) {
		currBpPath = path.Join(wdPath, mdFlags.path)
	}

	var allBpPaths []string
	_, err = os.Stat(path.Join(currBpPath, readmeFileName))

	// throw an error and exit if root level readme.md doesn't exist
	if err != nil {
		return fmt.Errorf("top-level module does not have a readme: %w", err)
	}

	allBpPaths = append(allBpPaths, currBpPath)
	var errors []string

	// if nested, check if modules/ exists and create paths
	// for submodules
	if mdFlags.nested {
		modulesPathforBp := path.Join(currBpPath, modulesPath)
		_, err = os.Stat(modulesPathforBp)
		if os.IsNotExist(err) {
			Log.Info("sub-modules do not exist for this blueprint")
		} else {
			moduleDirs, err := util.WalkTerraformDirs(modulesPathforBp)
			if err != nil {
				errors = append(errors, err.Error())
			} else {
				allBpPaths = append(allBpPaths, moduleDirs...)
			}
		}
	}

	for _, modPath := range allBpPaths {
		// check if module path has readme.md
		_, err := os.Stat(path.Join(modPath, readmeFileName))

		// log info if a sub-module doesn't have a readme.md and continue
		if err != nil {
			Log.Info("skipping metadata for sub-module identified as an internal module", "Path:", modPath)
			continue
		}

		err = generateMetadataForBpPath(modPath)
		if err != nil {
			e := fmt.Sprintf("path: %s\n %s", modPath, err.Error())
			errors = append(errors, e)
		}
	}

	if len(errors) > 0 {
		return fmt.Errorf("%s", strings.Join(errors, "\n"))
	}

	Log.Info("metadata generated successfully")
	return nil
}