private static void AddGetter(this Command command) { var c = new Command("get", "Gets a system setting") { new Argument <string>("property") }; c.Handler = CommandHandler.Create(async(bool verbose, string property) => { var store = GetSettingsStore(); var propertyInfo = typeof(Settings).GetProperty(property, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (propertyInfo is null) { var properties = typeof(Settings).GetProperties().Select(info => info.Name); ConsoleRendering.ClearLineAndWriteError( $"Invalid property. Available properties: {string.Join(",", properties)}"); return(5); } if (verbose) { ConsoleRendering.ClearLineAndWrite("Loading settings..."); } var settings = await store.ReadOrCreateAsync(); ConsoleRendering.ClearLineAndWrite($"{propertyInfo.GetValue(settings)}\n"); return(0); }); command.Add(c); }
private static void EditNote(this Command command) { var c = new Command("edit", "Edits the description for a note") { new Argument <Guid>("id", "Id for the note"), new Argument <string>("description", "New description for the note") }; c.Handler = CommandHandler.Create(async(Guid id, string description) => { var store = new NoteStore(); try { var item = await store.GetByIdAsync(id); item.Text = description; await store.UpdateAsync(item); await store.SaveChangesAsync(); } catch (ItemNotFoundException e) { ConsoleRendering.ClearLineAndWriteError(e.Message); } }); command.Add(c); }
private static void AddNote(this Command command) { var c = new Command("add", "Adds a note") { new Argument <string>("description", "Description of the note") }; c.Handler = CommandHandler.Create(async(string description) => { var store = new NoteStore(); try { await store.AddAsync(new Note { Text = description }); await store.SaveChangesAsync(); } catch (UniquenessConstraintViolationException e) { ConsoleRendering.ClearLineAndWriteError(e.Message); } }); command.Add(c); }
private static void RemoveNote(this Command command) { var c = new Command("remove", "Removes a note") { new Argument <Guid>("id", "Id for the note") }; c.AddAlias("rm"); c.Handler = CommandHandler.Create(async(Guid id) => { var store = new NoteStore(); try { await store.RemoveAsync(id); await store.SaveChangesAsync(); } catch (ItemNotFoundException e) { ConsoleRendering.ClearLineAndWriteError(e.Message); } }); command.Add(c); }
private static void RemoveCountry(this Command command) { var c = new Command("remove", "Removes a country") { new Argument <int>("country-code", "Code for the country") }; c.AddAlias("rm"); c.Handler = CommandHandler.Create(async(int countryCode) => { var store = GetCountriesStore(); try { await store.RemoveAsync(countryCode); await store.SaveChangesAsync(); } catch (ItemNotFoundException e) { ConsoleRendering.ClearLineAndWriteError(e.Message); } }); command.Add(c); }
private static void AddSetter(this Command command) { var c = new Command("set", "Sets a system configuration") { new Argument <string>("property"), new Argument <string>("value") }; c.Handler = CommandHandler.Create(async(bool verbose, string property, string value) => { var store = GetSettingsStore(); var propertyInfo = typeof(Settings).GetProperty(property, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (propertyInfo is null) { var properties = typeof(Settings).GetProperties().Select(info => info.Name); ConsoleRendering.ClearLineAndWriteError( $"Invalid property. Available properties: {string.Join(",", properties)}"); return(5); } object castedValue = propertyInfo.Name switch { nameof(Settings.Name) => value, nameof(Settings.Email) => value, nameof(Settings.Age) => int.Parse(value), _ => throw new ArgumentOutOfRangeException(nameof(property), property, $"Can't change property '{property}'") }; if (verbose) { ConsoleRendering.ClearLineAndWrite("Loading settings..."); } var settings = await store.ReadOrCreateAsync(); if (verbose) { ConsoleRendering.ClearLineAndWrite("Saving..."); } propertyInfo.SetValue(settings, castedValue); await store.SaveAsync(settings); if (verbose) { ConsoleRendering.ClearLine(); } return(0); }); command.Add(c); }
private static void AddCountry(this Command command) { var c = new Command("add", "Add a country") { new Argument <int>("country-code", "Code for the country"), new Argument <string>("name", "Name of the country") }; c.Handler = CommandHandler.Create(async(Country country) => { var store = GetCountriesStore(); try { await store.AddAsync(country); await store.SaveChangesAsync(); } catch (UniquenessConstraintViolationException e) { ConsoleRendering.ClearLineAndWriteError(e.Message); } }); command.Add(c); }