コード例 #1
0
ファイル: GetOctopusUser.cs プロジェクト: tmpreston/OctoPosh
        protected override void ProcessRecord()
        {
            var baseResourceList = new List <UserResource>();

            var userNameList = UserName?.ToList().ConvertAll(s => s.ToLower());

            if (userNameList == null)
            {
                baseResourceList = _connection.Repository.Users.FindAll();
            }
            else
            {
                //Multiple values but one of them is wildcarded, which is not an accepted scenario (I.e -MachineName WebServer*, Database1)
                if (userNameList.Any(item => WildcardPattern.ContainsWildcardCharacters(item) && userNameList.Count > 1))
                {
                    throw OctoposhExceptions.ParameterCollectionHasRegularAndWildcardItem("UserName");
                }
                //Only 1 wildcarded value (ie -MachineName WebServer*)
                else if (userNameList.Any(item => WildcardPattern.ContainsWildcardCharacters(item) && userNameList.Count == 1))
                {
                    var pattern = new WildcardPattern(userNameList.First());
                    baseResourceList = _connection.Repository.Users.FindMany(u => pattern.IsMatch(u.Username.ToLower()));
                }
                //multiple non-wildcared values (i.e. -MachineName WebServer1,Database1)
                else if (!userNameList.Any(WildcardPattern.ContainsWildcardCharacters))
                {
                    baseResourceList = _connection.Repository.Users.FindMany(u => userNameList.Contains(u.Username.ToLower()));
                }
            }

            if (ResourceOnly)
            {
                if (baseResourceList.Count == 1)
                {
                    WriteObject(baseResourceList.FirstOrDefault(), true);
                }
                else
                {
                    WriteObject(baseResourceList, true);
                }
            }
            else
            {
                var converter  = new OutputConverter();
                var outputList = converter.GetOctopusUser(baseResourceList);

                if (outputList.Count == 1)
                {
                    WriteObject(outputList.FirstOrDefault(), true);
                }
                else
                {
                    WriteObject(outputList, true);
                }
            }
        }