func loadAndModelResources()

in tpgtools/main.go [148:225]


func loadAndModelResources() (map[Version][]*Resource, map[Version][]*ProductMetadata, error) {
	flag.Parse()
	if tPath == nil || *tPath == "" {
		return nil, nil, errors.New("no path specified")
	}

	dirs, err := ioutil.ReadDir(*tPath)
	if err != nil {
		return nil, nil, err
	}
	resources := make(map[Version][]*Resource)
	products := make(map[Version][]*ProductMetadata)

	for _, version := range allVersions() {
		resources[version] = make([]*Resource, 0)
		for _, v := range dirs {
			// skip flat files- we're looking for service directory
			if !v.IsDir() {
				continue
			}

			var overrideFiles []os.FileInfo
			var packagePath Filepath
			if version == GA_VERSION {
				// GA has no separate directory
				packagePath = Filepath(v.Name())
			} else {
				packagePath = Filepath(path.Join(v.Name(), version.V))
			}

			overrideFiles, err = ioutil.ReadDir(path.Join(*tPath, string(packagePath)))
			var newResources []*Resource

			// keep track of the last document in a service- we need one for the product later
			var document *openapi.Document
			for _, resourceFile := range overrideFiles {
				if resourceFile.IsDir() || resourceFile.Name() == "tpgtools_product.yaml" {
					continue
				}

				document = &openapi.Document{}
				b := directory.Services().GetResource(version.V, v.Name(), stripExt(resourceFile.Name()))
				if b == nil {
					return nil, nil, fmt.Errorf("could not find resource in DCL directory: %q in %q at %q", stripExt(resourceFile.Name()), packagePath, version.V)
				}

				err = yaml.Unmarshal(b.Bytes(), document)
				if err != nil {
					return nil, nil, err
				}
				// TODO: the openapi library cannot handle extensions except in the Schema object.  If this is ever added,
				// this workaround can be removed.
				if err := addInfoExtensionsToSchemaObjects(document, b.Bytes()); err != nil {
					return nil, nil, err
				}

				overrides := loadOverrides(packagePath, resourceFile.Name())
				if len(overrides) > 0 {
					glog.Infof("Loaded overrides for %s", resourceFile.Name())
				}

				newResources = append(newResources, createResourcesFromDocumentAndOverrides(document, overrides, packagePath, version)...)
			}

			// if we found no resources, just keep going
			if document == nil {
				continue
			}

			products[version] = append(products[version], GetProductMetadataFromDocument(document, packagePath))
			glog.Infof("Loaded product %s", packagePath)

			resources[version] = append(resources[version], newResources...)
		}
	}

	return resources, products, nil
}