cmd

package
v0.0.0-...-52f67f4 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: MPL-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "remove-trash [PATH]",
	Short: "Removes trash files from the filesystem starting at PATH",
	Long: `Removes files like .DS_Store and Thumb.db from the disk, as well as other "useless" files:

	- .DS_Store
	- .cache
	- .gradle
	- .mypy_cache
	- .sass-cache
	- .textpadtmp
	- Thumbs.db
	- __pycache__
	- _build
	- build
	- slprj
	- zig-cache
	- zig-out
	- *.slxc
	- *.bak
	- ~*`,
	Run: func(cmd *cobra.Command, args []string) {
		version, err := cmd.Flags().GetBool("version")
		if err != nil {
			fmt.Fprintf(os.Stderr, "Could not parse options: %s\n", err)
			os.Exit(1)
		}

		dryRun, err := cmd.Flags().GetBool("dry-run")
		if err != nil {
			fmt.Fprintf(os.Stderr, "Could not parse options: %s\n", err)
			os.Exit(1)
		}

		noErrors, err := cmd.Flags().GetBool("no-error")
		if err != nil {
			fmt.Fprintf(os.Stderr, "Could not parse options: %s\n", err)
			os.Exit(1)
		}

		if version {
			fmt.Printf("Version %s", remove_trash.Version)
			return
		} else if len(args) == 0 {
			cmd.Help()
			return
		}

		var removedTotal []string
		var failedTotal []remove_trash.FailedPath

		var countAtomic atomic.Uint64
		var sizeAtomic atomic.Uint64
		var totalAtomic atomic.Uint64

		var bar *progressbar.ProgressBar = progressbar.NewOptions(1,
			progressbar.OptionSetWriter(os.Stdout),
			progressbar.OptionSetDescription("Removed"),
			progressbar.OptionThrottle(0),
			progressbar.OptionShowCount(),
			progressbar.OptionFullWidth(),
			progressbar.OptionSetRenderBlankState(true))

		var wg sync.WaitGroup
		var mutex sync.Mutex
		var channels []<-chan remove_trash.ProgressReport

		for _, pathPtr := range args {
			var path string = pathPtr
			channel := make(chan remove_trash.ProgressReport, 10)
			channels = append(channels, channel)

			worker := func() {
				defer wg.Done()

				removed, failed, err := remove_trash.Traverse(path, dryRun, channel)
				if err != nil {
					bar.Clear()
					fmt.Printf("An error occurred: %s\n", err)
				}

				mutex.Lock()
				defer mutex.Unlock()

				failedTotal = append(failed, failed...)
				removedTotal = append(removedTotal, removed...)
			}

			wg.Add(1)
			go worker()
		}

		for pr := range merge(channels...) {
			sizeAtomic.Add(pr.Bytes)

			if pr.Bytes != 0 {
				countAtomic.Add(1)
			} else {
				totalAtomic.Add(1)
			}

			total := totalAtomic.Load()
			count := countAtomic.Load()

			if total == count {
				count -= 1
			}

			bar.ChangeMax64(int64(total))
			bar.Describe(fmt.Sprintf("Removed %s", bytesize.New(float64(sizeAtomic.Load()))))
			bar.Set64(int64(count))
		}

		wg.Wait()

		bar.ChangeMax64(int64(totalAtomic.Load()))
		bar.Set64(int64(countAtomic.Load()))

		if dryRun {
			bar.Clear()
			for _, path := range removedTotal {
				fmt.Printf("Removed %s\n", path)
			}
			fmt.Printf("Would remove %d files for a total of %s\n", len(removedTotal), bytesize.New(float64(sizeAtomic.Load())))
		} else if !noErrors {
			fmt.Println("")
			for _, failed := range failedTotal {
				fmt.Fprintln(os.Stderr, failed.Err)
			}
		}
	},
}

Functions

func Execute

func Execute()

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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