internal

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PRODUCT is called igo for Install Go
	PRODUCT string = "igo"

	// AUTHOR is Andrei
	AUTHOR string = "github.com/ProjectApario/igo"

	// XRP is how you can tip the AUTHOR
	XRP string = "rAparioji3FxAtD7UufS8Hh9XmFn7h6AX"
)

Variables

View Source
var Capture = func(err ...error) {
	if err == nil || len(err) == 0 || err[0] == nil {
		return
	}
	fmt.Println(err)
	os.Exit(1)
}

Capture accepts nil or an error or multiple errors

Example:

		Capture(errors.New("this is an error to run os.Exit(1) after printing this =D"))
		// OR
     var E1 error
     var E2 error
		Capture(E1, E2)
View Source
var CaptureInt = func(_ int, err error) {
	if err != nil {
		panic(err)
	}
	return
}

CaptureInt will Discard the integer and process the error using Capture()

View Source
var CaptureOpenFile = func(path string, flag int, perm os.FileMode) *os.File {
	f, e := os.OpenFile(path, flag, perm)
	Capture(e)
	return f
}

CaptureOpenFile is a helper func that accepts a path, opens it or Capture() the error

Example:

handler := CaptureOpenFile("/opt/app/config.yaml", os.O_RDONLY, 0600)
View Source
var CheckRootPrivileges = func() bool {
	if runtime.GOOS == "windows" {
		return false
	} else {
		if os.Geteuid() == 0 {
			return true
		}
		if User().Uid == "0" {
			return true
		}

		return false
	}
}
View Source
var Discard = func(err ...error) {
	if err == nil || len(err) == 0 || err[0] == nil {
		return
	}
	fmt.Println(err)
}

Discard err will print the error only if it occurred

View Source
var FindSymlinks = func(dirPath string) ([]string, error) {
	dir, err := os.Open(dirPath)
	if err != nil {
		return nil, ErrFile{dirPath, err, "os.Open"}
	}
	defer dir.Close()
	entries, err := dir.ReadDir(-1)
	if err != nil {
		return nil, ErrDirEntries{dirPath, err}
	}
	var symlinks []string
	for _, entry := range entries {
		if entry.Type()&os.ModeSymlink != 0 {
			fullPath := filepath.Join(dirPath, entry.Name())
			symlinks = append(symlinks, fullPath)
		}
	}
	return symlinks, nil
}

FindSymlinks scans the provided directory path and returns a slice of full paths to any symlinks found in the root directory only. It does not recurse into subdirectories.

View Source
var IsDirectory = func(path string) bool {
	fileInfo, err := os.Lstat(path)
	if err != nil {
		return false
	}
	return fileInfo.IsDir()
}
View Source
var IsSymlink = func(path string) bool {
	fileInfo, err := os.Lstat(path)
	if err != nil {
		return false
	}
	return fileInfo.Mode()&os.ModeSymlink != 0
}
View Source
var MakeDirsWritable = func(path string) error {
	return filepath.WalkDir(path, func(p string, d os.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if !d.IsDir() {
			return nil
		}

		info, err := d.Info()
		if err != nil {
			return err
		}

		mode := info.Mode()
		if mode&0200 == 0 {
			newMode := mode | 0200
			if err := os.Chmod(p, newMode); err != nil {
				return ErrChmodFailed{p, err}
			}
		}
		return nil
	})
}
View Source
var PathExists = func(path string) bool {
	_, err := os.Lstat(path)
	return !os.IsNotExist(err) && !os.IsPermission(err)
}
View Source
var ReadSymlink = func(path string) (string, error) {
	target, err := os.Readlink(path)
	if err != nil {
		return "", ErrPathFailed{path, err, "remove"}
	}

	if !filepath.IsAbs(target) {
		symlinkDir := filepath.Dir(path)
		target = filepath.Join(symlinkDir, target)
	}

	return target, nil
}

ReadSymlink reads the target of a symlink

View Source
var RemoveSetuidSetgidBits = func(path string) error {
	var cmd *exec.Cmd
	switch runtime.GOOS {
	case "linux", "darwin":
		cmd = exec.Command("sh", "-c", fmt.Sprintf("chmod -R a-s %s", path))
	default:
		return ErrOSNotSupported{runtime.GOOS}
	}
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	if err := cmd.Run(); err != nil {
		return ErrSetUIDGIDBit{path, err, "remove"}
	}
	return nil
}
View Source
var RemoveStickyBit = func(path string) error {
	if !IsDirectory(path) {
		return ErrStickyBitsOnFile{path}
	}
	var cmd *exec.Cmd
	switch runtime.GOOS {
	case "linux", "darwin":
		cmd = exec.Command("sh", "-c", fmt.Sprintf("chmod -t %s", path))
	default:
		return ErrOSNotSupported{runtime.GOOS}
	}

	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	if err := cmd.Run(); err != nil {
		return ErrStickyBitFailed{path, err, "remove"}
	}
	return nil
}
View Source
var RemoveSymlinkOrBackupPath = func(path string) error {
	if !PathExists(path) {
		return nil
	}
	if IsSymlink(path) {
		err := os.Remove(path)
		if err != nil {
			return ErrPathFailed{path, err, ""}
		}
		return nil
	}

	err := os.Rename(path, path+".bak")
	if err != nil {
		return ErrPathFailed{path, err, "non-"}
	}

	return nil
}

