예제 #1
0
파일: Session.cs 프로젝트: wiltaylor/FCE
        public void Apply(string script, string settings, bool testOnly = false)
        {
            if (settings != null && !_fileSystem.FileExist(settings))
            {
                _console.Error("Unable to find settings file {file}", settings);
                return;
            }

            if (!Validate(script))
            {
                return;
            }

            try
            {
                _gatherManager.Run();
            }
            catch (Exception e)
            {
                _console.Error("There was an error while running gathers! Error: {error}", e);
                return;
            }

            try
            {
                var settingData = settings == null
                    ? null
                    : JsonConvert.DeserializeObject <Dictionary <string, string> >(_fileSystem.ReadFile(settings));

                var runlist = _configManager.BuildRunList(settingData);
                var results = testOnly ? _configManager.Test(runlist).ToArray() : _configManager.ApplyRunList(runlist).ToArray();

                if (results.Any(r => r.State == ResourceState.NotConfigured))
                {
                    _environmentHelper.SetExitCode(ExitCodes.Error);
                }
                else if (results.Any(r => r.State == ResourceState.NeedReboot))
                {
                    _environmentHelper.SetExitCode(ExitCodes.Reboot);
                }
                else
                {
                    _environmentHelper.SetExitCode(ExitCodes.Ok);
                }

                _fileSystem.WriteFile("data.json", _dataStore.GetPersistString());
                _fileSystem.WriteFile("result.json", JsonConvert.SerializeObject(results));
            }
            catch (Exception e)
            {
                _console.Error("There was an error while trying to read {file} Error: {error}", settings, e);
            }
        }