]> Cypherpunks.ru repositories - goredo.git/blob - do.go
Various refactoring
[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                         pth := path.Join(updir, doFile)
63                         exists, err = existsDo(fdDep, cwd, pth)
64                         if err != nil {
65                                 return "", 0, err
66                         }
67                         if exists {
68                                 return doFile, len(levels), nil
69                         }
70                         exts = exts[1:]
71                 }
72                 doFile = "default.do"
73                 pth := path.Join(updir, doFile)
74                 exists, err = existsDo(fdDep, cwd, pth)
75                 if err != nil {
76                         return "", 0, err
77                 }
78                 if exists {
79                         return pth, len(levels), nil
80                 }
81                 levels = append(levels, "..")
82                 dirAbs, err := filepath.Abs(updir)
83                 if err != nil {
84                         panic(err)
85                 }
86                 if dirAbs == TopDir {
87                         break
88                 }
89                 if _, err = os.Stat(path.Join(dirAbs, RedoDir, TopFile)); err == nil {
90                         break
91                 }
92                 if dirAbs == dirAbsPrev {
93                         break
94                 }
95                 dirAbsPrev = dirAbs
96         }
97         return "", 0, nil
98 }