func getHCLInfo()

in pkg/modulereader/hcl_utils.go [35:88]


func getHCLInfo(source string) (ModuleInfo, error) {
	var module *tfconfig.Module
	ret := ModuleInfo{}

	if sourcereader.IsEmbeddedPath(source) {
		wrapFS := tfconfig.WrapFS(sourcereader.ModuleFS)
		if !tfconfig.IsModuleDirOnFilesystem(wrapFS, source) {
			return ret, fmt.Errorf("source is not a terraform or packer module: %s", source)
		}
		module, _ = tfconfig.LoadModuleFromFilesystem(wrapFS, source)
	} else {
		fileInfo, err := os.Stat(source)
		if os.IsNotExist(err) {
			return ret, fmt.Errorf("source to module does not exist: %s", source)
		}
		if err != nil {
			return ret, fmt.Errorf("failed to read source of module: %s", source)
		}
		if !fileInfo.IsDir() {
			return ret, fmt.Errorf("source of module must be a directory: %s", source)
		}
		if !tfconfig.IsModuleDir(source) {
			return ret, fmt.Errorf("source is not a terraform or packer module: %s", source)
		}
		module, _ = tfconfig.LoadModule(source)
	}

	var vars []VarInfo
	var outs []OutputInfo
	for _, v := range module.Variables {
		ty, err := GetCtyType(v.Type)
		if err != nil {
			return ModuleInfo{}, fmt.Errorf("failed to parse type of variable %q: %w", v.Name, err)
		}
		vInfo := VarInfo{
			Name:        v.Name,
			Type:        ty,
			Description: v.Description,
			Default:     v.Default,
			Required:    v.Required,
		}
		vars = append(vars, vInfo)
	}
	ret.Inputs = vars
	for _, v := range module.Outputs {
		oInfo := OutputInfo{
			Name:        v.Name,
			Description: v.Description,
		}
		outs = append(outs, oInfo)
	}
	ret.Outputs = outs
	return ret, nil
}