Skip to content

Commit

Permalink
chore: add clear-work-dir flag (#57)
Browse files Browse the repository at this point in the history
It will completely clear the specified working directory.
  • Loading branch information
ffenix113 committed May 23, 2024
1 parent 62e0b10 commit f851f01
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cmd/zigbee/firmware/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
Expand All @@ -24,6 +25,9 @@ func buildCmd() *cli.Command {
&cli.BoolFlag{
Name: "only-generate",
},
&cli.BoolFlag{
Name: "clear-work-dir",
},
},
Action: buildFirmware,
}
Expand Down Expand Up @@ -53,6 +57,12 @@ func buildFirmware(ctx *cli.Context) error {
return fmt.Errorf("new generator: %w", err)
}

if ctx.Bool("clear-work-dir") {
if err := clearWorkDir(workDir); err != nil {
return err
}
}

if err := generator.Generate(workDir, cfg); err != nil {
return fmt.Errorf("generate base: %w", err)
}
Expand Down Expand Up @@ -123,3 +133,28 @@ func runBuild(ctx context.Context, device *config.Device, workDir string) error

return nil
}

func clearWorkDir(workDir string) error {
return filepath.WalkDir(workDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
var pathErr *os.PathError
if !errors.As(err, &pathErr) {
return err
}

if errors.Is(pathErr, os.ErrNotExist) {
return nil
}

return err
}

if path == workDir {
return nil
}

log.Printf("deleting %s\n", path)

return os.RemoveAll(path)
})
}

0 comments on commit f851f01

Please sign in to comment.