func GetIndexFromLocalDir()

in coverage/index.go [27:75]


func GetIndexFromLocalDir(swaggerRepo, indexFilePath string) (*azidx.Index, error) {
	if indexCache != nil {
		return indexCache, nil
	}

	if indexFilePath != "" {
		if _, err := os.Stat(indexFilePath); err == nil {
			byteValue, _ := os.ReadFile(indexFilePath)

			var index azidx.Index
			if err := json.Unmarshal(byteValue, &index); err != nil {
				return nil, fmt.Errorf("unmarshal index file: %+v", err)
			}
			indexCache = &index

			logrus.Infof("load index from cache file %s", indexFilePath)

			return indexCache, nil
		}
	}

	logrus.Infof("building index from from local swagger %s, it might take several minutes", swaggerRepo)
	index, err := azidx.BuildIndex(swaggerRepo, "")
	if err != nil {
		logrus.Error(fmt.Sprintf("failed to build index: %+v", err))
		return nil, err
	}
	logrus.Infof("index successfully built on commit %+v", index.Commit)

	indexCache = index

	if indexFilePath != "" {
		jsonBytes, err := json.Marshal(&index)
		if err != nil {
			logrus.Warningf("failed to marshal index: %+v", err)
			return index, nil
		}

		err = os.WriteFile(indexFilePath, jsonBytes, 0644)
		if err != nil {
			logrus.Warningf("failed to write index cache file %s: %+v", indexFilePath, err)
			return index, nil
		}

		logrus.Infof("index successfully saved to cache file %s", indexFilePath)
	}

	return index, nil
}