| 1 |
// Copyright (c) HashiCorp, Inc. |
| 2 |
// SPDX-License-Identifier: MPL-2.0 |
| 3 |
|
| 4 |
package tfconfig |
| 5 |
|
| 6 |
import ( |
| 7 |
"io/fs" |
| 8 |
"io/ioutil" |
| 9 |
"os" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
// FS is an interface used by [LoadModuleFromFilesystem]. |
| 14 |
// |
| 15 |
// Unfortunately this package implemented a draft version of the io/fs.FS |
| 16 |
// API before it was finalized and so this interface is not compatible with |
| 17 |
// the final design. To use this package with the final filesystem API design, |
| 18 |
// use [WrapFS] to wrap a standard filesystem implementation so that it |
| 19 |
// implements this interface. |
| 20 |
type FS interface { |
| 21 |
Open(name string) (File, error) |
| 22 |
ReadFile(name string) ([]byte, error) |
| 23 |
ReadDir(dirname string) ([]os.FileInfo, error) |
| 24 |
} |
| 25 |
|
| 26 |
// File represents an open file in [FS]. |
| 27 |
type File interface { |
| 28 |
Stat() (os.FileInfo, error) |
| 29 |
Read([]byte) (int, error) |
| 30 |
Close() error |
| 31 |
} |
| 32 |
|
| 33 |
// wrapFS is a rather regrettable adapter from the standard library filesystem |
| 34 |
// interfaces to the one we have designed in this package, since we adopted |
| 35 |
// a draft of that API before it was finalized and the final is incompatible. |
| 36 |
type wrapFS struct { |
| 37 |
wrapped fs.FS |
| 38 |
} |
| 39 |
|
| 40 |
// WrapFS wraps a standard library filesystem implementation so that it |
| 41 |
// implements this package's own (slightly-incompatible) interface [FS]. |
| 42 |
func WrapFS(wrapped fs.FS) FS { |
| 43 |
return wrapFS{wrapped} |
| 44 |
} |
| 45 |
|
| 46 |
func (wfs wrapFS) Open(name string) (File, error) { |
| 47 |
return wfs.wrapped.Open(name) |
| 48 |
} |
| 49 |
|
| 50 |
func (wfs wrapFS) ReadFile(name string) ([]byte, error) { |
| 51 |
return fs.ReadFile(wfs.wrapped, name) |
| 52 |
} |
| 53 |
|
| 54 |
func (wfs wrapFS) ReadDir(dirname string) ([]os.FileInfo, error) { |
| 55 |
entries, err := fs.ReadDir(wfs.wrapped, dirname) |
| 56 |
var ret []os.FileInfo |
| 57 |
if len(entries) != 0 { |
| 58 |
ret = make([]os.FileInfo, len(entries)) |
| 59 |
for i, entry := range entries { |
| 60 |
ret[i] = wrapFileInfoDirEntry{entry} |
| 61 |
} |
| 62 |
} |
| 63 |
return ret, err |
| 64 |
} |
| 65 |
|
| 66 |
type wrapFileInfoDirEntry struct { |
| 67 |
wrapped fs.DirEntry |
| 68 |
} |
| 69 |
|
| 70 |
func (d wrapFileInfoDirEntry) IsDir() bool { |
| 71 |
return d.wrapped.IsDir() |
| 72 |
} |
| 73 |
|
| 74 |
func (d wrapFileInfoDirEntry) ModTime() time.Time { |
| 75 |
// this package doesn't actually care about modification times, |
| 76 |
// so we don't need to implement this. |
| 77 |
panic("unimplemented") |
| 78 |
} |
| 79 |
|
| 80 |
func (d wrapFileInfoDirEntry) Mode() fs.FileMode { |
| 81 |
// this package doesn't actually care about file modes, |
| 82 |
// so we don't need to implement this. |
| 83 |
panic("unimplemented") |
| 84 |
} |
| 85 |
|
| 86 |
func (d wrapFileInfoDirEntry) Name() string { |
| 87 |
return d.wrapped.Name() |
| 88 |
} |
| 89 |
|
| 90 |
func (d wrapFileInfoDirEntry) Size() int64 { |
| 91 |
// this package doesn't actually care about file sizes, |
| 92 |
// so we don't need to implement this. |
| 93 |
panic("unimplemented") |
| 94 |
} |
| 95 |
|
| 96 |
func (d wrapFileInfoDirEntry) Sys() any { |
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
type osFs struct{} |
| 101 |
|
| 102 |
func (fs *osFs) Open(name string) (File, error) { |
| 103 |
return os.Open(name) |
| 104 |
} |
| 105 |
|
| 106 |
func (fs *osFs) ReadFile(name string) ([]byte, error) { |
| 107 |
return ioutil.ReadFile(name) |
| 108 |
} |
| 109 |
|
| 110 |
func (fs *osFs) ReadDir(dirname string) ([]os.FileInfo, error) { |
| 111 |
return ioutil.ReadDir(dirname) |
| 112 |
} |
| 113 |
|
| 114 |
// NewOsFs provides a basic implementation of FS for an OS filesystem |
| 115 |
func NewOsFs() FS { |
| 116 |
return &osFs{} |
| 117 |
} |