Skip to content

Commit

Permalink
more consistent error handling in tmutil
Browse files Browse the repository at this point in the history
  • Loading branch information
tg44 committed Oct 13, 2021
1 parent 8bde077 commit 05a06c2
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions pkg/tmutil/tmutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func AddPathsToTM(paths []string, logDir string, bufferSize int, verbose bool) {

go func() {
for j := range check {
res, _ := checkPath(j)
if res {
res, err := checkPath(j, verbose)
if res && err == nil {
add <- j
}
}
Expand All @@ -51,8 +51,10 @@ func AddPathsToTM(paths []string, logDir string, bufferSize int, verbose bool) {

go func() {
for j := range add {
addPath(j, logFile)
added += 1
err = addPath(j, logFile)
if err == nil {
added += 1
}
}
finished <- true
close(finished)
Expand All @@ -68,33 +70,46 @@ func AddPathsToTM(paths []string, logDir string, bufferSize int, verbose bool) {
}
}

func checkPath(path string) (bool, error) {
func checkPath(path string, verbose bool) (bool, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false, err
}
cmd := exec.Command("tmutil", "isexcluded", path)

var out bytes.Buffer
cmd.Stdout = &out
var outErr bytes.Buffer
cmd.Stderr = &outErr
err := cmd.Run()
if err != nil {
if verbose {
log.Println(out.String())
log.Println(outErr.String())
}
return false, err
}

s := out.String()
return !strings.Contains(s, "[Excluded]"), nil
}

func addPath(path string, logfile *os.File) {
func addPath(path string, logfile *os.File) error {
cmd := exec.Command("tmutil", "addexclusion", path)
var out bytes.Buffer
cmd.Stdout = &out
var outErr bytes.Buffer
cmd.Stderr = &outErr
err := cmd.Run()
if err != nil {
log.Fatal(err)
log.Println(out.String())
log.Println(outErr.String())
return err
}
_, _ = logfile.WriteString(path + "\r\n")
return nil
}

func removePath(path string) {
func removePath(path string) error {
cmd := exec.Command("tmutil", "removeexclusion", path)
var out bytes.Buffer
cmd.Stdout = &out
Expand All @@ -104,8 +119,8 @@ func removePath(path string) {
if err != nil {
log.Println(out.String())
log.Println(outErr.String())
log.Fatal(err)
}
return err
}

func RemoveAllFromLogs(logPath string, bufferSize int, verbose bool) {
Expand Down Expand Up @@ -145,7 +160,7 @@ func RemovePathsFromTM(paths []string, bufferSize int, verbose bool) {

go func() {
for j := range check {
res, err := checkPath(j)
res, err := checkPath(j, verbose)
if !res && err == nil {
remove <- j
}
Expand All @@ -155,8 +170,10 @@ func RemovePathsFromTM(paths []string, bufferSize int, verbose bool) {

go func() {
for j := range remove {
removePath(j)
removed += 1
err := removePath(j)
if err == nil {
removed += 1
}
}
finished <- true
close(finished)
Expand Down

0 comments on commit 05a06c2

Please sign in to comment.