@@ -14,83 +14,108 @@ internal class CommitCliCommand(
14
14
IGetDigestsByIdQuery getDigestsByIdQuery ,
15
15
IGetContainersQuery getContainersQuery ,
16
16
IStopAndRemoveContainerCommand stopAndRemoveContainerCommand ,
17
- ListCliCommand listCliCommand )
18
- : AsyncCommand < CommitSettings >
17
+ ConditionalListCliCommand conditionalListCliCommand
18
+ ) : AsyncCommand < CommitSettings >
19
19
{
20
20
public override async Task < int > ExecuteAsync ( CommandContext context , CommitSettings settings )
21
21
{
22
- var container = await GetContainerAsync ( settings ) ??
23
- throw new InvalidOperationException ( "No running container found" ) ;
22
+ var container =
23
+ await GetContainerAsync ( settings )
24
+ ?? throw new InvalidOperationException ( "No running container found" ) ;
24
25
25
- await Spinner . StartAsync ( "Committing container" , async ctx =>
26
- {
27
- string newTag ;
28
- string imageName ;
29
- string tagPrefix ;
30
- if ( settings . Overwrite )
31
- {
32
- newTag = container . ImageTag ??
33
- throw new InvalidOperationException (
34
- "When using --overwrite, container must have an image tag" ) ;
35
- imageName = container . ImageIdentifier ;
36
- tagPrefix = container . TagPrefix ;
37
- }
38
- else
26
+ await Spinner . StartAsync (
27
+ "Committing container" ,
28
+ async ctx =>
39
29
{
40
- var tag = settings . Tag ?? $ "{ DateTime . Now : yyyyMMddhhmmss} ";
41
- ( imageName , tagPrefix , newTag ) = await GetNewTagAsync ( container , tag ) ;
42
- }
43
-
44
-
45
- ctx . Status = $ "Looking for existing container named '{ container . ContainerName } '";
46
- var containerWithSameTag = await getContainersQuery
47
- . QueryByContainerIdentifierAndTagAsync ( container . ContainerIdentifier , newTag )
48
- . ToListAsync ( ) ;
49
-
50
- ctx . Status = $ "Creating image from running container '{ container . ContainerName } '";
51
- newTag = await createImageFromContainerCommand . ExecuteAsync ( container , imageName , tagPrefix , newTag ) ;
52
-
53
- ctx . Status = $ "Removing containers named '{ container . ContainerName } '";
54
- await Task . WhenAll ( containerWithSameTag . Select ( async container1 =>
55
- await stopAndRemoveContainerCommand . ExecuteAsync ( container1 . Id ) ) ) ;
56
-
57
- if ( settings . Overwrite )
58
- {
59
- if ( newTag == null )
60
- throw new InvalidOperationException ( "newTag is null" ) ;
61
-
62
- if ( container . ImageTag == null )
63
- throw new InvalidOperationException (
64
- "Switch argument not supported when creating image from untagged container" ) ;
65
-
66
- ctx . Status = "Launching new image" ;
67
- var id = await createContainerCommand . ExecuteAsync ( container , tagPrefix , newTag ) ;
68
- await runContainerCommand . ExecuteAsync ( id ) ;
69
- }
70
- else if ( settings . Switch )
71
- {
72
- if ( newTag == null )
73
- throw new InvalidOperationException ( "newTag is null" ) ;
74
-
75
- if ( container . ImageTag == null )
76
- throw new InvalidOperationException (
77
- "Switch argument not supported when creating image from untagged container" ) ;
78
-
79
- ctx . Status = $ "Stopping running container '{ container . ContainerName } '";
80
- await stopContainerCommand . ExecuteAsync ( container . Id ) ;
81
-
82
- ctx . Status = "Launching new image" ;
83
- var id = await createContainerCommand . ExecuteAsync ( container , tagPrefix , newTag ) ;
84
- await runContainerCommand . ExecuteAsync ( id ) ;
30
+ string newTag ;
31
+ string imageName ;
32
+ string tagPrefix ;
33
+ if ( settings . Overwrite )
34
+ {
35
+ newTag =
36
+ container . ImageTag
37
+ ?? throw new InvalidOperationException (
38
+ "When using --overwrite, container must have an image tag"
39
+ ) ;
40
+ imageName = container . ImageIdentifier ;
41
+ tagPrefix = container . TagPrefix ;
42
+ }
43
+ else
44
+ {
45
+ var tag = settings . Tag ?? $ "{ DateTime . Now : yyyyMMddhhmmss} ";
46
+ ( imageName , tagPrefix , newTag ) = await GetNewTagAsync ( container , tag ) ;
47
+ }
48
+
49
+ ctx . Status = $ "Looking for existing container named '{ container . ContainerName } '";
50
+ var containerWithSameTag = await getContainersQuery
51
+ . QueryByContainerIdentifierAndTagAsync ( container . ContainerIdentifier , newTag )
52
+ . ToListAsync ( ) ;
53
+
54
+ ctx . Status = $ "Creating image from running container '{ container . ContainerName } '";
55
+ newTag = await createImageFromContainerCommand . ExecuteAsync (
56
+ container ,
57
+ imageName ,
58
+ tagPrefix ,
59
+ newTag
60
+ ) ;
61
+
62
+ ctx . Status = $ "Removing containers named '{ container . ContainerName } '";
63
+ await Task . WhenAll (
64
+ containerWithSameTag . Select ( async container1 =>
65
+ await stopAndRemoveContainerCommand . ExecuteAsync ( container1 . Id )
66
+ )
67
+ ) ;
68
+
69
+ if ( settings . Overwrite )
70
+ {
71
+ if ( newTag == null )
72
+ throw new InvalidOperationException ( "newTag is null" ) ;
73
+
74
+ if ( container . ImageTag == null )
75
+ throw new InvalidOperationException (
76
+ "Switch argument not supported when creating image from untagged container"
77
+ ) ;
78
+
79
+ ctx . Status = "Launching new image" ;
80
+ var id = await createContainerCommand . ExecuteAsync (
81
+ container ,
82
+ tagPrefix ,
83
+ newTag
84
+ ) ;
85
+ await runContainerCommand . ExecuteAsync ( id ) ;
86
+ }
87
+ else if ( settings . Switch )
88
+ {
89
+ if ( newTag == null )
90
+ throw new InvalidOperationException ( "newTag is null" ) ;
91
+
92
+ if ( container . ImageTag == null )
93
+ throw new InvalidOperationException (
94
+ "Switch argument not supported when creating image from untagged container"
95
+ ) ;
96
+
97
+ ctx . Status = $ "Stopping running container '{ container . ContainerName } '";
98
+ await stopContainerCommand . ExecuteAsync ( container . Id ) ;
99
+
100
+ ctx . Status = "Launching new image" ;
101
+ var id = await createContainerCommand . ExecuteAsync (
102
+ container ,
103
+ tagPrefix ,
104
+ newTag
105
+ ) ;
106
+ await runContainerCommand . ExecuteAsync ( id ) ;
107
+ }
85
108
}
86
- } ) ;
109
+ ) ;
87
110
88
- await listCliCommand . ExecuteAsync ( ) ;
111
+ await conditionalListCliCommand . ExecuteAsync ( ) ;
89
112
return 0 ;
90
113
}
91
114
92
- private async Task < ( string imageName , string tagPrefix , string newTag ) > GetNewTagAsync ( Container container ,
93
- string tag )
115
+ private async Task < ( string imageName , string tagPrefix , string newTag ) > GetNewTagAsync (
116
+ Container container ,
117
+ string tag
118
+ )
94
119
{
95
120
var image = await getImageQuery . QueryAsync ( container . ImageIdentifier , container . ImageTag ) ;
96
121
string imageName ;
@@ -101,7 +126,8 @@ await Task.WhenAll(containerWithSameTag.Select(async container1 =>
101
126
var digest = digests ? . SingleOrDefault ( ) ;
102
127
if ( digest == null || ! DigestHelper . TryGetImageNameAndId ( digest , out var nameNameAndId ) )
103
128
throw new InvalidOperationException (
104
- $ "Unable to determine image name from running container '{ container . ContainerName } '") ;
129
+ $ "Unable to determine image name from running container '{ container . ContainerName } '"
130
+ ) ;
105
131
imageName = nameNameAndId . imageName ;
106
132
}
107
133
else
@@ -114,7 +140,8 @@ await Task.WhenAll(containerWithSameTag.Select(async container1 =>
114
140
115
141
var tagPrefix = container . TagPrefix ;
116
142
var newTag = baseTag == null ? tag : $ "{ tagPrefix } { baseTag } -{ tag } ";
117
- if ( newTag . Contains ( '.' ) ) throw new ArgumentException ( "only [a-zA-Z0-9][a-zA-Z0-9_-] are allowed" ) ;
143
+ if ( newTag . Contains ( '.' ) )
144
+ throw new ArgumentException ( "only [a-zA-Z0-9][a-zA-Z0-9_-] are allowed" ) ;
118
145
return ( imageName , tagPrefix , newTag ) ;
119
146
}
120
147
@@ -129,4 +156,4 @@ await Task.WhenAll(containerWithSameTag.Select(async container1 =>
129
156
var identifier = containerNamePrompt . GetIdentifierOfContainerFromUser ( containers , "commit" ) ;
130
157
return containers . SingleOrDefault ( c => c . ContainerName == identifier ) ;
131
158
}
132
- }
159
+ }
0 commit comments