Skip to content

Commit 02a7280

Browse files
committed
small refactor
1 parent 3a0126b commit 02a7280

File tree

4 files changed

+13
-21
lines changed

4 files changed

+13
-21
lines changed

BaseCommand.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using CliFx.Attributes;
2-
using IO.Swagger.Client;
2+
using System.Net.Http.Headers;
33

44
public abstract class BaseCommand
55
{
@@ -9,12 +9,12 @@ public abstract class BaseCommand
99
[CommandOption("api-url", Description = "set the url to api", EnvironmentVariable = "API_URL")]
1010
public string ApiUrl { get; set; } = "https://staging.v2.d-f.pw";
1111

12-
protected T GetApi<T>()
12+
public HttpRequestMessage CreateRequest(HttpMethod method, string query)
1313
{
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 });
14+
var request = new HttpRequestMessage();
15+
request.Method = method;
16+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
17+
request.RequestUri = new Uri(new Uri(ApiUrl), query);
18+
return request;
1919
}
2020
}

ExecCommand.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using CliFx;
22
using CliFx.Attributes;
33
using CliFx.Infrastructure;
4-
using System.Net;
5-
using System.Net.Http.Headers;
64

75
[Command("exec", Description = "exec command in the app container")]
86
public class ExecCommand : ApplicationBaseCommand, ICommand
@@ -12,13 +10,11 @@ public class ExecCommand : ApplicationBaseCommand, ICommand
1210

1311
public async ValueTask ExecuteAsync(IConsole console)
1412
{
15-
var request = new HttpRequestMessage();
16-
request.Method = HttpMethod.Put;
17-
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
18-
request.RequestUri = new Uri(new Uri(ApiUrl), $"api/application/{AppId}/exec");
13+
var request = CreateRequest(HttpMethod.Put, $"api/application/{AppId}/exec");
1914
var content = new MultipartFormDataContent();
15+
request.Content = content;
2016

21-
foreach(var command in ParseCmd(Command))
17+
foreach (var command in ParseCmd(Command))
2218
{
2319
content.Add(new StringContent(command), "command");
2420
}
@@ -28,8 +24,6 @@ public async ValueTask ExecuteAsync(IConsole console)
2824
content.Add(new StreamContent(console.Input.BaseStream), "file", "stdin");
2925
}
3026

31-
request.Content = content;
32-
3327
var response = await new HttpClient().SendAsync(request);
3428

3529
response.EnsureSuccessStatusCode();

Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
await new CliApplicationBuilder()
77
.AddCommandsFromThisAssembly()
88
.SetExecutableName("df")
9+
.SetTitle("Deploy-f cli")
910
.Build()
1011
.RunAsync();
1112

UploadFileCommand.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@ public async ValueTask ExecuteAsync(IConsole console)
1616
{
1717
using Stream file = console.IsInputRedirected ? console.Input.BaseStream : File.OpenRead(SourceFile);
1818

19-
var request = new HttpRequestMessage();
20-
request.Method = HttpMethod.Put;
21-
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
22-
request.RequestUri = new Uri(new Uri(ApiUrl), $"api/application/{AppId}/exec");
19+
var request = CreateRequest(HttpMethod.Put, $"api/application/{AppId}/exec");
2320
var content = new MultipartFormDataContent();
21+
request.Content = content;
2422
content.Add(new StringContent("tee"), "command");
2523
content.Add(new StringContent(ContainerPath), "command");
2624
content.Add(new StreamContent(file), "file", "stdin");
27-
request.Content = content;
2825

2926
var response = await new HttpClient().SendAsync(request);
3027

0 commit comments

Comments
 (0)