From f2c78750b13fc41cf9f3a562a150ed73b850757c Mon Sep 17 00:00:00 2001 From: garyng Date: Sat, 14 Nov 2020 23:36:12 +0800 Subject: [PATCH 01/15] build: add docker build and publish --- .github/workflows/release.yml | 79 +++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..1f32c72 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,79 @@ +name: Docker Image Build and Publish +on: push + +jobs: + build: + runs-on: ubuntu-latest + if: "!contains(github.event.head_commit.message, '[skip ci]')" + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Install GitVersion + uses: gittools/actions/gitversion/setup@v0.9.5 + with: + versionSpec: "5.x" + + - name: Versioning + id: gitversion + uses: gittools/actions/gitversion/execute@v0.9.5 + + + - name: Build and push + uses: docker/build-push-action@v2 + with: + context: ./src/Tss.Api + file: ./Dockerfile + # platforms: linux/amd64,linux/arm64,linux/386 + push: true + tags: garyng/tss:latest, garyng/tss:${{ steps.gitversion.outputs.fullSemVer }} + + + + # - name: Create directory + # run: | + # md -Force ${{ env.artifacts-dir }} + # shell: pwsh + + # - name: Set variable + # id: vars + # run: echo "::set-output name=filepath::${{ env.artifacts-dir }}${{ env.archive-name }}-${{ steps.gitversion.outputs.fullSemVer }}.zip" + + # - name: Zip + # run: | + # Compress-Archive -Path ${{ env.out-dir }}/* -DestinationPath ${{ steps.vars.outputs.filepath }} + # shell: pwsh + + # - name: Hash + # run: | + # $hash = $(Get-FileHash ${{ steps.vars.outputs.filepath }}).Hash + # $hash | Set-Content ${{ steps.vars.outputs.filepath }}.sha256 + # shell: pwsh + + # - name: Upload Artifacts + # uses: actions/upload-artifact@v2 + # with: + # path: ${{ env.artifacts-dir }} + + # - name: Release Github + # uses: softprops/action-gh-release@master + # env: + # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # with: + # draft: true + # files: "${{ env.artifacts-dir }}/*" + # name: ${{ steps.gitversion.outputs.fullSemVer }} + # tag_name: ${{ steps.gitversion.outputs.fullSemVer }} \ No newline at end of file From 4e8955f5d9a48d23549541955f3faca87e45e308 Mon Sep 17 00:00:00 2001 From: garyng Date: Sat, 14 Nov 2020 23:38:27 +0800 Subject: [PATCH 02/15] build: update checkout fetch-depth for gitversion --- .github/workflows/release.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1f32c72..ed5c4fd 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -9,6 +9,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v2 + with: + fetch-depth: 0 - name: Set up QEMU uses: docker/setup-qemu-action@v1 From 5d6f5fc01f6ae7dae091deb820a300b5fd2a6f8f Mon Sep 17 00:00:00 2001 From: garyng Date: Sat, 14 Nov 2020 23:40:16 +0800 Subject: [PATCH 03/15] build: update dockerfile path --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ed5c4fd..268b6ad 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -38,7 +38,7 @@ jobs: uses: docker/build-push-action@v2 with: context: ./src/Tss.Api - file: ./Dockerfile + file: ./src/Tss.Api/Dockerfile # platforms: linux/amd64,linux/arm64,linux/386 push: true tags: garyng/tss:latest, garyng/tss:${{ steps.gitversion.outputs.fullSemVer }} From 03836b7ca5467970c8942699e93cf0e57342fce3 Mon Sep 17 00:00:00 2001 From: garyng Date: Sat, 14 Nov 2020 23:42:46 +0800 Subject: [PATCH 04/15] build: update context path --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 268b6ad..f47b7d2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -37,7 +37,7 @@ jobs: - name: Build and push uses: docker/build-push-action@v2 with: - context: ./src/Tss.Api + context: ./src/ file: ./src/Tss.Api/Dockerfile # platforms: linux/amd64,linux/arm64,linux/386 push: true From daeaddcc89f648799c05d3eb52de1a37ee7807ec Mon Sep 17 00:00:00 2001 From: garyng Date: Sun, 15 Nov 2020 02:10:18 +0800 Subject: [PATCH 05/15] fix: remove gitversion task LibGit2Sharp is not supported on Debian/Ubuntu https://github.com/GitTools/GitVersion/issues/2260#issuecomment-625741864 --- src/Tss.Api/Tss.Api.csproj | 4 ---- src/Tss.Cli/Tss.Cli.csproj | 4 ---- src/Tss.Core/Tss.Core.csproj | 4 ---- 3 files changed, 12 deletions(-) diff --git a/src/Tss.Api/Tss.Api.csproj b/src/Tss.Api/Tss.Api.csproj index 5aebd64..cd36073 100644 --- a/src/Tss.Api/Tss.Api.csproj +++ b/src/Tss.Api/Tss.Api.csproj @@ -14,10 +14,6 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/src/Tss.Cli/Tss.Cli.csproj b/src/Tss.Cli/Tss.Cli.csproj index 64529f7..8ab3e34 100644 --- a/src/Tss.Cli/Tss.Cli.csproj +++ b/src/Tss.Cli/Tss.Cli.csproj @@ -20,10 +20,6 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/src/Tss.Core/Tss.Core.csproj b/src/Tss.Core/Tss.Core.csproj index 5d42931..0f60666 100644 --- a/src/Tss.Core/Tss.Core.csproj +++ b/src/Tss.Core/Tss.Core.csproj @@ -5,10 +5,6 @@ enable - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - From 59740c5877180d8ad581b940788bad9e1fa7daaf Mon Sep 17 00:00:00 2001 From: garyng Date: Sun, 15 Nov 2020 03:00:43 +0800 Subject: [PATCH 06/15] build: create github release --- .github/workflows/release.yml | 45 +++++++---------------------------- 1 file changed, 8 insertions(+), 37 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f47b7d2..8c67627 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -39,43 +39,14 @@ jobs: with: context: ./src/ file: ./src/Tss.Api/Dockerfile - # platforms: linux/amd64,linux/arm64,linux/386 push: true tags: garyng/tss:latest, garyng/tss:${{ steps.gitversion.outputs.fullSemVer }} - - - # - name: Create directory - # run: | - # md -Force ${{ env.artifacts-dir }} - # shell: pwsh - - # - name: Set variable - # id: vars - # run: echo "::set-output name=filepath::${{ env.artifacts-dir }}${{ env.archive-name }}-${{ steps.gitversion.outputs.fullSemVer }}.zip" - - # - name: Zip - # run: | - # Compress-Archive -Path ${{ env.out-dir }}/* -DestinationPath ${{ steps.vars.outputs.filepath }} - # shell: pwsh - - # - name: Hash - # run: | - # $hash = $(Get-FileHash ${{ steps.vars.outputs.filepath }}).Hash - # $hash | Set-Content ${{ steps.vars.outputs.filepath }}.sha256 - # shell: pwsh - - # - name: Upload Artifacts - # uses: actions/upload-artifact@v2 - # with: - # path: ${{ env.artifacts-dir }} - - # - name: Release Github - # uses: softprops/action-gh-release@master - # env: - # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - # with: - # draft: true - # files: "${{ env.artifacts-dir }}/*" - # name: ${{ steps.gitversion.outputs.fullSemVer }} - # tag_name: ${{ steps.gitversion.outputs.fullSemVer }} \ No newline at end of file + - name: Release Github + uses: softprops/action-gh-release@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + draft: true + name: ${{ steps.gitversion.outputs.fullSemVer }} + tag_name: ${{ steps.gitversion.outputs.fullSemVer }} \ No newline at end of file From f2a90feb80e89e5a85183147525d1795cadf84b4 Mon Sep 17 00:00:00 2001 From: garyng Date: Sun, 15 Nov 2020 03:15:10 +0800 Subject: [PATCH 07/15] docs: add badges --- readme.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 436f1f3..0aeb865 100644 --- a/readme.md +++ b/readme.md @@ -1,10 +1,13 @@ # Tss +[![Docker Image Version (latest semver)](https://img.shields.io/docker/v/garyng/tss)]((https://hub.docker.com/r/garyng/tss) +) ![Docker Image Size (tag)](https://img.shields.io/docker/image-size/garyng/tss/latest) + > This Song S**ks A dumb playlist management tool. -# (My) workflow +# (My) Workflow 1. Gather a bunch of tracks into a Spotify playlist, normally called `xxx (pending)` 1. Create two more playlists: From 721c2f79c81edcd7357f9c3db6b6c41b0993f217 Mon Sep 17 00:00:00 2001 From: garyng Date: Sun, 15 Nov 2020 03:48:11 +0800 Subject: [PATCH 08/15] misc: cleanup unused code in cli --- src/Tss.Cli/Program.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/Tss.Cli/Program.cs b/src/Tss.Cli/Program.cs index 75bf067..892c5ff 100644 --- a/src/Tss.Cli/Program.cs +++ b/src/Tss.Cli/Program.cs @@ -45,15 +45,6 @@ static async Task Main(string[] args) public class Startup { private readonly StandaloneTssService _service; - //private readonly IOptions _config; - //private readonly IOptionsMonitor _mappings; - - //public Startup(IOptions config, IOptionsMonitor mappings) - //{ - // _config = config; - // _mappings = mappings; - // //_service = service; - //} public Startup(StandaloneTssService service) { From 532e1a2858f8e0c7300044a2d062d8bf5832db80 Mon Sep 17 00:00:00 2001 From: garyng Date: Sun, 15 Nov 2020 03:57:10 +0800 Subject: [PATCH 09/15] feat: override callback and docker ports during debugging default to 8123, debugging will use 8124 --- src/Tss.Core/StandaloneTssService.cs | 4 +--- src/Tss.Core/TssConfig.cs | 1 + src/Tss.Core/TssService.cs | 8 +++++--- src/docker-compose.override.yml | 5 +++++ 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Tss.Core/StandaloneTssService.cs b/src/Tss.Core/StandaloneTssService.cs index 6029663..fe86835 100644 --- a/src/Tss.Core/StandaloneTssService.cs +++ b/src/Tss.Core/StandaloneTssService.cs @@ -7,8 +7,6 @@ namespace Tss.Core { public class StandaloneTssService : TssService { - protected const int CALLBACK_PORT = 8123; - public StandaloneTssService(IOptions config, IOptionsMonitor mappings) : base(config, mappings) { @@ -19,7 +17,7 @@ public StandaloneTssService(IOptions config, IOptionsMonitor public async Task Login() { - var server = new EmbedIOAuthServer(new Uri(CALLBACK_URL), CALLBACK_PORT); + var server = new EmbedIOAuthServer(new Uri(_callbackUrl), _callbackPort); await server.Start(); var auth = new TaskCompletionSource(); diff --git a/src/Tss.Core/TssConfig.cs b/src/Tss.Core/TssConfig.cs index ff1635a..887f731 100644 --- a/src/Tss.Core/TssConfig.cs +++ b/src/Tss.Core/TssConfig.cs @@ -5,5 +5,6 @@ public class TssConfig public string ClientId { get; set; } public string CredentialsPath { get; set; } public string MappingsPath { get; set; } + public int CallbackPort { get; set; } = 8123; } } \ No newline at end of file diff --git a/src/Tss.Core/TssService.cs b/src/Tss.Core/TssService.cs index aca4f23..3825491 100644 --- a/src/Tss.Core/TssService.cs +++ b/src/Tss.Core/TssService.cs @@ -10,10 +10,10 @@ namespace Tss.Core { public class TssService { - protected const string CALLBACK_URL = "http://localhost:8123/callback"; - protected string _clientId; protected string _credentialsPath; + protected string _callbackUrl; + protected int _callbackPort; protected TssLoginFlow _loginFlow; protected SpotifyClient _client; @@ -25,6 +25,8 @@ public TssService(IOptions config, IOptionsMonitor mappi var c = config.Value; _clientId = c.ClientId; _credentialsPath = c.CredentialsPath; + _callbackUrl = $"http://localhost:{c.CallbackPort}/callback"; + _callbackPort = c.CallbackPort; } public async Task TryLogin() @@ -36,7 +38,7 @@ public async Task TryLogin() return new TryLoginResult(true, null); } - _loginFlow = new TssLoginFlow(_clientId, CALLBACK_URL); + _loginFlow = new TssLoginFlow(_clientId, _callbackUrl); var url = await _loginFlow.Start(); return new TryLoginResult(false, url); } diff --git a/src/docker-compose.override.yml b/src/docker-compose.override.yml index 6918247..0f6d014 100644 --- a/src/docker-compose.override.yml +++ b/src/docker-compose.override.yml @@ -4,3 +4,8 @@ services: tss.api: environment: - ASPNETCORE_ENVIRONMENT=Development + ports: + - '8124:80' + env_file: + - .env + - override.env \ No newline at end of file From 41d9038b293e6ef2070d6ac290003e252705b6e4 Mon Sep 17 00:00:00 2001 From: garyng Date: Sun, 15 Nov 2020 03:57:31 +0800 Subject: [PATCH 10/15] fix: check if client is null everywhere --- src/Tss.Api/Controllers/TssController.cs | 2 +- src/Tss.Core/TssService.cs | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Tss.Api/Controllers/TssController.cs b/src/Tss.Api/Controllers/TssController.cs index e6284e5..1b6feab 100644 --- a/src/Tss.Api/Controllers/TssController.cs +++ b/src/Tss.Api/Controllers/TssController.cs @@ -41,7 +41,7 @@ public async Task> Callback([FromQuery] string? code, [From return Unauthorized($"Error: {error}"); } - await _service.CompleteLogin(code); + await _service.CompleteLogin(code!); return Ok("Spotify Authorization was successful. You can close this tab now."); } diff --git a/src/Tss.Core/TssService.cs b/src/Tss.Core/TssService.cs index 3825491..cbdd356 100644 --- a/src/Tss.Core/TssService.cs +++ b/src/Tss.Core/TssService.cs @@ -16,7 +16,7 @@ public class TssService protected int _callbackPort; protected TssLoginFlow _loginFlow; - protected SpotifyClient _client; + protected SpotifyClient? _client; private IOptionsMonitor _mappings; public TssService(IOptions config, IOptionsMonitor mappings) @@ -43,9 +43,9 @@ public async Task TryLogin() return new TryLoginResult(false, url); } - public async Task CompleteLogin(string? code) + public async Task CompleteLogin(string code) { - var token = await _loginFlow?.Complete(code); + var token = await _loginFlow!.Complete(code); await CreateClient(token); } @@ -95,6 +95,8 @@ public async Task MoveCurrentToNotGood() public async Task MoveCurrentTo(Func getPlaylistId, bool skip = true) { + if (_client == null) return; + // todo: return track name, playlist name (source and target)? var current = await Current(); @@ -129,6 +131,7 @@ private string GetTargetPlaylistId(string? currentPlaylistId, Func Current() { + if (_client == null) return (null, null); + var current = await _client.Player.GetCurrentlyPlaying(new PlayerCurrentlyPlayingRequest()); var currentPlaylistId = current?.Context.Uri.Replace("spotify:playlist:", ""); From 186a49006cb7007a2cdd9cb52d65172716d2ce4b Mon Sep 17 00:00:00 2001 From: garyng Date: Sun, 15 Nov 2020 04:02:53 +0800 Subject: [PATCH 11/15] docs: fix badges [skip ci] --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 0aeb865..190daba 100644 --- a/readme.md +++ b/readme.md @@ -1,7 +1,7 @@ # Tss -[![Docker Image Version (latest semver)](https://img.shields.io/docker/v/garyng/tss)]((https://hub.docker.com/r/garyng/tss) -) ![Docker Image Size (tag)](https://img.shields.io/docker/image-size/garyng/tss/latest) +[![Docker Image Version (latest semver)](https://img.shields.io/docker/v/garyng/tss)](https://hub.docker.com/r/garyng/tss) +![Docker Image Size (tag)](https://img.shields.io/docker/image-size/garyng/tss/latest) > This Song S**ks From 693483e49807fd833d53a7c56df7121a0285ec91 Mon Sep 17 00:00:00 2001 From: garyng Date: Sun, 15 Nov 2020 04:15:55 +0800 Subject: [PATCH 12/15] fix: remove port 8123 docker-compose concatenates ports, instead of replacing --- src/docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/docker-compose.yml b/src/docker-compose.yml index 614e30d..362ea05 100644 --- a/src/docker-compose.yml +++ b/src/docker-compose.yml @@ -7,7 +7,7 @@ services: context: . dockerfile: Tss.Api/Dockerfile env_file: .env - ports: - - '8123:80' + # ports: + # - '8123:80' volumes: - ../data:/data From b970fb727d4d6f71c1629f648879121040cdd045 Mon Sep 17 00:00:00 2001 From: garyng Date: Sun, 15 Nov 2020 04:53:16 +0800 Subject: [PATCH 13/15] feat: add serilog for logging --- src/Tss.Api/Controllers/TssController.cs | 4 +--- src/Tss.Api/Program.cs | 14 ++++++++++++++ src/Tss.Api/Tss.Api.csproj | 4 ++++ src/Tss.Api/appsettings.Development.json | 8 +------- src/Tss.Api/appsettings.json | 18 ++++++++++-------- src/Tss.Core/StandaloneTssService.cs | 5 +++-- src/Tss.Core/Tss.Core.csproj | 1 + src/Tss.Core/TssService.cs | 11 ++++++++++- 8 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/Tss.Api/Controllers/TssController.cs b/src/Tss.Api/Controllers/TssController.cs index 1b6feab..4e6f918 100644 --- a/src/Tss.Api/Controllers/TssController.cs +++ b/src/Tss.Api/Controllers/TssController.cs @@ -1,6 +1,4 @@ -using System; -using System.IO; -using System.Threading.Tasks; +using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Tss.Core; diff --git a/src/Tss.Api/Program.cs b/src/Tss.Api/Program.cs index 83f31fb..e997d89 100644 --- a/src/Tss.Api/Program.cs +++ b/src/Tss.Api/Program.cs @@ -7,6 +7,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; +using Serilog; using Tss.Core; namespace Tss.Api @@ -31,6 +32,19 @@ public static IHostBuilder CreateHostBuilder(string[] args) => } }) + .ConfigureLogging((context, builder) => + { + Log.Logger = new LoggerConfiguration() + .Enrich.FromLogContext() + .WriteTo.Console( + outputTemplate: + "{Level:u3}: [{Timestamp:HH:mm:ss}] {SourceContext} {Scope:lj} {NewLine} {Message:lj}{NewLine}{Exception}{NewLine}") + .ReadFrom.Configuration(context.Configuration) + .CreateLogger(); + + builder.ClearProviders() + .AddSerilog(); + }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); diff --git a/src/Tss.Api/Tss.Api.csproj b/src/Tss.Api/Tss.Api.csproj index cd36073..d7d8234 100644 --- a/src/Tss.Api/Tss.Api.csproj +++ b/src/Tss.Api/Tss.Api.csproj @@ -18,6 +18,10 @@ + + + + diff --git a/src/Tss.Api/appsettings.Development.json b/src/Tss.Api/appsettings.Development.json index 1e4c961..0db3279 100644 --- a/src/Tss.Api/appsettings.Development.json +++ b/src/Tss.Api/appsettings.Development.json @@ -1,9 +1,3 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } - } + } diff --git a/src/Tss.Api/appsettings.json b/src/Tss.Api/appsettings.json index d9d9a9b..14ed27c 100644 --- a/src/Tss.Api/appsettings.json +++ b/src/Tss.Api/appsettings.json @@ -1,10 +1,12 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } - }, - "AllowedHosts": "*" + "Serilog": { + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } + }, + "AllowedHosts": "*" } diff --git a/src/Tss.Core/StandaloneTssService.cs b/src/Tss.Core/StandaloneTssService.cs index fe86835..4ffec30 100644 --- a/src/Tss.Core/StandaloneTssService.cs +++ b/src/Tss.Core/StandaloneTssService.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using SpotifyAPI.Web.Auth; @@ -7,8 +8,8 @@ namespace Tss.Core { public class StandaloneTssService : TssService { - public StandaloneTssService(IOptions config, IOptionsMonitor mappings) : - base(config, mappings) + public StandaloneTssService(IOptions config, IOptionsMonitor mappings, + ILogger logger) : base(config, mappings, logger) { } diff --git a/src/Tss.Core/Tss.Core.csproj b/src/Tss.Core/Tss.Core.csproj index 0f60666..4f9abc6 100644 --- a/src/Tss.Core/Tss.Core.csproj +++ b/src/Tss.Core/Tss.Core.csproj @@ -5,6 +5,7 @@ enable + diff --git a/src/Tss.Core/TssService.cs b/src/Tss.Core/TssService.cs index cbdd356..07d208f 100644 --- a/src/Tss.Core/TssService.cs +++ b/src/Tss.Core/TssService.cs @@ -2,6 +2,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Newtonsoft.Json; using SpotifyAPI.Web; @@ -18,10 +19,12 @@ public class TssService protected TssLoginFlow _loginFlow; protected SpotifyClient? _client; private IOptionsMonitor _mappings; + private readonly ILogger _logger; - public TssService(IOptions config, IOptionsMonitor mappings) + public TssService(IOptions config, IOptionsMonitor mappings, ILogger logger) { _mappings = mappings; + _logger = logger; var c = config.Value; _clientId = c.ClientId; _credentialsPath = c.CredentialsPath; @@ -34,18 +37,21 @@ public async Task TryLogin() if (File.Exists(_credentialsPath)) { var token = await LoadToken(); + _logger.LogInformation("Loaded token from '{filename}'", _credentialsPath); await CreateClient(token); return new TryLoginResult(true, null); } _loginFlow = new TssLoginFlow(_clientId, _callbackUrl); var url = await _loginFlow.Start(); + _logger.LogInformation("Started authentication flow"); return new TryLoginResult(false, url); } public async Task CompleteLogin(string code) { var token = await _loginFlow!.Complete(code); + _logger.LogInformation("Completed authentication flow"); await CreateClient(token); } @@ -72,6 +78,7 @@ private async Task SaveToken(PKCETokenResponse token) EnsureDirectoryExist(_credentialsPath); var json = JsonConvert.SerializeObject(token); await File.WriteAllTextAsync(_credentialsPath, json); + _logger.LogInformation("Token saved to '{filename}'", _credentialsPath); } private void EnsureDirectoryExist(string path) @@ -86,11 +93,13 @@ private void EnsureDirectoryExist(string path) public async Task MoveCurrentToGood() { await MoveCurrentTo(m => m.Good, false); + _logger.LogInformation("Moved current to good"); } public async Task MoveCurrentToNotGood() { await MoveCurrentTo(m => m.NotGood); + _logger.LogInformation("Moved current to not good"); } public async Task MoveCurrentTo(Func getPlaylistId, bool skip = true) From 6eee7bdcb08bf5396e50a93a215f8e711407964c Mon Sep 17 00:00:00 2001 From: garyng Date: Sun, 15 Nov 2020 04:54:14 +0800 Subject: [PATCH 14/15] feat(cli): use serilog --- src/Tss.Cli/Program.cs | 17 ++++++++++++++++- src/Tss.Cli/Tss.Cli.csproj | 4 ++++ src/Tss.Cli/appsettings.json | 14 ++++++++------ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Tss.Cli/Program.cs b/src/Tss.Cli/Program.cs index 892c5ff..7e18bba 100644 --- a/src/Tss.Cli/Program.cs +++ b/src/Tss.Cli/Program.cs @@ -4,7 +4,9 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using Tss.Core; +using Serilog; namespace Tss.Cli { @@ -23,6 +25,19 @@ static async Task Main(string[] args) config.AddYamlFile(tssConfig.MappingsPath, optional: false, reloadOnChange: true); } }) + .ConfigureLogging((context, builder) => + { + Log.Logger = new LoggerConfiguration() + .Enrich.FromLogContext() + .WriteTo.Console( + outputTemplate: + "{Level:u3}: [{Timestamp:HH:mm:ss}] {SourceContext} {Scope:lj} {NewLine} {Message:lj}{NewLine}{Exception}{NewLine}") + .ReadFrom.Configuration(context.Configuration) + .CreateLogger(); + + builder.ClearProviders() + .AddSerilog(); + }) .ConfigureServices((context, services) => { var tssConfig = context.Configuration.GetSection(nameof(TssConfig)); @@ -54,7 +69,7 @@ public Startup(StandaloneTssService service) public async Task Run() { await _service.Login(); - // await _service.MoveCurrentToGood(); + await _service.MoveCurrentToGood(); // await _service.MoveCurrentToNotGood(); } } diff --git a/src/Tss.Cli/Tss.Cli.csproj b/src/Tss.Cli/Tss.Cli.csproj index 8ab3e34..54c242b 100644 --- a/src/Tss.Cli/Tss.Cli.csproj +++ b/src/Tss.Cli/Tss.Cli.csproj @@ -22,6 +22,10 @@ + + + + diff --git a/src/Tss.Cli/appsettings.json b/src/Tss.Cli/appsettings.json index 8983e0f..54c7915 100644 --- a/src/Tss.Cli/appsettings.json +++ b/src/Tss.Cli/appsettings.json @@ -1,9 +1,11 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" + "Serilog": { + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } } - } } From 2cf9ef730d9d02891b40c048e27291aa9d50aca8 Mon Sep 17 00:00:00 2001 From: garyng Date: Sun, 15 Nov 2020 04:54:33 +0800 Subject: [PATCH 15/15] fix(cli): move common settings to appsettings --- src/Tss.Cli/appsettings.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Tss.Cli/appsettings.json b/src/Tss.Cli/appsettings.json index 54c7915..5684f3d 100644 --- a/src/Tss.Cli/appsettings.json +++ b/src/Tss.Cli/appsettings.json @@ -7,5 +7,11 @@ "Microsoft.Hosting.Lifetime": "Information" } } + }, + "TssConfig": { + "ClientId": "", + "CredentialsPath": "./credentials.json", + "MappingsPath": "mappings.yml", + "CallbackPort": 8124 } }