This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from weaveworks/rawstorage
Factor out RawStorage from Storage
- Loading branch information
Showing
14 changed files
with
1,265 additions
and
1,168 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
/* | ||
Note: This file is autogenerated! Do not edit it manually! | ||
Edit client_image_template.go instead, and run | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
/* | ||
Note: This file is autogenerated! Do not edit it manually! | ||
Edit client_kernel_template.go instead, and run | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
/* | ||
Note: This file is autogenerated! Do not edit it manually! | ||
Edit client_vm_template.go instead, and run | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package storage | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/weaveworks/ignite/pkg/constants" | ||
"github.com/weaveworks/ignite/pkg/util" | ||
) | ||
|
||
type RawStorage interface { | ||
Read(key string) ([]byte, error) | ||
Exists(key string) bool | ||
Write(key string, content []byte) error | ||
Delete(key string) error | ||
List(directory string) ([]string, error) | ||
} | ||
|
||
func NewDefaultRawStorage(dir string) RawStorage { | ||
return &DefaultRawStorage{ | ||
dir: dir, | ||
} | ||
} | ||
|
||
type DefaultRawStorage struct { | ||
dir string | ||
} | ||
|
||
func (r *DefaultRawStorage) realPath(key string) string { | ||
// The "/" prefix is enforced | ||
if !strings.HasPrefix(key, "/") { | ||
key = "/" + key | ||
} | ||
// If a top-level kind is described, and not a file, return the kind directory path | ||
if len(strings.Split(key, "/")) == 2 { | ||
return path.Join(r.dir, key) | ||
} | ||
// Return the file location, with the metadata.json suffix | ||
return path.Join(r.dir, key, constants.METADATA) | ||
} | ||
|
||
func (r *DefaultRawStorage) Read(key string) ([]byte, error) { | ||
file := r.realPath(key) | ||
return ioutil.ReadFile(file) | ||
} | ||
|
||
func (r *DefaultRawStorage) Exists(key string) bool { | ||
file := r.realPath(key) | ||
return util.FileExists(file) | ||
} | ||
|
||
func (r *DefaultRawStorage) Write(key string, content []byte) error { | ||
file := r.realPath(key) | ||
|
||
// Create the underlying directories if they do not exist already | ||
if !r.Exists(key) { | ||
if err := os.MkdirAll(path.Dir(file), 0755); err != nil { | ||
return err | ||
} | ||
} | ||
return ioutil.WriteFile(file, content, 0644) | ||
} | ||
|
||
func (r *DefaultRawStorage) Delete(key string) error { | ||
file := r.realPath(key) | ||
dir := path.Dir(file) | ||
return os.RemoveAll(dir) | ||
} | ||
|
||
func (r *DefaultRawStorage) List(parentKey string) ([]string, error) { | ||
realPath := r.realPath(parentKey) | ||
entries, err := ioutil.ReadDir(realPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result := []string{} | ||
for _, entry := range entries { | ||
entryPath := path.Join(parentKey, entry.Name()) | ||
result = append(result, entryPath) | ||
} | ||
return result, nil | ||
} |
Oops, something went wrong.