Exemple #1
0
        public T GetService <T>(CmdletBase CmdletBase, object parameters = null) where T : IService
        {
            var serviceType = typeof(T);

            if (!_factories.ContainsKey(serviceType))
            {
                throw new ArgumentException($"Invalid service {serviceType.FullName}. Are you missing an [Exports] attribute?");
            }

            return((T)_factories[serviceType](this, CmdletBase, parameters));
        }
Exemple #2
0
        public IDataService <TObj> GetDataService <TObj>(CmdletBase CmdletBase, object overriddenParameters) where TObj : class
        {
            var serviceType = typeof(TObj);

            if (!_factories.ContainsKey(serviceType))
            {
                throw new ArgumentException($"Invalid service {serviceType.FullName}. Are you missing an [Exports] attribute?");
            }

            var dataService = ((IDataService <TObj>)_factories[serviceType](this, CmdletBase, overriddenParameters));

            return(dataService);
        }
Exemple #3
0
        // internal static void Log(this CmdletBase cmdlet, string message, string commandName = null, bool force = false)
        // {
        //     if (!IsVerbose(cmdlet, force)) return;

        //     if (string.IsNullOrEmpty(commandName))
        //     {
        //         commandName = cmdlet.CommandName;
        //     }

        //     cmdlet.WriteVerbose($"[{DateTime.Now:HH:mm:ss.ffff}] [{commandName}] {message}");
        // }

        // internal static void LogParameters(this CmdletBase cmdlet)
        // {
        //     if (!IsVerbose(cmdlet)) return;

        //     var parms = new ParameterDictionary(cmdlet);

        //     if (parms.ContainsKey("Password") && parms["Password"] != null)
        //     {
        //         parms["Password"] = "******";
        //     }

        //     Log(cmdlet, "ARGS: " + JObject.FromObject(parms)
        //                     .ToString(Formatting.None)
        //                     .Replace("\":", "\" = ")
        //                     .Replace(",\"", "; \"")
        //                     .Trim('{', '}')
        //                 );
        // }

        //private static string GetLogSiteName(CmdletBase cmdlet)
        //{
        //var cs = new Stack();
        //var callStack = cmdlet.InvokeCommand.InvokeScript("Get-PSCallStack").Select(o => o.BaseObject as CallStackFrame).Skip(1);

        //foreach (var frame in callStack)
        //{
        //    var cmd = GetCommandName(frame);

        //    cs.Push(cmd.Trim('_'));

        //    //if (cmd.Contains("-"))
        //    //{
        //    //    break;
        //    //}
        //}

        //    return cmdlet.MyInvocation.InvocationName;
        //}

        //private static string GetCommandName(CallStackFrame frame)
        //{
        //    if (frame.InvocationInfo == null)
        //    {
        //        return frame.FunctionName;
        //    }

        //    var commandInfo = frame.InvocationInfo.MyCommand;

        //    if (commandInfo == null)
        //    {
        //        return !string.IsNullOrEmpty(frame.InvocationInfo.InvocationName) ?
        //            frame.InvocationInfo.InvocationName :
        //            frame.Position.Text;
        //    }

        //    return !string.IsNullOrEmpty(commandInfo.Name) ? commandInfo.Name : frame.FunctionName;
        //}

        private static bool IsVerbose(CmdletBase cmdlet, bool force = false)
        {
            if (force)
            {
                return(true);
            }

            var containsVerbose = cmdlet.MyInvocation.BoundParameters.ContainsKey("Verbose");

            if (containsVerbose)
            {
                return(((SwitchParameter)cmdlet.MyInvocation.BoundParameters["Verbose"]).ToBool());
            }

            return((ActionPreference)cmdlet.GetVariableValue("VerbosePreference") != ActionPreference.SilentlyContinue);
        }
Exemple #4
0
        public Models.Connection GetCollection(CmdletBase cmdlet, ParameterDictionary parameters = null)
        {
            var pd = new ParameterDictionary(parameters)
            {
                ["ConnectionType"] = ClientScope.Collection
            };

            var tpc = GetDataService <Models.Connection>(cmdlet, pd).GetItem();

            if (tpc == null)
            {
                throw new ArgumentException("No TFS connection information available. Either supply a valid -Collection argument or use Connect-TfsTeamProjectCollection prior to invoking this cmdlet.");
            }

            return(tpc);
        }
Exemple #5
0
        public Models.Connection GetServer(CmdletBase cmdlet, ParameterDictionary parameters = null)
        {
            var pd = new ParameterDictionary(parameters)
            {
                ["ConnectionType"] = ClientScope.Server
            };

            var srv = GetDataService <Models.Connection>(cmdlet, pd).GetItem();

            if (srv == null)
            {
                throw new ArgumentException("No TFS connection information available. Either supply a valid -Server argument or use Connect-TfsConfigurationServer prior to invoking this cmdlet.");
            }

            return(srv);
        }
Exemple #6
0
        public (Models.Connection, WebApiTeamProject) GetCollectionAndProject(CmdletBase cmdlet, ParameterDictionary parameters = null)
        {
            var tpc = GetCollection(cmdlet, parameters);

            var pd = new ParameterDictionary(parameters)
            {
                ["Collection"] = tpc
            };

            var tp = GetDataService <WebApiTeamProject>(cmdlet, pd).GetItem();

            if (tp == null)
            {
                throw new ArgumentException("No TFS team project information available. Either supply a valid -Project argument or use Connect-TfsTeamProject prior to invoking this cmdlet.");
            }

            return(tpc, tp);
        }
Exemple #7
0
 internal LoggerImpl(CmdletBase cmdlet)
 {
     Cmdlet = cmdlet;
 }
Exemple #8
0
        public (Models.Connection, WebApiTeamProject, WebApiTeam) GetCollectionProjectAndTeam(CmdletBase cmdlet, ParameterDictionary parameters = null)
        {
            var parms = new ParameterDictionary(cmdlet, parameters);

            if (parms.Get <object>("Team") is WebApiTeam t)
            {
                parms["Project"] = t.ProjectId;
            }

            var(tpc, tp) = GetCollectionAndProject(cmdlet, parms);

            parms["Collection"] = tpc;
            parms["Project"]    = tp;

            var team = GetDataService <Models.Team>(cmdlet, parms).GetItem();

            if (team == null)
            {
                throw new ArgumentException("No TFS team information available. Either supply a valid -Team argument or use Connect-TfsTeam prior to invoking this cmdlet.");
            }

            return(tpc, tp, team);
        }