Skip to content

Commit e759a85

Browse files
committed
init
0 parents  commit e759a85

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+7980
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
bin
2+
obj
3+
publish
4+
pack
5+
.vs
6+
.idea
7+
nupkg
8+
.config

ApplicationBaseCommand.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using CliFx.Attributes;
2+
3+
public abstract class ApplicationBaseCommand : BaseCommand
4+
{
5+
[CommandOption("app-id", Description = "set application id", EnvironmentVariable = "APP_ID")]
6+
public int AppId { get; set; }
7+
}

BaseCommand.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using CliFx.Attributes;
2+
using IO.Swagger.Client;
3+
4+
public abstract class BaseCommand
5+
{
6+
[CommandOption("token", Description = "set auth token", EnvironmentVariable = "TOKEN")]
7+
public string Token { get; set; }
8+
9+
[CommandOption("api-url", Description = "set the url to api", EnvironmentVariable = "API_URL")]
10+
public string ApiUrl { get; set; } = "https://staging.v2.d-f.pw";
11+
12+
protected T GetApi<T>()
13+
{
14+
Configuration.DefaultApiClient.RestClient.BaseUrl = ApiUrl;
15+
Configuration.ApiKey["Authorization"] = Token;
16+
Configuration.ApiKeyPrefix["Authorization"] = "Bearer";
17+
18+
return (T)typeof(T).GetConstructor(new [] { typeof(ApiClient) }).Invoke(new object[] { null });
19+
}
20+
}

Deployf.Cli.csproj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
8+
<PackAsTool>true</PackAsTool>
9+
<ToolCommandName>df</ToolCommandName>
10+
<PackageOutputPath>./nupkg</PackageOutputPath>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="CliFx" Version="2.2.2" />
15+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
16+
<PackageReference Include="RestSharp.Net2" version="1.1.11" />
17+
</ItemGroup>
18+
19+
</Project>

Deployf.Cli.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32120.378
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Deployf.Cli", "Deployf.Cli.csproj", "{8B9F32D5-FC58-4204-A273-1BE376D1FFE0}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{8B9F32D5-FC58-4204-A273-1BE376D1FFE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{8B9F32D5-FC58-4204-A273-1BE376D1FFE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{8B9F32D5-FC58-4204-A273-1BE376D1FFE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{8B9F32D5-FC58-4204-A273-1BE376D1FFE0}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {FF2898BF-AFB9-40FB-9ED5-DA743F1A3E1F}
24+
EndGlobalSection
25+
EndGlobal

InitCommand.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using CliFx;
2+
using CliFx.Attributes;
3+
using CliFx.Infrastructure;
4+
5+
[Command("init", Description = "store token into global user config ~/.df/conf.ini")]
6+
public class InitCommand : ICommand
7+
{
8+
[CommandParameter(0, Description = "set the auth token")]
9+
public string Token { get; init; }
10+
11+
[CommandOption("api-url", Description = "set the url to api")]
12+
public string ApiUrl { get; set; }
13+
14+
public async ValueTask ExecuteAsync(IConsole console)
15+
{
16+
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
17+
var dfPath = Path.Combine(home, ".df");
18+
if (!Directory.Exists(dfPath))
19+
{
20+
Directory.CreateDirectory(dfPath);
21+
}
22+
23+
var confPath = Path.Combine(dfPath, "conf.ini");
24+
25+
var config = $"TOKEN={Token}";
26+
if(!string.IsNullOrEmpty(ApiUrl))
27+
{
28+
config += $"\nAPI_URL={ApiUrl}";
29+
}
30+
31+
File.WriteAllText(confPath, config);
32+
33+
console.Output.WriteLine($"config has been saved to {confPath}");
34+
}
35+
}

Program.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using CliFx;
2+
using System.Diagnostics;
3+
4+
await InitConfig();
5+
6+
await new CliApplicationBuilder()
7+
.AddCommandsFromThisAssembly()
8+
.SetExecutableName("df")
9+
.Build()
10+
.RunAsync();
11+
12+
static async Task InitConfig()
13+
{
14+
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
15+
16+
try
17+
{
18+
var userConfPath = Path.Combine(home, ".df", "conf.ini");
19+
if (File.Exists(userConfPath))
20+
{
21+
var userConf = await File.ReadAllTextAsync(userConfPath);
22+
ApplyEnv(userConf);
23+
}
24+
25+
if (File.Exists(".df.ini"))
26+
{
27+
var userConf = await File.ReadAllTextAsync(".df.ini");
28+
ApplyEnv(userConf);
29+
}
30+
}
31+
catch (Exception ex)
32+
{
33+
Debug.WriteLine(ex.ToString());
34+
}
35+
36+
void ApplyEnv(string userConf)
37+
{
38+
var lines = userConf.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
39+
foreach (var line in lines)
40+
{
41+
var items = line.Split('=', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
42+
if (items.Length == 2)
43+
{
44+
Environment.SetEnvironmentVariable(items[0], items[1]);
45+
}
46+
}
47+
}
48+
}

Properties/launchSettings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"Deployf.Cli": {
4+
"commandName": "Project",
5+
"commandLineArgs": "upload /app/test.txt --app-id 7 --file ../../../swagger-codegen.ps1"
6+
}
7+
}
8+
}

UploadFileCommand.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using CliFx;
2+
using CliFx.Attributes;
3+
using CliFx.Infrastructure;
4+
using IO.Swagger.Api;
5+
6+
[Command("upload", Description = "uplaoad file to the container")]
7+
public class UploadFileCommand : ApplicationBaseCommand, ICommand
8+
{
9+
10+
[CommandParameter(0)]
11+
public string ContainerPath { get; set; }
12+
13+
[CommandOption("file")]
14+
public string SourceFile { get; set; }
15+
16+
public async ValueTask ExecuteAsync(IConsole console)
17+
{
18+
var api = GetApi<ApplicationApi>();
19+
20+
using Stream file = console.IsInputRedirected ? console.Input.BaseStream : File.OpenRead(SourceFile);
21+
22+
var response = api.Exec(AppId, new List<string> { "tee", ContainerPath }, file);
23+
24+
console.Output.BaseStream.Write(response);
25+
}
26+
}

0 commit comments

Comments
 (0)