Skip to content

Commit

Permalink
add -k flag to keep temporary files, #32
Browse files Browse the repository at this point in the history
  • Loading branch information
miku committed Jun 22, 2023
1 parent 8e9f00c commit a4f5dab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/metha-sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
extraHeaders xflag.Array // Extra HTTP header.
timeout = flag.Duration("T", 30*time.Second, "http client timeout")
maxRetries = flag.Int("r", 10, "max number of retries")
keepTemporaryFiles = flag.Bool("k", false, "keep temporary files when interrupted")
)

func main() {
Expand Down Expand Up @@ -123,6 +124,7 @@ func main() {
harvest.DailyInterval = *daily
harvest.ExtraHeaders = extra
harvest.Delay = *delay
harvest.KeepTemporaryFiles = *keepTemporaryFiles
log.Printf("harvest: %+v", harvest)
if *removeCached {
log.Printf("removing already cached files from %s", harvest.Dir())
Expand Down
6 changes: 6 additions & 0 deletions harvest.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Harvest struct {
HourlyInterval bool
DailyInterval bool
ExtraHeaders http.Header
KeepTemporaryFiles bool

Delay int

Expand Down Expand Up @@ -143,6 +144,11 @@ func (h *Harvest) temporaryFilesSuffix(suffix string) []string {

// cleanupTemporaryFiles will remove all temporary files in the harvesting dir.
func (h *Harvest) cleanupTemporaryFiles() error {
if h.KeepTemporaryFiles {
log.Printf("keeping %d temporary file(s) under %s",
len(h.temporaryFiles()), h.Dir())
return nil
}
for _, filename := range h.temporaryFiles() {
if err := os.Remove(filename); err != nil {
if e, ok := err.(*os.PathError); ok && e.Err == syscall.ENOENT {
Expand Down

0 comments on commit a4f5dab

Please sign in to comment.