@@ -14,83 +14,108 @@ internal class CommitCliCommand(
1414 IGetDigestsByIdQuery getDigestsByIdQuery ,
1515 IGetContainersQuery getContainersQuery ,
1616 IStopAndRemoveContainerCommand stopAndRemoveContainerCommand ,
17- ListCliCommand listCliCommand )
18- : AsyncCommand < CommitSettings >
17+ ListCliCommand listCliCommand
18+ ) : AsyncCommand < CommitSettings >
1919{
2020 public override async Task < int > ExecuteAsync ( CommandContext context , CommitSettings settings )
2121 {
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" ) ;
2425
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 =>
3929 {
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+ }
85108 }
86- } ) ;
109+ ) ;
87110
88111 await listCliCommand . ExecuteAsync ( ) ;
89112 return 0 ;
90113 }
91114
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+ )
94119 {
95120 var image = await getImageQuery . QueryAsync ( container . ImageIdentifier , container . ImageTag ) ;
96121 string imageName ;
@@ -101,7 +126,8 @@ await Task.WhenAll(containerWithSameTag.Select(async container1 =>
101126 var digest = digests ? . SingleOrDefault ( ) ;
102127 if ( digest == null || ! DigestHelper . TryGetImageNameAndId ( digest , out var nameNameAndId ) )
103128 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+ ) ;
105131 imageName = nameNameAndId . imageName ;
106132 }
107133 else
@@ -128,4 +154,4 @@ await Task.WhenAll(containerWithSameTag.Select(async container1 =>
128154 var identifier = containerNamePrompt . GetIdentifierOfContainerFromUser ( containers , "commit" ) ;
129155 return containers . SingleOrDefault ( c => c . ContainerName == identifier ) ;
130156 }
131- }
157+ }
0 commit comments