示例#1
0
 public KeyValueData(bool showProcess             = false,
                     KeyValueLogType outputFilter = KeyValueLogType.Info)
 {
     ShowProcess  = showProcess;
     keyValues    = new Dictionary <string, string>();
     logs         = new List <KeyValueLog>();
     users        = new List <KeyValueUser>();
     Status       = ProcessStatus.Ok;
     OutputFilter = outputFilter;
 }
示例#2
0
        public static KeyValueLog New(KeyValueLogType type, FileInfo file, string what)
        {
            var result = new KeyValueLog()
            {
                LogType = type,
                When    = DateTime.Now,
                File    = file,
                What    = what
            };

            return(result);
        }
示例#3
0
        public static KeyValueData Load(string path, bool recursive  = false,
                                        bool showProcess             = false,
                                        KeyValueLogType outputFilter = KeyValueLogType.Info)
        {
            var files = Directory.EnumerateFiles(path, "*.*",
                                                 recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
                        .Where(x => x.EndsWith(".yml") || x.EndsWith(".yaml"))
                        .Select(x => new { file = x, firstLine = File.ReadLines(x).FirstOrDefault() })
                        .Where(x => x.firstLine != null && x.firstLine.StartsWith("kv-configurator: 1.0"))
                        .Select(x => x.file);

            KeyValueData result = new KeyValueData(showProcess);

            foreach (var file in files)
            {
                var config = ConfigUtils.LoadConfig(file);
                var fi     = new FileInfo(file);

                if (config.etcd is JObject etcdJo)
                {
                    var data = etcdJo.GetParents();

                    var host = data["host"] as string;
                    var port = data["port"] as string;

                    result.InitEtcd(host, port);
                }

                if (config.enviroment is JObject envJo)
                {
                    var envs = envJo.GetParents();

                    foreach (var env in envs)
                    {
                        var envData = env.Value.GetParents();

                        if (envData.ContainsKey("user"))
                        {
                            var usrData  = envData["user"].GetParents();
                            var name     = usrData["name"] as string;
                            var password = usrData["pass"] as string;

                            result.AddUser(name, password, $"{env.Key}");
                        }
                    }
                }

                if (config.application is JObject appJo)
                {
                    var apps = appJo.GetParents();

                    foreach (var app in apps)
                    {
                        if (app.Value == null)
                        {
                            result.Warning(fi, $"Application {app.Key} does not contain environments");
                            continue;
                        }

                        var envs = app.Value.GetParents();

                        foreach (var env in envs)
                        {
                            var envData = env.Value.GetParents();

                            var prefix = $"cfg/{env.Key}/{app.Key}";

                            if (envData.ContainsKey("tree"))
                            {
                                result.TreeToFlat(prefix, envData["tree"]);
                            }

                            if (envData.ContainsKey("files"))
                            {
                                result.LoadFiles(fi.DirectoryName, prefix, envData["files"]);
                            }

                            if (envData.ContainsKey("user"))
                            {
                                var userData = envData["user"].GetParents();

                                var name = userData["name"] as string;
                                var pass = userData["pass"] as string;

                                result.AddUser(name, pass, $"{env.Key}/{app.Key}");
                            }
                        } //envs
                    }     //apps
                }

                result.Info(fi, "File processed");
            }

            return(result);
        }