]> Cypherpunks.ru repositories - goredo.git/commitdiff
DRY filepath.Abs/Rel
authorSergey Matveev <stargrave@stargrave.org>
Fri, 29 Sep 2023 19:44:05 +0000 (22:44 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sat, 30 Sep 2023 09:40:23 +0000 (12:40 +0300)
buildlog.go
cleanup.go
dep.go
do.go
main.go
ood.go
path.go [new file with mode: 0644]
run.go
sources.go
targets.go

index 3bf3ce07f5bd0c4073d079b63d1a06253f13021c..297808080f112443487832145e17ce6753d4bdb4 100644 (file)
@@ -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
        }
index 26dbadce23b7540e6267a862acffd40176f4a628..b978baec1deb255456fa3d6d2936d58ec56e6fb1 100644 (file)
@@ -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 88e2215d89d498c2e0a8d9d8d31ee0f231d73f9a..9792cb0bbcd5b40a6289cdcdccc109ffd6fd7b92 100644 (file)
--- 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 8c2b62c91ffd09ecf7673a612afb937a299440bd..202e964ea486d991d26e088b83eacabe333ef622 100644 (file)
--- 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 f6304aca8dd3b92c4d2a05bb77cc714ebd96842b..025ecbed5be4972146276c386d0fb14810de6975 100644 (file)
--- 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 0c8d506b8ada12b827a8d7c7fba9dae028fbcb5a..8f3d7798d8ccf718b0e5ae8b9052b179e8ea7ade 100644 (file)
--- 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 (file)
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 90eb7e6ec1669c996efb04dd7a2c837b46cb2203..88adf66f2d9e4239b0596f82a9a0617f36d0438b 100644 (file)
--- 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),
index b7dcfd473d0add22302afd8560972b7000feb64c..659df3a51d9ff76e352dcbc104b62e7d01a8be6b 100644 (file)
@@ -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 {
index d7c4ebbc49b6a62a55650fbfe400db373301fa77..6459a2db5529d9061ce393b044073f62e9c2ba71 100644 (file)
@@ -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)