aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sdp/meta/read_config_files.d
blob: 012ccb60ebba69ad941283a884beeff2dfcb40b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/++
  read configuration files<BR>
  - read config files<BR>
  meta_config_files.d
+/
module sdp.meta.read_config_files;
static template configIn() {
  import
    sdp.meta,
    std.file,
    std.path;
  final string configIn(C,E)(C conf_sdl, E env) {
    /+ FIX clean up conf paths ↓ +/
    string sisudoc_conf_pwd = chainPath(to!string(env["pwd"]), "sisudoc/conf").array;
    string sisudoc_conf_pwd_a = chainPath(to!string(env["pwd"]), "conf").array;
    string sisudoc_conf_pwd_b = chainPath(to!string(env["pwd"]), "../conf").array;
    string sisudoc_conf_pwd_c = chainPath(to!string(env["pwd"]), "../../conf").array;
    string sisudoc_conf_pwd_d = chainPath(to!string(env["pwd"]), "../../../conf").array;
    /+ FIX clean up conf paths ↑
       (compare pwd to doc path location, and build config path)
    +/
    string dot_pwd = chainPath(to!string(env["pwd"]), ".sisu").array;
    string underscore_pwd = chainPath(to!string(env["pwd"]), "_sisu").array;
    string dot_home = chainPath(to!string(env["home"]), ".sisu").array;
    string[] possible_config_path_locations = [
      sisudoc_conf_pwd,
      sisudoc_conf_pwd_a,
      sisudoc_conf_pwd_b,
      sisudoc_conf_pwd_c,
      sisudoc_conf_pwd_d,
      dot_pwd,
      underscore_pwd,
      dot_home,
      "/etc/sisu"
    ];
    string config_file_str;
    foreach(pth; possible_config_path_locations) {
      auto conf_file = format(
        "%s/%s",
        pth,
        conf_sdl,
      );
      if (config_file_str.length > 0) {
        break;
      }
      try {
        if (exists(conf_file)) {
          debug(configfile) {
            writeln(conf_file);
          }
          config_file_str = conf_file.readText;
          break;
        }
      }
      catch (ErrnoException ex) {
      }
      catch (FileException ex) {
      }
    }
    return config_file_str;
  }
}
/+

+/
static template ConfigSDLang() {
  import sdlang;
  import
    sdp.meta,
    std.file,
    std.path;
  auto ConfigSDLang(string configuration, string conf_sdl_filename) {
    Tag sdl_root_conf;
    try {
      sdl_root_conf = parseSource(configuration);
    }
    catch(ParseException e) {
      stderr.writeln("SDLang problem with content for ", conf_sdl_filename);
      stderr.writeln(e.msg);
    }
    return sdl_root_conf;
  }
}
/+
+/
static template configRead() {
  import
    sdp.meta,
    std.file,
    std.path;
  
  final auto configRead(C,E)(C conf_sdl, E env) {
    auto configuration = configIn!()(conf_sdl, env);
    auto sdl_root = ConfigSDLang!()(configuration, conf_sdl);
    return sdl_root;
  }
}