本文整理汇总了Golang中github.com/jfindley/testfs.FileSystem类的典型用法代码示例。如果您正苦于以下问题:Golang FileSystem类的具体用法?Golang FileSystem怎么用?Golang FileSystem使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FileSystem类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: writeVersion
func (e *exportedImage) writeVersion(fs testfs.FileSystem) (err error) {
fp := e.VersionPath
f, err := fs.Create(fp)
if err != nil {
return
}
defer f.Close()
_, err = f.WriteString("1.0")
if err != nil {
return
}
return
}
示例2: writeData
func (e *exportedImage) writeData(fs testfs.FileSystem, data []byte) (err error) {
fp := e.LayerTarPath
f, err := fs.Create(fp)
if err != nil {
return
}
defer f.Close()
_, err = f.Write(data)
if err != nil {
return
}
return err
}
示例3: touch
// touch a file. Same as system touch.
func (f *File) touch(fs testfs.FileSystem, r core.ResponseWriter) {
file, err := fs.OpenFile(f.path, os.O_CREATE|os.O_WRONLY, f.mode)
if err != nil {
r.Message("error", err.Error())
r.Success(false)
return
}
err = file.Close()
if err != nil {
r.Message("error", err.Error())
r.Success(false)
return
}
r.Message("info", "touched ", f.path)
r.Success(true)
r.Changed(true)
return
}
示例4: writeJSON
func (e *exportedImage) writeJSON(fs testfs.FileSystem) (err error) {
fp := e.JSONPath
f, err := fs.Create(fp)
if err != nil {
return
}
defer f.Close()
jb, err := json.Marshal(e.LayerConfig)
if err != nil {
return
}
_, err = f.WriteString(string(jb))
if err != nil {
return
}
return err
}
示例5: set
// set sets the contents of a file.
func (f *File) set(fs testfs.FileSystem, r core.ResponseWriter) {
var (
file testfs.File
err error
)
// If we have a valid checksum for the input data and the file exists, read it
// and avoid modifying the file if possible.
if f.matching(fs) {
r.Success(true)
r.Changed(false)
r.Message("info", f.path, " unchanged")
return
}
file, err = fs.OpenFile(f.path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, f.mode)
if err != nil {
r.Message("error", err.Error())
r.Success(false)
return
}
err = bufferedWrite(f.data, file)
if err != nil {
r.Message("error", err.Error())
r.Success(false)
return
}
err = file.Close()
if err != nil {
r.Message("error", err.Error())
r.Success(false)
return
}
r.Message("info", "set content of ", f.path)
r.Success(true)
r.Changed(true)
return
}
示例6: archive
func archive(fs testfs.FileSystem) (out []byte, err error) {
buf := new(bytes.Buffer)
t := tar.NewWriter(buf)
origDir, err := fs.Getwd()
if err != nil {
return
}
// Start at the root of the fs
fs.Chdir("/")
err = walk(fs, t)
if err != nil {
return
}
err = fs.Chdir(origDir)
if err != nil {
return
}
err = t.Close()
return buf.Bytes(), err
}
示例7: matching
// matching returns true if a file matches the checksum, false otherwise
func (f *File) matching(fs testfs.FileSystem) bool {
if f.sum == nil || len(f.sum) == 0 {
return false
}
_, err := fs.Stat(f.path)
if err != nil {
return false
}
file, err := fs.OpenFile(f.path, os.O_RDONLY, 0)
if err != nil {
return false
}
data, err := ioutil.ReadAll(file)
if err != nil {
return false
}
if bytes.Compare(f.sum, data) == 0 {
return true
}
return false
}
示例8: remove
// remove removes a file.
func (f *File) remove(fs testfs.FileSystem, r core.ResponseWriter) {
err := fs.Remove(f.path)
switch {
// Don't error if the file is already removed
case os.IsNotExist(err):
r.Success(true)
r.Changed(false)
r.Message("info", f.path, " already removed")
case err == nil:
r.Success(true)
r.Changed(true)
r.Message("info", f.path, " removed")
default:
r.Message("error", err.Error())
r.Success(false)
}
return
}
示例9: walk
func walk(fs testfs.FileSystem, t *tar.Writer) (err error) {
cwd, err := fs.Getwd()
if err != nil {
return
}
f, err := fs.OpenFile(cwd, os.O_RDONLY, 0)
if err != nil {
return
}
fi, err := f.Readdir(-1)
if err != nil {
return
}
for _, file := range fi {
// Add a new file to the archive
var hdr *tar.Header
hdr, err = tarHdr(file)
if err != nil {
return
}
// Make header name fully qualified
if cwd[len(cwd)-1] == '/' {
hdr.Name = cwd + hdr.Name
} else {
hdr.Name = cwd + "/" + hdr.Name
}
// Strip leading slashes
if hdr.Name[0] == '/' {
hdr.Name = hdr.Name[1:]
}
err = t.WriteHeader(hdr)
if err != nil {
return
}
// Write the file data to the archive
if !file.IsDir() && file.Size() > 0 {
var target testfs.File
target, err = fs.Open(file.Name())
if err != nil {
return
}
_, err = io.Copy(t, target)
if err != nil {
return
}
}
// Close the file in the archive
err = t.Flush()
if err != nil {
return
}
// If a directory, recurse into it
if file.IsDir() {
err = fs.Chdir(file.Name())
if err != nil {
return
}
return walk(fs, t)
}
}
return
}