Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions to handle directories #223

Open
marcopaganini opened this issue Jan 29, 2025 · 5 comments
Open

Functions to handle directories #223

marcopaganini opened this issue Jan 29, 2025 · 5 comments

Comments

@marcopaganini
Copy link

Hello!

After working with script for a while (nice work btw!) I miss some basic functions that I eventually implemented (in a quick and dirty way) for myself.

Most of them have to do with directory manipulation, like "list of files in a directory", "list of directories in a directory" as well as "by date" variants which are super useful when we need, for instance, to find the N newest files on a dir?

Would there be interest in PRs with these?

@bitfield
Copy link
Owner

Hi @marcopaganini! Thanks for opening the issue. Could you show an example program here which you wrote using one or more of your proposed functions? It'll be very useful to be able to see them in context.

@marcopaganini
Copy link
Author

Hello!

Yes, sure.

Right now, it's not in integrated with the script way of doing things at all. Naturally a PR would have it done better. This was quick hack that I had to do for a simple tool that looks at the last file in a list of files and then looks for a string inside this file (it basically checks if my backups succeeded).

package main

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

	"github.com/bitfield/script"
)

func backupStatus() error {
	var err error

	if len(os.Args) != 1 {
		return errors.New("use: backup-status")
	}
	logroot := "/var/log/netbackup"

	// The directory structure for logs is:
	// /var/log/netbackup / backup-names... / backup-files [.gz]
	logdirs, err := listDirs(logroot)
	if err != nil {
		return err
	}
	for _, logdir := range logdirs {
		logfiles, err := listFilesByDate(filepath.Join(logroot, logdir.Name()))
		if err != nil {
			return err
		}
		if len(logfiles) == 0 {
			continue
		}
		latestFile := logfiles[len(logfiles)-1].Name()
		latestPath := filepath.Join(logroot, logdir.Name(), latestFile)

		var out string

		// Gzip decompress on the fly if needed.
		if strings.HasSuffix(latestFile, ".gz") {
			out, err = script.File(latestPath).Filter(gunzip).Last(10).String()
		} else {
			out, err = script.File(latestPath).Last(10).String()
		}
		if err != nil {
			return err
		}
		// Results
		if strings.Contains(out, "Backup Result: Success") {
			fmt.Println("✅", latestFile)
		} else {
			fmt.Println("❌", latestFile)
			fmt.Println(out)
		}
	}
	return nil
}

@bitfield
Copy link
Owner

bitfield commented Feb 3, 2025

Great, thanks! Would you now like to try rewriting this program using your proposed script functions, and show how they would work, and how much shorter and clearer it makes the calling code? That'll help us to nail down what specifically is proposed, and also make the case for why it's needed in script.

@bitfield
Copy link
Owner

@marcopaganini just checking if you're still interested in this.

@marcopaganini
Copy link
Author

Yes! Sorry just stuck with work. Will get back to this as soon as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants