func SwapModules()

in infra/module-swapper/cmd/swap.go [385:431]


func SwapModules(rootPath, moduleRegistrySuffix, moduleRegistryPrefix, subModulesDir, examplesDir string, restore bool) {
	rootPath = filepath.Clean(rootPath)
	moduleName, foundRegistryPrefix, err := getModuleNameRegistry(rootPath)
	if err != nil && moduleRegistryPrefix == "" {
		log.Printf("failed to get module name and registry: %v", err)
		return
	}

	if moduleRegistryPrefix != "" {
		foundRegistryPrefix = moduleRegistryPrefix
	}

	// add root module to slice of localModules
	localModules = append(localModules, LocalTerraformModule{moduleName, rootPath, fmt.Sprintf("%s/%s/%s", foundRegistryPrefix, moduleName, moduleRegistrySuffix)})
	examplesPath := fmt.Sprintf("%s/%s", rootPath, examplesDir)
	subModulesPath := fmt.Sprintf("%s/%s", rootPath, subModulesDir)

	// add submodules, if any to localModules
	submods := findSubModules(subModulesPath, localModules[0].ModuleFQN)
	localModules = append(localModules, submods...)

	// find all TF files in examples dir to process
	exampleTFFiles := getTFFiles(examplesPath)
	for _, TFFilePath := range exampleTFFiles {
		file, err := os.ReadFile(TFFilePath)
		if err != nil {
			log.Printf("Error reading file: %v", err)
		}

		var newFile []byte
		if restore {
			newFile, err = localToRemote(file, TFFilePath)
		} else {
			newFile, err = remoteToLocal(file, TFFilePath)
		}
		if err != nil {
			log.Printf("Error processing file: %v", err)
		}

		if newFile != nil {
			err = os.WriteFile(TFFilePath, newFile, 0644)
			if err != nil {
				log.Printf("Error writing file: %v", err)
			}
		}
	}
}