wastebasket

package module
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2025 License: MPL-2.0 Imports: 13 Imported by: 3

README

Wastebasket

Go Reference CI codecoverage

Wastebasket is a go library / CLI tool allowing you to interact with your system trashbin. It supports nix-systems (such as Linux and BSD) and Windows.

Documentation

Further documentation can be found at /docs.

Dependencies

Golang

For v2, go 1.23.4 or newer is required. For go 1.18 and up use the old implementation.

MacOS

Note that github.com/Bios-Marcel/wastebasket/v2 only supports MacOS patially. I simply can't be bothered maintaining it, as the performance is horrible and adding features is hard to test. Additionally, a "proper" implementation requires a very complex solution, that according to research won't work reliable. For now, we are stuck calling finder.

It's recommended to not call the Trash functon in a tight loop, put some time.Sleep calls, as the finder noise will sound very painful and cause your device to lag.

Restoring and querying files is not possible as of now.

IF you are willing to contribute and potentially maintain a proper MacOS implementation, I'd be glad to accept a PR.

Windows

There are no dependencies, it depends on the Shell32 API built into Windows.

No CGO required

Linux (Unix)

There are no dependencies, as Wastebasket offers a native golang implementation of the FreeDesktop Trash specification.

Additionally, the custom implementation should also work for systems such as BSD and its derivatives. However, this has not been tested and I do not plan on doing so, simply because GitHub does not currently support running tests on any BSD derivatives.

If anyone is willing to host a custom runner (which I think is possible), then I'd be open to this though.

No CGO required

Library usage

Grab it via

go get github.com/Bios-Marcel/wastebasket/v2

and you are ready to go.

Minimal Go example that creates a file, deletes it and empties the trashbin:

package main

import (
    "fmt"
    "os"

    "github.com/Bios-Marcel/wastebasket/v2"
)

func main() {
    os.WriteFile("test.txt", []byte("Test"), os.ModePerm)
    fmt.Println(wastebasket.Trash("test.txt"))
    wastebasket.Empty()
}

Make sure to check for ErrPlatformNotSupported if you are deploying to MacOS or nieche systems.

CLI usage

UNSTABLE, USE AT YOUR OWN RISK

Wastebasket can also be used as a commandline interface. You can build executables for each subcommand (such as empty) separately, or build wastebasket as a whole.

In order to do so, run

go build ./cmd/CMD

where CMD is whichever command you want to build.

Autocompletion

The CLI offers autocompletion for flags and pre-defined arguments.

Bash; Write the following into your bash profile:

source <(wastebasket completion bash)

Additionally, completion is supported for zsh, powershell and fish. For information, see Cobra shell completions

Tests

For windows, there is a utility that allows you to run linux or windows tests using containers. This prevents the utilitiy from accidentally deleting files or emptying your trashbin.

Simply execute:

test <linux|windows>

Note that this doesn't work when you are on linux.

Benchmarks

Run benchmarks using:

go test -bench=.

Documentation

Index

Constants

View Source
const RFC3339 string = "2006-01-02T15:04:05"

RFC3339 is the same as time.RFC3339 but without timezones.

Variables

View Source
var (
	// ErrPlatformNotSupported indicates that the current platform does not
	// suport trashing files or the API isn't fully implemented.
	ErrPlatformNotSupported = errors.New("platform not supported")
	ErrAlreadyExists        = errors.New("couldn't restore file, already exists, apply force")
	ErrOnlyOneGlobAllowed   = errors.New("only one glob is allowed")
)

Functions

func Empty

func Empty() error

func Trash

func Trash(paths ...string) error

Types

type QueryOptions

type QueryOptions struct {
	Glob bool
	// Search can be relative or absolute.
	Search []string
}

QueryOptions allows to configure the Query-Call. Options Globs and Paths aren't allowed to both be set.

type QueryResult

type QueryResult struct {
	// Matches are the query results, mapped from the query input (globa / path)
	// to the respective trashed files. Note that the same file can be trashed
	// multiple times and a glob can match multiple files. Therefore, expect
	// multiple entries in both scenarios.
	Matches map[string][]TrashedFileInfo
}

func Query

func Query(options QueryOptions) (*QueryResult, error)

type TrashedFileInfo

type TrashedFileInfo interface {
	// OriginalPath is the files path before it was deleted.
	OriginalPath() string
	// DeletionDate is the deletion date in the computers local timezone.
	DeletionDate() time.Time
	// Restore will attempt restoring the file to its previous location.
	Restore(force bool) error
	// Delete will permanently deleting the underlying file. Note that we do not
	// zero the respective bytes on the disk.
	Delete() error
	// UniqueIdentifier can be used to uniquely identify a file if the path and
	// deletion date are exactly the same. This CAN be useful file restoration.
	UniqueIdentifier() string
}

TrashedFileInfo represents a file that has been deleted and now resides in the trashbin.

Directories

Path Synopsis
cmd
empty command
query command
restore command
trash command
wastebasket command

Jump to

Keyboard shortcuts

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