aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sdp/ao_read_config_files.d
blob: 571dbcc5a645bb2ded1af6c6ce957da24c95127f (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/+
  ao_config_files.d
  - read config files
+/
template SiSUconfigIn() {
  private import
    std.exception,
    // std.regex,
    std.stdio,
    std.utf,
    std.conv : to;
  // private import
  //   ao_rgx;       // ao_defaults.d
  // mixin RgxInit;
  // auto rgx = Rgx();
  private
  struct ConfigIn {
    private import std.file;
    final private string readInConfigFile(string conf_sdl) {
      // enforce(
      //   exists(fn_src)!=0,
      //   "file not found"
      // );
      string[] possible_config_path_locations = [
        environment["PWD"] ~ "/.sisu",
        environment["PWD"] ~ "/_sisu",
        environment["HOME"] ~ "/.sisu",
        "/etc/sisu"
      ];
      string config_file_str;
      foreach(pth; possible_config_path_locations) {
        auto conf_file = format(
          "%s/%s",
          pth,
          conf_sdl,
        );
        // writeln(conf_file);
        try {
          if (exists(conf_file)) {
            debug(configfile) {
              writeln(conf_file);
            }
            config_file_str = readText(conf_file);
            break;
          }
        }
        catch (ErrnoException ex) {
        //// Handle errors
        // switch(ex.errno) {
        // case EPERM:
        // case EACCES:
        //   // Permission denied
        //   break;
        // case ENOENT:
        //   // File does not exist
        //   break;
        // default:
        //   // Handle other errors
        //   break;
        // }
        }
        // catch (UTFException ex) {
        //   // Handle validation errors
        // }
        catch (FileException ex) {
          // Handle errors
        }
      }
      return config_file_str;
    }
  }
}
/+

+/
template SiSUconfigSDLang() {
  struct ConfigSDLangRootTag {
    private auto configSDLangRootTag(string configuration, string conf_sdl_filename) {
      Tag sdl_root_conf;
      try {
        sdl_root_conf = parseSource(configuration);
      }
      catch(SDLangParseException e) {
        stderr.writeln("SDLang problem with content for ", conf_sdl_filename);
        // Error messages of the form:
        // myFile.sdl(5:28): Error: Invalid integer suffix.
        stderr.writeln(e.msg);
      }
      debug(sdlang) {
        // Value is a std.variant.Algebraic
        Value output_dir_structure_by = sdl_root_conf.tags["output_dir_structure_by"][0].values[0];
        assert(output_dir_structure_by.type == typeid(string));
        writeln(output_dir_structure_by);
        // Tag person = sdl_root_conf.namespaces["myNamespace"].tags["person"][0];
        // writeln("Name: ", person.attributes["name"][0].value);
        //
        // int age = person.tags["age"][0].values[0].get!int();
        // writeln("Age: ", age);
        writeln("conf SDL:");
        writeln(sdl_root_conf.toSDLDocument());
      }
      return sdl_root_conf;
    }
  }
}
/+

+/
template SiSUconfigSDLangHub() {
  mixin SiSUconfigIn;
  mixin SiSUconfigSDLang;
  struct ConfigHub {
    final private auto configSDLang(string conf_sdl) {
      auto conf_get = ConfigIn();
      auto configuration = conf_get.readInConfigFile(conf_sdl);
      auto conf = ConfigSDLangRootTag();
      auto sdl_root = conf.configSDLangRootTag(configuration, conf_sdl);
      return sdl_root;
    }
  }
}