fsx

package
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2025 License: BSD-3-Clause Imports: 5 Imported by: 1

README


This package provides utility functions for common file system operations, including path parsing, file and directory copying, and symbolic link handling.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(src, dst string) error

Copy copies a file from src to dst. It handles files and symbolic links.

Example
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/exonlabs/go-utils/pkg/abc/fsx"
)

func main() {
	tmpDir := os.TempDir()
	srcPath := filepath.Join(tmpDir, "srcfile.txt")
	dstPath := filepath.Join(tmpDir, "srcfile_copy.txt")

	err := fsx.Copy(srcPath, dstPath)
	if err != nil {
		fmt.Println(err)
	}
}
Output:

func CopyDir

func CopyDir(src, dst string) error

CopyDir copies a directory and its contents from src to dst.

Example
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/exonlabs/go-utils/pkg/abc/fsx"
)

func main() {
	tmpDir := os.TempDir()
	srcDir := filepath.Join(tmpDir, "srcdir")
	dstDir := filepath.Join(tmpDir, "srcdir_copy")

	err := fsx.CopyDir(srcDir, dstDir)
	if err != nil {
		fmt.Println(err)
	}
}
Output:

func IsExist

func IsExist(path string) bool

IsExist checks if a file or directory exists.

func ParsePath

func ParsePath(path string) (string, error)

ParsePath validates and returns the absolute path.

Example
package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/abc/fsx"
)

func main() {
	paths := []string{
		"",
		" ",
		"/",
		"/tmp/..",
		"/tmp/dir1/../",
		"/tmp/dir1/../srcfile.txt",
	}
	for _, path := range paths {
		p, err := fsx.ParsePath(path)
		if err == nil {
			fmt.Printf("\"%s\" --> \"%s\"\n", path, p)
		} else {
			fmt.Printf("\"%s\" --> err: %s\n", path, err.Error())
		}
	}
}
Output:

"" --> err: invalid path
" " --> err: invalid path
"/" --> "/"
"/tmp/.." --> "/"
"/tmp/dir1/../" --> "/tmp"
"/tmp/dir1/../srcfile.txt" --> "/tmp/srcfile.txt"

func Remove

func Remove(path string) error

Remove removes regular file or directory if exists.

func Touch

func Touch(path string) error

Touch creates empty file at path if not exists. it returns error if path exist and is a directory.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