github.com/hashicorp/go-getter/v2@v2.2.2/folder_storage.go (about) 1 package getter 2 3 import ( 4 "context" 5 "crypto/md5" 6 "encoding/hex" 7 "fmt" 8 "os" 9 "path/filepath" 10 ) 11 12 // FolderStorage is an implementation of the Storage interface that manages 13 // modules on the disk. 14 type FolderStorage struct { 15 // StorageDir is the directory where the modules will be stored. 16 StorageDir string 17 } 18 19 // Dir implements Storage.Dir 20 func (s *FolderStorage) Dir(key string) (d string, e bool, err error) { 21 d = s.dir(key) 22 _, err = os.Stat(d) 23 if err == nil { 24 // Directory exists 25 e = true 26 return 27 } 28 if os.IsNotExist(err) { 29 // Directory doesn't exist 30 d = "" 31 e = false 32 err = nil 33 return 34 } 35 36 // An error 37 d = "" 38 e = false 39 return 40 } 41 42 // Get implements Storage.Get 43 func (s *FolderStorage) Get(ctx context.Context, key string, source string, update bool) error { 44 dir := s.dir(key) 45 if !update { 46 if _, err := os.Stat(dir); err == nil { 47 // If the directory already exists, then we're done since 48 // we're not updating. 49 return nil 50 } else if !os.IsNotExist(err) { 51 // If the error we got wasn't a file-not-exist error, then 52 // something went wrong and we should report it. 53 return fmt.Errorf("Error reading module directory: %s", err) 54 } 55 } 56 57 // Get the source. This always forces an update. 58 _, err := Get(ctx, dir, source) 59 return err 60 } 61 62 // dir returns the directory name internally that we'll use to map to 63 // internally. 64 func (s *FolderStorage) dir(key string) string { 65 sum := md5.Sum([]byte(key)) 66 return filepath.Join(s.StorageDir, hex.EncodeToString(sum[:])) 67 }