]> Cypherpunks.ru repositories - goredo.git/blob - do.go
findDo must return non-relative do-file path
[goredo.git] / do.go
1 /*
2 goredo -- redo implementation on pure Go
3 Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // .do finder
19
20 package main
21
22 import (
23         "os"
24         "path"
25         "path/filepath"
26         "strings"
27 )
28
29 const (
30         EnvTopDir = "REDO_TOP_DIR"
31         TopFile   = "top"
32 )
33
34 var TopDir string
35
36 func existsDo(fdDep *os.File, cwd, pth string) (bool, error) {
37         if _, err := os.Stat(path.Join(cwd, pth)); err == nil {
38                 return true, nil
39         }
40         return false, ifcreate(fdDep, pth)
41 }
42
43 func findDo(fdDep *os.File, cwd, tgt string) (string, int, error) {
44         doFile := tgt + ".do"
45         exists, err := existsDo(fdDep, cwd, doFile)
46         if err != nil {
47                 return "", 0, err
48         }
49         if exists {
50                 return doFile, 0, nil
51         }
52         levels := []string{}
53         extsOrig := strings.Split(tgt, ".")[1:]
54         dirAbsPrev := ""
55         for {
56                 exts := extsOrig
57                 updir := path.Join(levels...)
58                 for len(exts) > 0 {
59                         doFile = strings.Join(append(
60                                 []string{"default"}, append(exts, "do")...,
61                         ), ".")
62                         exists, err = existsDo(fdDep, cwd, path.Join(updir, doFile))
63                         if err != nil {
64                                 return "", 0, err
65                         }
66                         if exists {
67                                 return doFile, len(levels), nil
68                         }
69                         exts = exts[1:]
70                 }
71                 doFile = "default.do"
72                 exists, err = existsDo(fdDep, cwd, path.Join(updir, doFile))
73                 if err != nil {
74                         return "", 0, err
75                 }
76                 if exists {
77                         return doFile, len(levels), nil
78                 }
79                 levels = append(levels, "..")
80                 dirAbs, err := filepath.Abs(updir)
81                 if err != nil {
82                         panic(err)
83                 }
84                 if dirAbs == TopDir {
85                         break
86                 }
87                 if _, err = os.Stat(path.Join(dirAbs, RedoDir, TopFile)); err == nil {
88                         break
89                 }
90                 if dirAbs == dirAbsPrev {
91                         break
92                 }
93                 dirAbsPrev = dirAbs
94         }
95         return "", 0, nil
96 }