-
-
Notifications
You must be signed in to change notification settings - Fork 326
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
Comments
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. |
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
} |
Great, thanks! Would you now like to try rewriting this program using your proposed |
@marcopaganini just checking if you're still interested in this. |
Yes! Sorry just stuck with work. Will get back to this as soon as possible. |
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?
The text was updated successfully, but these errors were encountered: