-
Notifications
You must be signed in to change notification settings - Fork 1.5k
pkg/asset: Introduce Load() into the Asset interface that loads assets (from disk) #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ const ( | |
| // MetadataFilename is name of the file where clustermetadata is stored. | ||
| MetadataFilename = "metadata.json" | ||
|
|
||
| stateFileName = "terraform.state" | ||
| stateFileName = "terraform.tfstate" | ||
| ) | ||
|
|
||
| // Cluster uses the terraform executable to launch a cluster | ||
|
|
@@ -67,7 +67,7 @@ func (c *Cluster) Generate(parents asset.Parents) (err error) { | |
| return errors.Wrap(err, "failed to write terraform.tfvars file") | ||
| } | ||
|
|
||
| platform := terraformVariables.Platform | ||
| platform := installConfig.Config.Platform.Name() | ||
| if err := data.Unpack(tmpDir, platform); err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -155,3 +155,12 @@ func (c *Cluster) Generate(parents asset.Parents) (err error) { | |
| func (c *Cluster) Files() []*asset.File { | ||
| return c.FileList | ||
| } | ||
|
|
||
| // Load returns error if the tfstate file is already on-disk, because we want to | ||
|
||
| // prevent user from accidentally re-launching the cluster. | ||
| func (c *Cluster) Load(f asset.FileFetcher) (found bool, err error) { | ||
| if f.FetchByName(stateFileName) != nil { | ||
| return true, fmt.Errorf("%q already exisits", stateFileName) | ||
| } | ||
| return false, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package asset | ||
|
|
||
| import ( | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
| "regexp" | ||
| "sort" | ||
| ) | ||
|
|
||
| // FileFetcher fetches the asset files from disk. | ||
| type FileFetcher interface { | ||
| // FetchByName returns the file with the given name. | ||
| FetchByName(string) *File | ||
| // FetchByPattern returns the files whose name match the given regexp. | ||
| FetchByPattern(*regexp.Regexp) []*File | ||
| } | ||
|
|
||
| type fileFetcher struct { | ||
| onDiskAssets map[string][]byte | ||
| } | ||
|
|
||
| func newFileFetcher(clusterDir string) (*fileFetcher, error) { | ||
| fileMap := make(map[string][]byte) | ||
|
|
||
| // Don't bother if the clusterDir is not created yet because that | ||
| // means there's no assets generated yet. | ||
| _, err := os.Stat(clusterDir) | ||
| if err != nil && os.IsNotExist(err) { | ||
| return &fileFetcher{}, nil | ||
| } | ||
|
|
||
| if err := filepath.Walk(clusterDir, func(path string, info os.FileInfo, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if info.IsDir() { | ||
| return nil | ||
| } | ||
|
|
||
| filename, err := filepath.Rel(clusterDir, path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| data, err := ioutil.ReadFile(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fileMap[filename] = data | ||
| return nil | ||
| }); err != nil { | ||
| return nil, err | ||
| } | ||
| return &fileFetcher{onDiskAssets: fileMap}, nil | ||
| } | ||
|
|
||
| // FetchByName returns the file with the given name. | ||
| func (f *fileFetcher) FetchByName(name string) *File { | ||
| data, ok := f.onDiskAssets[name] | ||
| if !ok { | ||
| return nil | ||
| } | ||
| return &File{Filename: name, Data: data} | ||
| } | ||
|
|
||
| // FetchByPattern returns the files whose name match the given regexp. | ||
| func (f *fileFetcher) FetchByPattern(re *regexp.Regexp) []*File { | ||
| var files []*File | ||
|
|
||
| for filename, data := range f.onDiskAssets { | ||
| if re.MatchString(filename) { | ||
| files = append(files, &File{ | ||
| Filename: filename, | ||
| Data: data, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| sort.Slice(files, func(i, j int) bool { return files[i].Filename < files[j].Filename }) | ||
| return files | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we ensure here that the target assets are both loadable and writable?