This repository was archived by the owner on Apr 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathContainerArgumentCompleter.cs
59 lines (55 loc) · 2.45 KB
/
ContainerArgumentCompleter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Language;
using System.Linq;
using Docker.DotNet.Models;
using Docker.PowerShell.Objects;
using System;
public class ContainerArgumentCompleter : IArgumentCompleter
{
public IEnumerable<CompletionResult> CompleteArgument(string commandName,
string parameterName,
string wordToComplete,
CommandAst commandAst,
IDictionary fakeBoundParameters)
{
var client = DockerFactory.CreateClient(fakeBoundParameters);
IList<ContainerListResponse> result;
if (wordToComplete == "")
{
result = client.Containers.ListContainersAsync(new ContainersListParameters
{
All = true
}).Result;
}
else
{
result = ContainerOperations.GetContainersById(wordToComplete, client).Result;
result = result.Concat(ContainerOperations.GetContainersByName(wordToComplete, client).Result).ToList();
}
return result.SelectMany(container =>
{
// If the user has already typed part of the name, then include IDs that start
// with that portion. Otherwise, just let the user tab through the names.
// Special handling for Get-Container, where Id an Name are separate parameters.
if (commandName == "Get-Container" && parameterName == "Id")
{
return new List<string> {container.ID};
}
else if (wordToComplete == "" || parameterName == "Name")
{
return container.Names;
}
else
{
return container.Names.Concat(new List<string> { container.ID });
}
})
.Select(name => name.StartsWith("/") && !wordToComplete.StartsWith("/") ? name.Substring(1) : name)
.Distinct()
.Where(name => name.StartsWith(wordToComplete, StringComparison.CurrentCultureIgnoreCase))
.OrderBy(name => name)
.Select(name => new CompletionResult(name, name, CompletionResultType.Text, name));
}
}