lucidiot/HabitSharp
lucidiot
/
HabitSharp
Archived
1
0
Fork 0
This repository has been archived on 2022-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
HabitSharp/HabitSharp.Client/Program.cs

74 lines
2.6 KiB
C#

using System;
using System.IO;
using HabitSharp;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.Configuration;
namespace HabitClient
{
class Program
{
private static HabiticaApi GetClient() {
// TODO: Gracefully handle missing config and create a boilerplate
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) // TODO: Use home dir?
.AddJsonFile("habitsharp.json")
.Build();
// TODO: Bind to config object?
return new HabiticaApi(config["userId"], config["apiKey"]);
}
static void Main(string[] args)
{
var api = GetClient();
var app = new CommandLineApplication {
Name = "habitica",
Description = "An command-line tool for the Habitica API"
};
app.HelpOption(inherited: true);
app.Command("tags", tagCmd => {
tagCmd.Description = "Manage tags";
tagCmd.OnExecute(() => {
Console.WriteLine("Specify a subcommand");
tagCmd.ShowHelp();
return 1;
});
tagCmd.Command("list", listCmd => {
listCmd.Description = "List all tags";
listCmd.OnExecute(() => {
var tags = api.Tags.List();
Console.WriteLine("Habitica version " + tags.AppVersion.ToString());
foreach (Tag t in tags.Data) {
Console.Write($"{t.Id}: {t.Name}");
Console.WriteLine(t.Challenge ? " (Challenge Tag)" : "");
}
});
});
tagCmd.Command("clear", clearCmd => {
clearCmd.Description = "Delete all tags";
clearCmd.OnExecute(() => {
var tags = api.Tags.List();
Console.WriteLine("Habitica version " + tags.AppVersion.ToString());
foreach (Tag t in tags.Data) {
Console.WriteLine($"Deleting tag {t.Name}");
api.Tags.Delete(t);
}
});
});
});
app.OnExecute(() => {
Console.WriteLine("Specify a subcommand");
app.ShowHelp();
return 1;
});
app.Execute(args);
}
}
}