github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/utils/pip/dependencies/cache.go (about)

     1  package dependencies
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/jfrog/jfrog-client-go/artifactory/buildinfo"
     6  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
     7  	"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  )
    12  
    13  const cacheLatestVersion = 1
    14  
    15  type DependenciesCache struct {
    16  	Version int                              `json:"version,omitempty"`
    17  	DepsMap map[string]*buildinfo.Dependency `json:"dependencies,omitempty"`
    18  }
    19  
    20  // Reads the json cache file of recent used project's dependencies,  and converts it into a map of
    21  // Key: dependency_name Value: dependency's struct with all relevant information.
    22  // If cache file does not exist -> return nil, nil.
    23  // If error occurred, return error.
    24  func GetProjectDependenciesCache() (*DependenciesCache, error) {
    25  	cache := new(DependenciesCache)
    26  	cacheFilePath, exists, err := getCacheFilePath()
    27  	if errorutils.CheckError(err) != nil || !exists {
    28  		return nil, err
    29  	}
    30  	jsonFile, err := os.Open(cacheFilePath)
    31  	if errorutils.CheckError(err) != nil {
    32  		return nil, err
    33  	}
    34  	defer jsonFile.Close()
    35  	byteValue, err := ioutil.ReadAll(jsonFile)
    36  	if errorutils.CheckError(err) != nil {
    37  		return nil, err
    38  	}
    39  	err = json.Unmarshal(byteValue, cache)
    40  	if errorutils.CheckError(err) != nil {
    41  		return nil, err
    42  	}
    43  
    44  	return cache, nil
    45  }
    46  
    47  // Receives map of all current project's dependencies information.
    48  // The map contains the dependencies retrieved from Artifactory as well as those read from cache.
    49  // Writes the updated project's dependencies cache with all current dependencies.
    50  func UpdateDependenciesCache(updatedMap map[string]*buildinfo.Dependency) error {
    51  	updatedCache := DependenciesCache{Version: cacheLatestVersion, DepsMap: updatedMap}
    52  	content, err := json.Marshal(&updatedCache)
    53  	if err != nil {
    54  		return errorutils.CheckError(err)
    55  	}
    56  	cacheFilePath, _, err := getCacheFilePath()
    57  	if err != nil {
    58  		return errorutils.CheckError(err)
    59  	}
    60  
    61  	cacheFile, err := os.Create(cacheFilePath)
    62  	if err != nil {
    63  		return errorutils.CheckError(err)
    64  	}
    65  	defer cacheFile.Close()
    66  
    67  	_, err = cacheFile.Write(content)
    68  	if err != nil {
    69  		return errorutils.CheckError(err)
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  // Return required dependency from cache.
    76  // If dependency does not exist, return nil.
    77  // dependencyName - Name of dependency (lowercase package name).
    78  func (cache DependenciesCache) GetDependency(dependencyName string) *buildinfo.Dependency {
    79  	dependency, ok := cache.DepsMap[dependencyName]
    80  	if !ok {
    81  		return nil
    82  	}
    83  
    84  	return dependency
    85  }
    86  
    87  // Cache file will be located in the ./.jfrog/projects/deps.cache.json
    88  func getCacheFilePath() (cacheFilePath string, exists bool, err error) {
    89  	projectsDirPath, err := os.Getwd()
    90  	if errorutils.CheckError(err) != nil {
    91  		return "", false, err
    92  	}
    93  	projectsDirPath = filepath.Join(projectsDirPath, ".jfrog", "projects")
    94  	err = fileutils.CreateDirIfNotExist(projectsDirPath)
    95  	if err != nil {
    96  		return "", false, err
    97  	}
    98  	cacheFilePath = filepath.Join(projectsDirPath, "deps.cache.json")
    99  	exists, err = fileutils.IsFileExists(cacheFilePath, false)
   100  
   101  	return
   102  }