Skip to content

Commit 308b47d

Browse files
committed
Merge branch 'mloskot-ml/feat/config-path-via-env-var' into dev
2 parents b580671 + ea01d4e commit 308b47d

File tree

9 files changed

+64
-10
lines changed

9 files changed

+64
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ seqcli config -k connection.serverUrl -v https://your-seq-server
1919
seqcli config -k connection.apiKey -v your-api-key
2020
```
2121

22-
The API key will be stored in your `SeqCli.json` configuration file; on Windows, this is encrypted using DPAPI; on Mac/Linux the key is currently stored in plain text. As an alternative to storing the API key in configuration, it can be passed to each command via the `--apikey=` argument.
22+
The API key will be stored in your `SeqCli.json` configuration file; on Windows, this is encrypted using DPAPI; on Mac/Linux the key is stored in plain text unless an encryptor is defined in `encryption.encryptor`. As an alternative to storing the API key in configuration, it can be passed to each command via the `--apikey=` argument.
2323

2424
`seqcli` is also available as a Docker container under [`datalust/seqcli`](https://store.docker.com/community/images/datalust/seqcli):
2525

build/Build.Windows.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function Restore-Packages
2828

2929
function Execute-Tests($version)
3030
{
31-
& dotnet test ./test/SeqCli.Tests/SeqCli.Tests.csproj -c Release /p:Configuration=Release /p:Platform=x64 /p:VersionPrefix=$version
31+
& dotnet test ./test/SeqCli.Tests/SeqCli.Tests.csproj -c Release --framework "$framework$windowsTfmSuffix" /p:Configuration=Release /p:Platform=x64 /p:VersionPrefix=$version
3232
if($LASTEXITCODE -ne 0) { throw "Build failed" }
3333
}
3434

src/SeqCli/Cli/Commands/ConfigCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace SeqCli.Cli.Commands;
2323

24-
[Command("config", "View and set fields in the `SeqCli.json` file; run with no arguments to list all fields")]
24+
[Command("config", "View and set fields in `SeqCli.json`; run with no arguments to list all fields")]
2525
class ConfigCommand : Command
2626
{
2727
string? _key, _value;

src/SeqCli/Cli/Commands/Profile/RemoveCommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ int RunSync()
4646
}
4747

4848
SeqCliConfig.WriteToFile(config, _storagePath.ConfigFilePath);
49-
5049
return 0;
5150
}
5251
catch (Exception ex)

src/SeqCli/Cli/Features/StoragePathFeature.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,20 @@ namespace SeqCli.Cli.Features;
99

1010
class StoragePathFeature : CommandFeature
1111
{
12+
const string StoragePathVarName = "SEQCLI_STORAGE_PATH";
13+
1214
string? _storageRoot;
15+
readonly Func<string, string?> _getEnvironmentVariable;
16+
17+
public StoragePathFeature()
18+
: this(Environment.GetEnvironmentVariable)
19+
{
20+
}
21+
22+
public StoragePathFeature(Func<string, string?> getEnvironmentVariable)
23+
{
24+
_getEnvironmentVariable = getEnvironmentVariable;
25+
}
1326

1427
public string StorageRootPath
1528
{
@@ -18,7 +31,13 @@ public string StorageRootPath
1831
if (!string.IsNullOrWhiteSpace(_storageRoot))
1932
return _storageRoot;
2033

21-
return TryQueryInstalledStorageRoot() ?? GetDefaultStorageRoot();
34+
if (_getEnvironmentVariable(StoragePathVarName) is {} fromVar && !string.IsNullOrWhiteSpace(fromVar))
35+
return fromVar;
36+
37+
if (TryQueryInstalledStorageRoot() is { } installed)
38+
return installed;
39+
40+
return GetDefaultStorageRoot();
2241
}
2342
}
2443

@@ -31,8 +50,9 @@ public string StorageRootPath
3150
public override void Enable(OptionSet options)
3251
{
3352
options.Add("storage=",
34-
"The folder where `SeqCli.json` and other data will be stored; " +
35-
"`" + GetDefaultStorageRoot() + "` is used by default",
53+
$"The folder where `SeqCli.json` and other data will be stored; falls back to `{StoragePathVarName}` from " +
54+
$"the environment, then the `seqcli forwarder` service's configured storage path (Windows only), then " +
55+
"`" + GetDefaultStorageRoot() + "`",
3656
v => _storageRoot = Path.GetFullPath(v));
3757
}
3858

src/SeqCli/SeqCli.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
</Content>
4747
</ItemGroup>
4848
<ItemGroup>
49-
<PackageReference Include="newtonsoft.json" Version="13.0.3" />
5049
<PackageReference Include="Seq.Api" Version="2025.2.0" />
5150
<PackageReference Include="Serilog" Version="4.3.0" />
5251
<PackageReference Include="Serilog.Expressions" Version="5.0.0" />

test/SeqCli.EndToEnd/SeqCli.EndToEnd.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net9.0;net9.0-windows</TargetFrameworks>
5+
<TargetFrameworks>net9.0</TargetFrameworks>
6+
<TargetFrameworks Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">$(TargetFrameworks);net9.0-windows</TargetFrameworks>
67
</PropertyGroup>
78
<ItemGroup>
89
<PackageReference Include="xunit" Version="2.9.3" />
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.IO;
3+
using SeqCli.Cli.Features;
4+
using Xunit;
5+
6+
namespace SeqCli.Tests.Config;
7+
8+
public class ConfigFileLocationTests
9+
{
10+
[Fact]
11+
public void DefaultConfigFilename()
12+
{
13+
var storagePathFeature = new StoragePathFeature(_ => null);
14+
15+
// GetDefaultStorageRoot() isn't exposed by StoragePathFeature because it would introduce the risk we'd
16+
// use it accidentally elsewhere.
17+
var defaultPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
18+
19+
Assert.Equal(defaultPath, storagePathFeature.StorageRootPath);
20+
}
21+
22+
[Fact]
23+
public void EnvironmentOverridenConfigFilename()
24+
{
25+
var customStoragePath = Path.GetTempPath();
26+
27+
var storagePathFeature = new StoragePathFeature(
28+
name => "SEQCLI_STORAGE_PATH".Equals(name, StringComparison.OrdinalIgnoreCase)
29+
? customStoragePath
30+
: null);
31+
32+
Assert.Equal(customStoragePath, storagePathFeature.StorageRootPath);
33+
}
34+
}

test/SeqCli.Tests/SeqCli.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net9.0;net9.0-windows</TargetFrameworks>
3+
<TargetFrameworks>net9.0</TargetFrameworks>
4+
<TargetFrameworks Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">$(TargetFrameworks);net9.0-windows</TargetFrameworks>
45
</PropertyGroup>
56
<ItemGroup>
67
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />

0 commit comments

Comments
 (0)