File size: 4,304 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#region Using directives
using Microsoft.Management.Infrastructure.Options;
#endregion

namespace Microsoft.Management.Infrastructure.CimCmdlets
{
    /// <summary>
    /// <para>
    /// Prompt user the message coming from provider.
    /// </para>
    /// <para>
    /// At the same time <see cref="CimPromptUser"/> class will prepare the
    /// message for -whatif parameter, while the message represents
    /// what will happen if execute the operation, but not do the operation.
    /// For example, Remove-CimInstance, the whatif message will like,
    /// "CIM Instance: Win32_Process@{Key=1} will be deleted."
    /// </para>
    /// </summary>
    internal sealed class CimPromptUser : CimSyncAction
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CimPromptUser"/> class.
        /// </summary>
        public CimPromptUser(string message,
            CimPromptType prompt)
        {
            this.Message = message;
            this.prompt = prompt;
        }

        /// <summary>
        /// <para>
        /// Prompt user with the given message and prepared whatif message.
        /// </para>
        /// </summary>
        /// <param name="cmdlet">
        /// cmdlet wrapper object, to which write result.
        /// <see cref="CmdletOperationBase"/> for details.
        /// </param>
        public override void Execute(CmdletOperationBase cmdlet)
        {
            ValidationHelper.ValidateNoNullArgument(cmdlet, "cmdlet");

            bool yestoall = false;
            bool notoall = false;
            bool result = false;

            switch (this.prompt)
            {
                case CimPromptType.Critical:
                    // NOTES: prepare the whatif message and caption
                    try
                    {
                        result = cmdlet.ShouldContinue(Message, "caption", ref yestoall, ref notoall);
                        if (yestoall)
                        {
                            this.responseType = CimResponseType.YesToAll;
                        }
                        else if (notoall)
                        {
                            this.responseType = CimResponseType.NoToAll;
                        }
                        else if (result)
                        {
                            this.responseType = CimResponseType.Yes;
                        }
                        else if (!result)
                        {
                            this.responseType = CimResponseType.No;
                        }
                    }
                    catch
                    {
                        this.responseType = CimResponseType.NoToAll;
                        throw;
                    }
                    finally
                    {
                        // unblocking the waiting thread
                        this.OnComplete();
                    }

                    break;
                case CimPromptType.Normal:
                    try
                    {
                        result = cmdlet.ShouldProcess(Message);
                        if (result)
                        {
                            this.responseType = CimResponseType.Yes;
                        }
                        else if (!result)
                        {
                            this.responseType = CimResponseType.No;
                        }
                    }
                    catch
                    {
                        this.responseType = CimResponseType.NoToAll;
                        throw;
                    }
                    finally
                    {
                        // unblocking the waiting thread
                        this.OnComplete();
                    }

                    break;
                default:
                    break;
            }

            this.OnComplete();
        }

        #region members

        /// <summary>
        /// Prompt message.
        /// </summary>
        public string Message { get; }

        /// <summary>
        /// Prompt type -Normal or Critical.
        /// </summary>
        private readonly CimPromptType prompt;

        #endregion
    }
}