RemoveSymlinkOrBackupPath checks if the given path is a symlink and deletes it if it is not. Returns an error if the check or deletion fails.

View Source
var SetSetuidSetgidBits = func(path string) error {
	var cmd *exec.Cmd
	switch runtime.GOOS {
	case "linux", "darwin":
		cmd = exec.Command("sh", "-c", fmt.Sprintf("chmod -R a+s %s", path))
	default:
		return ErrOSNotSupported{runtime.GOOS}
	}
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	if err := cmd.Run(); err != nil {
		return ErrBitNotSet{err}
	}
	return nil
}
View Source
var SetStickyBit = func(path string) error {
	if !IsDirectory(path) {
		return ErrStickyBitsOnFile{path}
	}
	var cmd *exec.Cmd
	switch runtime.GOOS {
	case "linux", "darwin":
		cmd = exec.Command("sh", "-c", fmt.Sprintf("chmod +t %s", path))
	default:
		return ErrOSNotSupported{runtime.GOOS}
	}
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	if err := cmd.Run(); err != nil {
		return ErrStickyBitFailed{path, err, "set"}
	}
	return nil
}
View Source
var Touch = func(path string) error {
	_, err := os.Stat(path)
	if os.IsNotExist(err) {
		pathFile, err := os.Create(path)
		if err != nil {
			return ErrFile{path, err, "os.Create"}
		}
		if err := pathFile.Close(); err != nil {
			return ErrFile{path, err, "path.Close"}
		}
		return nil
	} else if err != nil {
		return ErrFile{path, err, "os.Stat"}
	}

	currentTime := time.Now()
	if err := os.Chtimes(path, currentTime, currentTime); err != nil {
		return ErrFile{path, err, "os.Chtimes"}
	}

	return nil
}

Touch creates a new empty file or updates the modification time of an existing file at the given path. Returns an error if the operation fails.

View Source
var User = func() *user.User {
	currentUser, err := UserCurrent()
	if err != nil {
		return &user.User{
			Uid:      "-1",
			Gid:      "-1",
			Username: "nobody",
			Name:     "nobody",
			HomeDir:  "/dev/null",
		}
	}
	return currentUser
}
View Source
var UserCurrent = user.Current
View Source
var VerifyLink = func(link, expectedTarget string) string {
	actualTarget, err := ReadSymlink(link)
	if err != nil {
		return "❌"
	}

	cleanExpected := filepath.Clean(expectedTarget)
	cleanActual := filepath.Clean(actualTarget)

	if cleanActual == cleanExpected {
		return "✅"
	}

	return "❌"
}

VerifyLink checks if a symlink correctly resolves to its expected target and returns the appropriate status emoji (✅ for success, ❌ for failure)

Functions

func About

func About() string

About prints the product information

Types

type ErrBitNotSet added in v1.1.0

type ErrBitNotSet struct {
	Err error
}

func (ErrBitNotSet) Error added in v1.1.0

func (e ErrBitNotSet) Error() string

type ErrChmodFailed added in v1.1.0

type ErrChmodFailed struct {
	Path string
	Err  error
}

func (ErrChmodFailed) Error added in v1.1.0

func (e ErrChmodFailed) Error() string

type ErrDirEntries added in v1.1.0

type ErrDirEntries struct {
	Path string
	Err  error
}

func (ErrDirEntries) Error added in v1.1.0

func (e ErrDirEntries) Error() string

type ErrFile added in v1.1.0

type ErrFile struct {
	Path string
	Err  error
	How  string
}

func (ErrFile) Error added in v1.1.0

func (e ErrFile) Error() string

type ErrOSNotSupported added in v1.1.0

type ErrOSNotSupported struct {
	OS string
}

func (ErrOSNotSupported) Error added in v1.1.0

func (e ErrOSNotSupported) Error() string

type ErrPathFailed added in v1.1.0

type ErrPathFailed struct {
	Path string
	Err  error
	Neg  string
}

func (ErrPathFailed) Error added in v1.1.0

func (e ErrPathFailed) Error() string

type ErrSetUIDGIDBit added in v1.1.0

type ErrSetUIDGIDBit struct {
	Path string
	Err  error
	How  string
}

func (ErrSetUIDGIDBit) Error added in v1.1.0

func (e ErrSetUIDGIDBit) Error() string

type ErrStickyBitFailed added in v1.1.0

type ErrStickyBitFailed struct {
	Path string
	Err  error
	How  string
}

func (ErrStickyBitFailed) Error added in v1.1.0

func (e ErrStickyBitFailed) Error() string

type ErrStickyBitsOnFile added in v1.1.0

type ErrStickyBitsOnFile struct {
	Path string
}

func (ErrStickyBitsOnFile) Error added in v1.1.0

func (e ErrStickyBitsOnFile) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL