...

Source file src/github.com/redhat-developer/odo/pkg/testingutil/filesystem/default_fs.go

Documentation: github.com/redhat-developer/odo/pkg/testingutil/filesystem

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  /*
    18  	This package is a FORK of https://github.com/kubernetes/kubernetes/blob/master/pkg/util/filesystem/defaultfs.go
    19  	See above license
    20  */
    21  
    22  package filesystem
    23  
    24  import (
    25  	"io/fs"
    26  	"os"
    27  	"path/filepath"
    28  	"time"
    29  )
    30  
    31  // DefaultFs implements Filesystem using same-named functions from "os" and "io"
    32  type DefaultFs struct{}
    33  
    34  var _ Filesystem = DefaultFs{}
    35  
    36  // Stat via os.Stat
    37  func (DefaultFs) Stat(name string) (os.FileInfo, error) {
    38  	return os.Stat(name)
    39  }
    40  
    41  // Create via os.Create
    42  func (DefaultFs) Create(name string) (File, error) {
    43  	file, err := os.Create(name)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return &defaultFile{file}, nil
    48  }
    49  
    50  // Open via os.Open
    51  func (DefaultFs) Open(name string) (File, error) {
    52  	file, err := os.Open(name)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	return &defaultFile{file}, nil
    58  }
    59  
    60  // OpenFile via os.OpenFile
    61  func (DefaultFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
    62  	file, err := os.OpenFile(name, flag, perm)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	return &defaultFile{file}, nil
    68  }
    69  
    70  // Rename via os.Rename
    71  func (DefaultFs) Rename(oldpath, newpath string) error {
    72  	return os.Rename(oldpath, newpath)
    73  }
    74  
    75  // MkdirAll via os.MkdirAll
    76  func (DefaultFs) MkdirAll(path string, perm os.FileMode) error {
    77  	return os.MkdirAll(path, perm)
    78  }
    79  
    80  // Chtimes via os.Chtimes
    81  func (DefaultFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
    82  	return os.Chtimes(name, atime, mtime)
    83  }
    84  
    85  // RemoveAll via os.RemoveAll
    86  func (DefaultFs) RemoveAll(path string) error {
    87  	return os.RemoveAll(path)
    88  }
    89  
    90  // Remove via os.RemoveAll
    91  func (DefaultFs) Remove(name string) error {
    92  	return os.Remove(name)
    93  }
    94  
    95  // Getwd via os.Getwd
    96  func (DefaultFs) Getwd() (dir string, err error) {
    97  	return os.Getwd()
    98  }
    99  
   100  // ReadFile via ioutil.ReadFile
   101  func (DefaultFs) ReadFile(filename string) ([]byte, error) {
   102  	return os.ReadFile(filename)
   103  }
   104  
   105  // WriteFile via ioutil.WriteFile
   106  func (DefaultFs) WriteFile(filename string, data []byte, perm os.FileMode) error {
   107  	return os.WriteFile(filename, data, perm)
   108  }
   109  
   110  // TempDir via ioutil.TempDir
   111  func (DefaultFs) TempDir(dir, prefix string) (string, error) {
   112  	return os.MkdirTemp(dir, prefix)
   113  }
   114  
   115  // TempFile via ioutil.TempFile
   116  func (DefaultFs) TempFile(dir, prefix string) (File, error) {
   117  	file, err := os.CreateTemp(dir, prefix)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	return &defaultFile{file}, nil
   122  }
   123  
   124  // ReadDir via ioutil.ReadDir
   125  func (DefaultFs) ReadDir(dirname string) ([]os.FileInfo, error) {
   126  	entries, err := os.ReadDir(dirname)
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  	infos := make([]fs.FileInfo, 0, len(entries))
   131  	var info os.FileInfo
   132  	for _, entry := range entries {
   133  		info, err = entry.Info()
   134  		if err != nil {
   135  			return nil, err
   136  		}
   137  		infos = append(infos, info)
   138  	}
   139  	return infos, nil
   140  }
   141  
   142  // Walk via filepath.Walk
   143  func (DefaultFs) Walk(root string, walkFn filepath.WalkFunc) error {
   144  	return filepath.Walk(root, walkFn)
   145  }
   146  
   147  // Chmod via os.Chmod
   148  func (f DefaultFs) Chmod(name string, mode os.FileMode) error {
   149  	return os.Chmod(name, mode)
   150  }
   151  
   152  // defaultFile implements File using same-named functions from "os"
   153  type defaultFile struct {
   154  	file *os.File
   155  }
   156  
   157  var _ File = (*defaultFile)(nil)
   158  
   159  // Name via os.File.Name
   160  func (file *defaultFile) Name() string {
   161  	return file.file.Name()
   162  }
   163  
   164  // Write via os.File.Write
   165  func (file *defaultFile) Write(b []byte) (n int, err error) {
   166  	return file.file.Write(b)
   167  }
   168  
   169  // WriteString via File.WriteString
   170  func (file *defaultFile) WriteString(s string) (int, error) {
   171  	return file.file.WriteString(s)
   172  }
   173  
   174  // Sync via os.File.Sync
   175  func (file *defaultFile) Sync() error {
   176  	return file.file.Sync()
   177  }
   178  
   179  // Close via os.File.Close
   180  func (file *defaultFile) Close() error {
   181  	return file.file.Close()
   182  }
   183  
   184  func (file *defaultFile) Readdir(n int) ([]os.FileInfo, error) {
   185  	return file.file.Readdir(n)
   186  }
   187  
   188  func (file *defaultFile) Read(b []byte) (n int, err error) {
   189  	return file.file.Read(b)
   190  }
   191  
   192  func (file *defaultFile) Chmod(name string, mode os.FileMode) error {
   193  	return file.file.Chmod(mode)
   194  }
   195  

View as plain text