From: Sergey Matveev Date: Fri, 29 Sep 2023 19:44:05 +0000 (+0300) Subject: DRY filepath.Abs/Rel X-Git-Tag: v2.0.0~37 X-Git-Url: http://www.git.cypherpunks.ru/?p=goredo.git;a=commitdiff_plain;h=9d1ca78917c32c17686d7ea2ef01c6ebcc0f63d8 DRY filepath.Abs/Rel --- diff --git a/buildlog.go b/buildlog.go index 3bf3ce0..2978080 100644 --- a/buildlog.go +++ b/buildlog.go @@ -25,7 +25,6 @@ import ( "io" "os" "path" - "path/filepath" "sort" "strconv" "strings" @@ -94,10 +93,7 @@ func depthPrefix(depth int) string { } func showBuildLogSub(sub *BuildLogJob, depth int) error { - abs, err := filepath.Abs(path.Join(sub.dir, sub.tgt)) - if err != nil { - return err - } + abs := mustAbs(path.Join(sub.dir, sub.tgt)) if _, ok := buildLogSeen[abs]; ok { return nil } diff --git a/cleanup.go b/cleanup.go index 26dbadc..b978bae 100644 --- a/cleanup.go +++ b/cleanup.go @@ -24,7 +24,6 @@ import ( "log" "os" "path" - "path/filepath" "strings" ) @@ -45,10 +44,7 @@ func init() { } func redoDirClean(root, what string) error { - root, err := filepath.Abs(root) - if err != nil { - panic(err) - } + root = mustAbs(root) dir, err := os.Open(root) if err != nil { return ErrLine(err) @@ -103,10 +99,7 @@ func redoDirClean(root, what string) error { } func cleanupWalker(root, what string) error { - root, err := filepath.Abs(root) - if err != nil { - panic(err) - } + root = mustAbs(root) dir, err := os.Open(root) if err != nil { return ErrLine(err) diff --git a/dep.go b/dep.go index 88e2215..9792cb0 100644 --- a/dep.go +++ b/dep.go @@ -26,7 +26,6 @@ import ( "io" "os" "path" - "path/filepath" "go.cypherpunks.ru/recfile" "lukechampine.com/blake3" @@ -124,20 +123,15 @@ func depsWrite(fdDep *os.File, tgts []string) error { tracef(CDebug, "no opened fdDep: %s", tgts) return nil } + var err error for _, tgt := range tgts { - tgtAbs, err := filepath.Abs(tgt) - if err != nil { - panic(err) - } + tgtAbs := mustAbs(tgt) cwd := Cwd if DepCwd != "" && Cwd != DepCwd { cwd = DepCwd } tgtDir := path.Join(cwd, DirPrefix) - tgtRel, err := filepath.Rel(tgtDir, tgtAbs) - if err != nil { - panic(err) - } + tgtRel := mustRel(tgtDir, tgtAbs) if _, errStat := os.Stat(tgt); errStat == nil { err = ErrLine(depWrite(fdDep, tgtDir, tgtRel, "")) } else { diff --git a/do.go b/do.go index 8c2b62c..202e964 100644 --- a/do.go +++ b/do.go @@ -22,7 +22,6 @@ package main import ( "os" "path" - "path/filepath" "strings" ) @@ -82,7 +81,7 @@ func findDo(fdDep *os.File, cwd, tgt string) (string, int, error) { } } levels = append(levels, "..") - dirAbs, err := filepath.Abs(path.Join(cwd, updir)) + dirAbs := mustAbs(path.Join(cwd, updir)) if err != nil { panic(err) } diff --git a/main.go b/main.go index f6304ac..025ecbe 100644 --- a/main.go +++ b/main.go @@ -148,10 +148,7 @@ func main() { if TopDir == "" { TopDir = "/" } else { - TopDir, err = filepath.Abs(TopDir) - if err != nil { - panic(err) - } + TopDir = mustAbs(TopDir) } DirPrefix = os.Getenv(EnvDirPrefix) DepCwd = os.Getenv(EnvDepCwd) @@ -432,10 +429,7 @@ CmdSwitch: p = append(p, "..") } p = append(p, doFile) - rel, err := filepath.Rel(Cwd, path.Join(p...)) - if err != nil { - panic(err) - } + rel := mustRel(Cwd, path.Join(p...)) fmt.Println(rel) } case CmdNameRedoTargets: diff --git a/ood.go b/ood.go index 0c8d506..8f3d779 100644 --- a/ood.go +++ b/ood.go @@ -26,7 +26,6 @@ import ( "log" "os" "path" - "path/filepath" "strings" "golang.org/x/sys/unix" @@ -79,23 +78,6 @@ func (e TgtError) Error() string { return fmt.Sprintf("%s: %s", e.Tgt, e.Err) } -func cwdMustRel(paths ...string) string { - rel, err := filepath.Rel(Cwd, path.Join(paths...)) - if err != nil { - panic(err) - } - return rel -} - -func cwdAndTgt(tgt string) (string, string) { - cwd, tgt := path.Split(tgt) - cwd, err := filepath.Abs(cwd) - if err != nil { - panic(err) - } - return cwd, tgt -} - func isSrc(cwd, tgt string) bool { d, f := path.Split(path.Join(cwd, tgt)) if !FileExists(path.Join(d, f)) { @@ -270,11 +252,9 @@ func isOODWithTrace( level int, seen map[string]struct{}, ) (bool, error) { - p, err := filepath.Abs(path.Join(cwd, tgtOrig)) - if err != nil { - panic(err) - } + p := mustAbs(path.Join(cwd, tgtOrig)) _, ood := OODTgts[p] + var err error if ood { if !isOODByBuildUUID(cwd, tgtOrig) { tracef( diff --git a/path.go b/path.go new file mode 100644 index 0000000..efb3e98 --- /dev/null +++ b/path.go @@ -0,0 +1,31 @@ +package main + +import ( + "path" + "path/filepath" +) + +func mustAbs(pth string) string { + pth, err := filepath.Abs(pth) + if err != nil { + panic(err) + } + return pth +} + +func mustRel(basepath, targpath string) string { + pth, err := filepath.Rel(basepath, targpath) + if err != nil { + panic(err) + } + return pth +} + +func cwdMustRel(paths ...string) string { + return mustRel(Cwd, path.Join(paths...)) +} + +func cwdAndTgt(tgt string) (string, string) { + cwd, tgt := path.Split(tgt) + return mustAbs(cwd), tgt +} diff --git a/run.go b/run.go index 90eb7e6..88adf66 100644 --- a/run.go +++ b/run.go @@ -31,7 +31,6 @@ import ( "os" "os/exec" "path" - "path/filepath" "strconv" "strings" "sync" @@ -395,10 +394,7 @@ func runScript(tgtOrig string, errs chan error, forced, traced bool) error { stdoutPath := fdStdout.Name() fdStdout.Close() tmpPath := stdoutPath + ".3" // and for $3 - tmpPathRel, err := filepath.Rel(cwd, tmpPath) - if err != nil { - panic(err) - } + tmpPathRel := mustRel(cwd, tmpPath) args = append( args, path.Join(dirPrefix, tgt), diff --git a/sources.go b/sources.go index b7dcfd4..659df3a 100644 --- a/sources.go +++ b/sources.go @@ -21,16 +21,12 @@ import ( "log" "os" "path" - "path/filepath" ) func sourcesWalker(tgts []string) ([]string, error) { seen := make(map[string]struct{}, 1<<10) for _, tgt := range tgts { - tgtAbsPath, err := filepath.Abs(path.Join(Cwd, tgt)) - if err != nil { - panic(err) - } + tgtAbsPath := mustAbs(path.Join(Cwd, tgt)) cwd, f := path.Split(path.Join(Cwd, tgt)) fdDep, err := os.Open(path.Join(cwd, RedoDir, f+DepSuffix)) if err != nil { @@ -46,10 +42,7 @@ func sourcesWalker(tgts []string) ([]string, error) { } for _, m := range depInfo.ifchanges { depTgt := m["Target"] - depTgtAbsPath, err := filepath.Abs(path.Join(cwd, depTgt)) - if err != nil { - panic(err) - } + depTgtAbsPath := mustAbs(path.Join(cwd, depTgt)) if isSrc(cwd, depTgt) { seen[cwdMustRel(depTgtAbsPath)] = struct{}{} } else if depTgtAbsPath != tgtAbsPath { diff --git a/targets.go b/targets.go index d7c4ebb..6459a2d 100644 --- a/targets.go +++ b/targets.go @@ -21,15 +21,11 @@ import ( "io" "os" "path" - "path/filepath" "strings" ) func targetsCollect(root string, tgts map[string]struct{}) error { - root, err := filepath.Abs(root) - if err != nil { - panic(err) - } + root = mustAbs(root) dir, err := os.Open(root) if err != nil { return ErrLine(err)