public ParametersModel Parse(string[] args) { var p = new ParametersModel(); var options = new Mono.Options.OptionSet(); options.Add("h|help", "prints the help", s => p.Action = ActionType.WriteDescripton); options.Add("t|test-connection", "tries to connect to the server using the specified credentials", s => p.Action = ActionType.TestConnection); options.Add("b|base-folder=", "use the supplied directory as the base folder for the import", s => p.BaseFolder = s); options.Add("v|vault=", "use the specified vault", s => p.Vault = s); options.Add("u|user="******"use the specified user", s => p.User = s); options.Add("s|server=", "use the specified server", s => p.Server = s); options.Add("p|password="******"use the specified password", s => p.Password = s); options.Add("c|csv=", "import based on the csv file", s => { p.Action = ActionType.CsvImport; p.CsvImportFile = s; }); options.Add("d|delimiter=", "csv delimiter", s => p.Delimiter = s); options.Add("destroy-by-class=", "destroy objects of the following classes, use Comma ',' as a separator for multiple classes", s => { p.Action = ActionType.DestroyObjectsOfClasses; p.DestroyObjectOfClasses = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); }); options.Add("destroy-by-objecttype=", "destroy objects of the specified object types, use Comma ',' as a separator for multiple classes", s => { p.Action = ActionType.DestroyObjectsOfObjectTypes; p.DestroyObjectsOfObjectTypes = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); }); options.Add("lc|list-classes", "list all classes and their object counts", s => p.Action = ActionType.ListClassesAndObjects); options.Add("lo|list-objecttypes", "list all object types and their object counts", s => p.Action = ActionType.ListObjectTypesAndObjects); options.Parse(args); p.Options = options; return p; }
private Vault CreateVault(SettingsModel settings, ParametersModel parameters) { /* use the parameters if specified, else fallback to the settings */ string user = string.IsNullOrEmpty(parameters.User) ? settings.User : parameters.User; string password = string.IsNullOrEmpty(parameters.Password) ? settings.Password : parameters.Password; string vault = string.IsNullOrEmpty(parameters.Vault) ? settings.Vault : parameters.Vault; string networkAddress = string.IsNullOrEmpty(parameters.Server) ? settings.Server : parameters.Server; return CreateVault(user, password, vault, networkAddress); }
private void CsvImport(ParametersModel p) { /* listen for CTRL-C */ bool cancelRequested = false; Console.CancelKeyPress += (o, e) => { e.Cancel = true; cancelRequested = true; }; Stopwatch sw = Stopwatch.StartNew(); var successful = new List<CsvModel>(); var unsuccessful = new List<CsvModel>(); var ignored = new List<CsvModel>(); string delimiter = p.Delimiter != string.Empty ? p.Delimiter : ","; DateTime now = DateTime.Now; string outputFile = GetOutputFilePath(p.CsvImportFile, now); Log(string.Format("reading Csv file: '{0}'", p.CsvImportFile)); var csvModels = this.CsvParserService.GetCsvModels(p.CsvImportFile, delimiter).ToArray(); Log(string.Format("found {0} models", csvModels.Length)); /* lock the input file for exclusive access, as we want to move it later on */ using (var fsInput = File.OpenWrite(p.CsvImportFile)) { Log(string.Format("writing result to file: {0}", outputFile)); using (var fsOutput = File.OpenWrite(outputFile)) using (var twOutput = new StreamWriter(fsOutput, Encoding.Default)) { Log(string.Format("logging successful imports to file: '{0}'", p.SuccessFile)); Log(string.Format("logging unsuccessful imports to file: '{0}'", p.FailureFile)); int index = 0; int tried = 0; using (var successWriter = this.ResultWriterService.GetSuccessWriter()) using (var failureWriter = this.ResultWriterService.GetFailureWriter()) { foreach (var csvModel in csvModels) { /* ignore already successful imports */ string importStatus = null; if (csvModel.Values.TryGetValue(CsvHeaders.ImportStatus, out importStatus) && importStatus == ImportStatusSuccess) ignored.Add(csvModel); else { try { tried++; int id = this.CsvImporterService.Import(csvModel); successful.Add(csvModel); successWriter.WriteLine(csvModel.ToString()); csvModel.Values[CsvHeaders.ImportStatus] = ImportStatusSuccess; csvModel.Values[CsvHeaders.Id] = id.ToString(); } catch (Exception ex) { unsuccessful.Add(csvModel); csvModel.Values[CsvHeaders.ImportStatus] = "failed due to error: " + ex.Message; failureWriter.WriteLine(string.Format("failed to import:")); failureWriter.WriteLine(csvModel.ToString()); failureWriter.WriteLine(string.Format("because of the following error:")); failureWriter.WriteLine(ex); } } int successRate = (int)(successful.Count * 100f / (tried != 0 ? tried : 1)); int failureRate = (int)(unsuccessful.Count * 100f / (tried != 0 ? tried : 1)); int ignoredRate = (int)(ignored.Count * 100f / (index != 0 ? index : 1)); int percent = (int)(++index * 100f / csvModels.Length); Log(string.Format("Progress: {0}/{1} ({2} %) ", index, csvModels.Length, percent)); Log(string.Format("Success: {0}/{1} ({2} %) ", successful.Count, tried, successRate)); Log(string.Format("Failure: {0}/{1} ({2} %) ", unsuccessful.Count, tried, failureRate)); Log(string.Format("Ignored: {0}/{1} ({2} %) ", ignored.Count, csvModels.Length, ignoredRate)); if (cancelRequested) { Log("Import cancelled!"); break; } if (index < csvModels.Length) Console.CursorTop = Console.CursorTop - 4; } this.CsvWriterService.Write(csvModels, twOutput, delimiter); } } Log(string.Format("ignored {0}/{1} ({2:0.00} %) as they were already successfully imported", ignored.Count, csvModels.Length, ignored.Count * 100f / csvModels.Length)); Log(string.Format("imported {0}/{1} ({2:0.00} %) successfully", successful.Count, csvModels.Length - ignored.Count, successful.Count * 100f / (csvModels.Length - ignored.Count))); sw.Stop(); Log(string.Format("importing took {0}", sw.Elapsed)); } /* rename the input file */ string backupFile = GetBackupFilePath(p.CsvImportFile, now); Log(string.Format("backing up input file to '{0}'", backupFile)); File.Move(p.CsvImportFile, backupFile); /* rename the output file */ Log(string.Format("replacing the input file")); File.Move(outputFile, p.CsvImportFile); }
private int Run(ParametersModel p) { if ((p.Action & ActionType.TestConnection) == ActionType.TestConnection) Log(this.ConnectionTestService.TestConnection() ? "Connection successful" : "Connection failed."); if ((p.Action & ActionType.WriteDescripton) == ActionType.WriteDescripton) WriteDescription(p); if ((p.Action & ActionType.CsvImport) == ActionType.CsvImport) CsvImport(p); if ((p.Action & ActionType.DestroyObjectsOfClasses) == ActionType.DestroyObjectsOfClasses) DestroyObjectsByClass(p); if ((p.Action & ActionType.DestroyObjectsOfObjectTypes) == ActionType.DestroyObjectsOfObjectTypes) DestroyObjectsByObjectType(p); if ((p.Action & ActionType.ListClassesAndObjects) == ActionType.ListClassesAndObjects) ListClassesAndObjects(p); if ((p.Action & ActionType.ListObjectTypesAndObjects) == ActionType.ListObjectTypesAndObjects) ListObjectTypesAndObjects(p); return 0; }
private void WriteDescription(ParametersModel p) { var writer = GetLogWriter(); writer.WriteLine("MFilesImporter written by martin kramer <*****@*****.**>"); p.Options.WriteOptionDescriptions(writer); }
private void ListObjectTypesAndObjects(ParametersModel p) { var objectTypes = this.ObjectTypeService.GetObjectTypes(); foreach (string objectType in objectTypes) { var searchResults = this.FindObjectsService.FindObjectsByObjectType(objectType); Log(string.Format("object type: '{0}' has {1} objects", objectType, searchResults.Count)); } }
private void ListClassesAndObjects(ParametersModel p) { var classes = this.ClassesService.GetClasses(); foreach (string className in classes) { var searchResults = this.FindObjectsService.FindObjectsByClass(className); Log(string.Format("class '{0}' has {1} objects", className, searchResults.Count)); } }
private void DestroyObjectsByObjectType(ParametersModel p) { foreach (string objectType in p.DestroyObjectsOfObjectTypes) { Log(string.Format("destroying objects of object type: '{0}'", objectType)); int count = this.DestroyObjectService.DestroyObjectsByObjectType(objectType); Log(string.Format("destroyed {0} objects", count)); } }
private void DestroyObjectsByClass(ParametersModel p) { foreach (string className in p.DestroyObjectOfClasses) { Log(string.Format("destroying objects of class: '{0}'", className)); int count = this.DestroyObjectService.DestroyObjectsByClass(className); Log(string.Format("destroyed {0} objects", count)); } }
public ParametersService(IParametersFactory parametersFactory, ArgumentsModel arguments) { this.Parameters = parametersFactory.Parse(arguments.Arguments); }