Add a small API client, see #11
This commit is contained in:
parent
7e32bfc30b
commit
c44fd6eb1a
|
@ -22,6 +22,7 @@ bld/
|
||||||
*.nupkg
|
*.nupkg
|
||||||
*.nuget.props
|
*.nuget.props
|
||||||
*.nuget.targets
|
*.nuget.targets
|
||||||
|
|
||||||
# Vim
|
# Vim
|
||||||
[._]*.s[a-v][a-z]
|
[._]*.s[a-v][a-z]
|
||||||
[._]*.sw[a-p]
|
[._]*.sw[a-p]
|
||||||
|
@ -34,8 +35,7 @@ tags
|
||||||
[._]*.un~
|
[._]*.un~
|
||||||
|
|
||||||
# Visual Studio Code
|
# Visual Studio Code
|
||||||
.vscode/*
|
.vscode/
|
||||||
!.vscode/settings.json
|
|
||||||
!.vscode/tasks.json
|
# HabitSharp secrets
|
||||||
!.vscode/launch.json
|
habitsharp.json
|
||||||
!.vscode/extensions.json
|
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\HabitSharp\HabitSharp.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.2.5" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||||
|
<AssemblyName>HabitSharp.Client</AssemblyName>
|
||||||
|
<PackageId>HabitSharp.Client</PackageId>
|
||||||
|
<VersionPrefix>0.1.0</VersionPrefix>
|
||||||
|
<Authors>Lucidiot</Authors>
|
||||||
|
<Company>Lucidiot</Company>
|
||||||
|
<Description>Command-line tool for Habitica.</Description>
|
||||||
|
<PackageLicenseUrl>https://gitlab.com/Lucidiot/HabitSharp/blob/master/LICENSE</PackageLicenseUrl>
|
||||||
|
<RepositoryType>git</RepositoryType>
|
||||||
|
<RepositoryUrl>https://gitlab.com/Lucidiot/HabitSharp</RepositoryUrl>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -0,0 +1,73 @@
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue