Documentation
¶
Overview ¶
Package files provides interfaces for reading and writing files.
Package files provides interfaces for reading and writing files.
Package files provides interfaces for reading and writing files.
Index ¶
- type FS
- type FileHelpers
- type FileSystem
- func (FileSystem) Chmod(name string, mode ioFS.FileMode) error
- func (FileSystem) Copy(dst io.Writer, src io.Reader) (written int64, err error)
- func (FileSystem) Create(name string) (*os.File, error)
- func (FileSystem) GetHomeDirectory() string
- func (FileSystem) Lstat(name string) (ioFS.FileInfo, error)
- func (FileSystem) MkdirAll(path string, perm ioFS.FileMode) error
- func (fs FileSystem) MkdirIfNotExist(path string, perm ioFS.FileMode) error
- func (FileSystem) Open(name string) (*os.File, error)
- func (FileSystem) OpenFile(name string, flag int, perm ioFS.FileMode) (*os.File, error)
- func (FileSystem) ReadDir(name string) ([]ioFS.DirEntry, error)
- func (FileSystem) ReadFile(name string) ([]byte, error)
- func (FileSystem) Remove(name string) error
- func (FileSystem) RemoveAll(path string) error
- func (FileSystem) Rename(oldpath string, newpath string) error
- func (FileSystem) Stat(name string) (ioFS.FileInfo, error)
- func (FileSystem) Symlink(oldname string, newname string) error
- func (FileSystem) WriteFile(name string, data []byte, perm ioFS.FileMode) error
- func (FileSystem) WriteString(w io.Writer, s string) (n int, err error)
- type Helper
- func (h Helper) AreVersionsCached() bool
- func (h Helper) CreateExecutableSymlink(goVersionName string) error
- func (h Helper) CreateInitFiles() (*os.File, error)
- func (h Helper) CreateTarFile(content io.ReadCloser) error
- func (h Helper) DeleteDirectory(goVersion string) error
- func (h Helper) DirectoryExists(goVersion string) bool
- func (h Helper) GetCachedResponse(v *[]api_client.VersionInfo) error
- func (h Helper) GetLatestCreatedGoVersionDirectory() (string, error)
- func (h Helper) GetRecentVersion() string
- func (h Helper) GetTarChecksum() (string, error)
- func (h Helper) ReadVersionFromMod() (string, error)
- func (h Helper) RemoveTarFile() error
- func (h Helper) RenameGoDirectory(goVersionName string) error
- func (h Helper) StoreVersionsResponse(body []byte) error
- func (h Helper) UnzipTarFile() error
- func (h Helper) UpdateRecentVersion(goVersionName string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FS ¶
type FS interface { // Chmod changes the mode of the named file to mode. Chmod(name string, mode ioFS.FileMode) error // Create creates or truncates the named file. Create(name string) (*os.File, error) // Open opens the named file for reading. Open(name string) (*os.File, error) // OpenFile opens the named file with specified flag. OpenFile(name string, flag int, perm ioFS.FileMode) (*os.File, error) // ReadDir reads the named directory, returning all its directory entries sorted by filename. ReadDir(name string) ([]ioFS.DirEntry, error) // ReadFile reads the named file and returns the contents. ReadFile(name string) ([]byte, error) // Stat returns a FileInfo describing the named file. Stat(name string) (ioFS.FileInfo, error) // Lstat returns a FileInfo describing the named file. If the file is a symbolic link, the returned FileInfo describes the symbolic link. Lstat(name string) (ioFS.FileInfo, error) // Copy copies from src to dst until either EOF is reached on src or an error occurs. It returns the number of bytes copied and the first error encountered while copying, if any. Copy(dst io.Writer, src io.Reader) (written int64, err error) // Symlink creates newname as a symbolic link to oldname. Symlink(oldname string, newname string) error // WriteFile writes data to the named file, creating it if necessary. WriteFile(name string, data []byte, perm ioFS.FileMode) error // WriteString writes the contents of the string s to w, which accepts a slice of bytes. WriteString(w io.Writer, s string) (n int, err error) // MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. MkdirAll(path string, perm ioFS.FileMode) error // MkdirIfNotExist creates a directory named path if it does not exist, along with any necessary parents, and returns nil, or else returns an error. MkdirIfNotExist(path string, perm ioFS.FileMode) error // Rename renames (moves) oldpath to newpath. Rename(oldpath string, newpath string) error // Remove removes the named file or (empty) directory. Remove(name string) error // RemoveAll removes path and any children it contains. RemoveAll(path string) error // GetHomeDirectory return back the home directory for the current user. GetHomeDirectory() string }
FS is the interface that wraps the basic methods for reading and writing files to the system.
type FileHelpers ¶
type FileHelpers interface { // CreateTarFile creates the archive file based on the given io.ReadCloser content. // CreateTarFile must return a non-null error if the creation of the file is successful. CreateTarFile(content io.ReadCloser) error // GetTarChecksum returns the checksum for the downloaded the archive file. // GetTarChecksum must return the name of the tar file and a non-null error. GetTarChecksum() (string, error) // UnzipTarFile extracts the downloaded archive file. // UnzipTarFile must return a non-null error if the operation is successful. UnzipTarFile() error // RenameGoDirectory renames the extracted directory to the version name. // RenameGoDirectory must return a non-null error if the operation is successful. RenameGoDirectory(goVersionName string) error // RemoveTarFile removes the archive file. // RemoveTarFile must return a non-null error if the deletion is successful. RemoveTarFile() error // CreateExecutableSymlink creates the symlinks for the binaries. // CreateExecutableSymlink must return a non-null error if the symlinks are created. CreateExecutableSymlink(goVersionName string) error // UpdateRecentVersion updates the file where the currect (used) version is stored with the new installed version. // UpdateRecentVersion must return a non-null error if the update is successful. UpdateRecentVersion(goVersionName string) error // StoreVersionsResponse stores the response from the fetch request. // StoreVersionsResponse must return a non-null error if the operation is successful. StoreVersionsResponse(body []byte) error // GetCachedResponse returns the cached response from fetch request. // The versions should be parsed and stored in the value pointed to by v. // GetCachedResponse must return a non-null error if the operation is successful. GetCachedResponse(v *[]api_client.VersionInfo) error // AreVersionsCached returns if either the response from the fetch request is already cached or not. AreVersionsCached() bool // GetRecentVersion returns the current (used) Go version. // GetRecentVersion should return an non empty string that contains the version name. GetRecentVersion() string // DirectoryExists checks if the given Go version directory exists or not. DirectoryExists(goVersion string) bool // DeleteDirectory deletes the given Go version directory. // DeleteDirectory must return a non-null error if the deletion is successful. DeleteDirectory(goVersion string) error // CreateInitFiles creates the files that are required for the CLI. // The *os.File can be used if any of the created files has to be returned back. // CreateInitFiles must return a non-null error if the creation of the files is successful. CreateInitFiles() (*os.File, error) // GetLatestCreatedGoVersionDirectory returns the name of the latest modified directory. // The path should be provided, but rather be static inside the method. // GetLatestCreatedGoVersionDirectory must return the name of the latest modified directory and // a non-null error if the operation is successful. GetLatestCreatedGoVersionDirectory() (string, error) ReadVersionFromMod() (string, error) }
FileHelpers is the interface that wraps the basic methods for working with files.
type FileSystem ¶
type FileSystem struct{}
FileSystem is the struct that implements the FS interface
func (FileSystem) Chmod ¶
func (FileSystem) Chmod(name string, mode ioFS.FileMode) error
Chmod changes the mode of the named file to mode.
It is a wrapper for the os.Chmod function.
func (FileSystem) Copy ¶
Copy copies from src to dst until either EOF is reached on src or an error occurs. It returns the number of bytes copied and the first error encountered while copying, if any.
It is a wrapper for the io.Copy function.
func (FileSystem) Create ¶
func (FileSystem) Create(name string) (*os.File, error)
Create creates or truncates the named file.
It is a wrapper for the os.Create function.
func (FileSystem) GetHomeDirectory ¶
func (FileSystem) GetHomeDirectory() string
GetHomeDirectory return back the home directory for the current user.
If an error occurs, GetHomeDirectory will panic.
func (FileSystem) Lstat ¶
func (FileSystem) Lstat(name string) (ioFS.FileInfo, error)
Lstat returns a FileInfo describing the named file. If the file is a symbolic link, the returned FileInfo describes the symbolic link.
It is a wrapper for the os.Lstat function.
func (FileSystem) MkdirAll ¶
func (FileSystem) MkdirAll(path string, perm ioFS.FileMode) error
MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error.
It is a wrapper for the os.MkdirAll function.
func (FileSystem) MkdirIfNotExist ¶
func (fs FileSystem) MkdirIfNotExist(path string, perm ioFS.FileMode) error
MkdirIfNotExist creates a directory named path if it does not exist, along with any necessary parents, and returns nil, or else returns an error.
It is using the Stat method from the FS interface to check if the directory exists already. Then is using the MkdirAll method from the FS interface to create the directory, along with any necessary parents.
It returns an error either when Stat fails or when the directory creation fails.
func (FileSystem) Open ¶
func (FileSystem) Open(name string) (*os.File, error)
Open opens the named file for reading.
It is a wrapper for the os.Open function.
func (FileSystem) OpenFile ¶
OpenFile opens the named file with specified flag.
It is a wrapper for the os.OpenFile function.
func (FileSystem) ReadDir ¶
func (FileSystem) ReadDir(name string) ([]ioFS.DirEntry, error)
ReadDir reads the named directory, returning all its directory entries sorted by filename.
It is a wrapper for the os.ReadDir function.
func (FileSystem) ReadFile ¶
func (FileSystem) ReadFile(name string) ([]byte, error)
ReadFile reads the named file and returns the contents.
It is a wrapper for the os.ReadFile function.
func (FileSystem) Remove ¶
func (FileSystem) Remove(name string) error
Remove removes the named file or (empty) directory.
It is a wrapper for the os.Remove function.
func (FileSystem) RemoveAll ¶
func (FileSystem) RemoveAll(path string) error
RemoveAll removes path and any children it contains.
It is a wrapper for the os.RemoveAll function.
func (FileSystem) Rename ¶
func (FileSystem) Rename(oldpath string, newpath string) error
Rename renames (moves) oldpath to newpath.
It is a wrapper for the os.Rename function.
func (FileSystem) Stat ¶
func (FileSystem) Stat(name string) (ioFS.FileInfo, error)
Stat returns a FileInfo describing the named file.
It is a wrapper for the os.Stat function.
func (FileSystem) Symlink ¶
func (FileSystem) Symlink(oldname string, newname string) error
Symlink creates newname as a symbolic link to oldname.
It is a wrapper for the os.Symlink function.
func (FileSystem) WriteFile ¶
WriteFile writes data to the named file, creating it if necessary.
It is a wrapper for the os.WriteFile function.
func (FileSystem) WriteString ¶
WriteString writes the contents of the string s to w, which accepts a slice of bytes.
It is a wrapper for the io.WriteString function.
type Helper ¶
type Helper struct {
// contains filtered or unexported fields
}
Helper is the struct that implements the FileHelpers interface
func New ¶
New returns a *Helper instance that implements the FileHelpers interface. Each call to New returns a distinct *Helper instance even if the parameters are identical.
func (Helper) AreVersionsCached ¶
AreVersionsCached returns if either the response from the fetch request is already cached or not.
In case the cached file is older than a week, it will return false to force the caching again.
func (Helper) CreateExecutableSymlink ¶
CreateExecutableSymlink creates the symlinks in $HOME/bin directory.
The symlinks that will be created are the ones that can be found in the bin directory inside the version directory (e.g. ~/.gvs/.go.versions/go1.21.3/bin). If the symlinks exist already, we first remove the existing ones, and then creare the new ones.
If for any reason if fails, CreateExecutableSymlink returns back an error.
func (Helper) CreateInitFiles ¶
CreateInitFiles creates the files that are required for the CLI.
It is creating the below:
- `$HOME/.gvs/` - The main directory for the CLI to store any data the CLI needs.
- `$HOME/.gvs/.go.versions/` - The directory where the downloaded versions are stored (as well as the CURRENT file).
- `$HOME/bin` - This is the directory when the symlinks are created for the selected version.
- `$HOME/.gvs/gvs.log` - This is where the logs are stored for debugging.
Once the gvs.log is created, the *os.File is returned back so it can be used from the logger.
If for any reason if fails, CreateInitFiles returns back nil for the file and the error.
func (Helper) CreateTarFile ¶
func (h Helper) CreateTarFile(content io.ReadCloser) error
CreateTarFile creates the archive file based on the response from the API call that returns the file binary.
If the creation of the file fails, CreateTarFile will return an error.
func (Helper) DeleteDirectory ¶
DeleteDirectory deletes the given Go version directory.
If for any reason if fails, DeleteDirectory returns back an error.
func (Helper) DirectoryExists ¶
DirectoryExists checks if the given Go version directory exists or not.
func (Helper) GetCachedResponse ¶
func (h Helper) GetCachedResponse(v *[]api_client.VersionInfo) error
GetCachedResponse returns the cached response from fetch request.
It will parse the JSON-encoded data and store it in the value pointed to by v.
If for any reason if fails, GetCachedResponse returns back an error.
func (Helper) GetLatestCreatedGoVersionDirectory ¶
GetLatestCreatedGoVersionDirectory returns the name of the latest modified directory in .gvs/.go.versions/.
If for any reason if fails, GetCachedResponse returns back an empty string for the name and the error.
func (Helper) GetRecentVersion ¶
GetRecentVersion returns the current (used) Go version from ~/.gvs/.go.versions/CURRENT
If for any reason if fails, GetRecentVersion logs the error message and returns back an empty string.
func (Helper) GetTarChecksum ¶
GetTarChecksum returns the checksum for the downloaded the archive file.
If for any reason if fails, GetTarChecksum returns back an empty checksum and the error.
func (Helper) ReadVersionFromMod ¶
func (Helper) RemoveTarFile ¶
RemoveTarFile removes the archive file.
If for any reason if fails, RemoveTarFile returns back an error.
func (Helper) RenameGoDirectory ¶
RenameGoDirectory renames the extracted directory to the version name.
After extracting the archive file, we need to rename it to the version name (e.g. go1.21.3). Since we can have multiple versions downloaded, this helps to store the versions into the correct directories.
If for any reason if fails, RenameGoDirectory returns back an error.
func (Helper) StoreVersionsResponse ¶
StoreVersionsResponse stores the response from the fetch request so it can be used by avoid multiple requests to the API.
If for any reason if fails, StoreVersionsResponse returns back an error.
func (Helper) UnzipTarFile ¶
UnzipTarFile extracts the downloaded archive file.
UnzipTarFile is using the unzip interface.
If for any reason if fails, UnzipTarFile returns back an error.
func (Helper) UpdateRecentVersion ¶
UpdateRecentVersion updates the ~/.gvs/.go.versions/CURRENT file with the new installed version.
We store the new installed version in this file, so we know which is the current used version.
If for any reason if fails, UpdateRecentVersion returns back an error.