protected PipelineBuilderBase(Source[] source) { Option = new PSRuleOption(); Source = source; _Output = new PSPipelineWriter(Option); HostContext = new HostContext(); }
public static IInvokePipelineBuilder Test(Source[] source, PSRuleOption option) { var pipeline = new TestPipelineBuilder(source); pipeline.Configure(option); return(pipeline); }
internal Runspace GetRunspace() { if (_Runspace == null) { var state = HostState.CreateSessionState(); state.LanguageMode = _LanguageMode == LanguageMode.FullLanguage ? PSLanguageMode.FullLanguage : PSLanguageMode.ConstrainedLanguage; _Runspace = RunspaceFactory.CreateRunspace(state); if (Runspace.DefaultRunspace == null) { Runspace.DefaultRunspace = _Runspace; } _Runspace.Open(); _Runspace.SessionStateProxy.PSVariable.Set(new PSRuleVariable()); _Runspace.SessionStateProxy.PSVariable.Set(new RuleVariable()); _Runspace.SessionStateProxy.PSVariable.Set(new LocalizedDataVariable()); _Runspace.SessionStateProxy.PSVariable.Set(new AssertVariable()); _Runspace.SessionStateProxy.PSVariable.Set(new TargetObjectVariable()); _Runspace.SessionStateProxy.PSVariable.Set(new ConfigurationVariable()); _Runspace.SessionStateProxy.PSVariable.Set(ErrorPreference, ActionPreference.Continue); _Runspace.SessionStateProxy.PSVariable.Set(WarningPreference, ActionPreference.Continue); _Runspace.SessionStateProxy.PSVariable.Set(VerbosePreference, ActionPreference.Continue); _Runspace.SessionStateProxy.PSVariable.Set(DebugPreference, ActionPreference.Continue); _Runspace.SessionStateProxy.Path.SetLocation(PSRuleOption.GetWorkingPath()); } return(_Runspace); }
internal TemplateLinkPipeline(PipelineContext context, string basePath, bool skipUnlinked) : base(context) { _BasePath = PSRuleOption.GetRootedBasePath(basePath); _SkipUnlinked = skipUnlinked; _PathBuilder = new PathBuilder(context.Writer, basePath, DEFAULT_TEMPLATESEARCH_PATTERN); }
private bool TryTemplateFile(JObject metadata, string parameterFile, out string templateFile) { if (!TryStringProperty(metadata, PROPERTYNAME_TEMPLATE, out templateFile)) { if (_SkipUnlinked) { Context.Writer.VerboseTemplateLinkNotFound(parameterFile); } return(false); } templateFile = TrimSlash(templateFile); var pathBase = IsRelative(templateFile) ? Path.GetDirectoryName(parameterFile) : PSRuleOption.GetWorkingPath(); templateFile = Path.GetFullPath(Path.Combine(pathBase, templateFile)); // Template file must be within working path if (!templateFile.StartsWith(PSRuleOption.GetRootedBasePath(""), StringComparison.Ordinal)) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, PSRuleResources.PathTraversal, templateFile)); } if (!File.Exists(templateFile)) { Context.Writer.VerboseTemplateFileNotFound(templateFile); throw new FileNotFoundException( string.Format(CultureInfo.CurrentCulture, PSRuleResources.TemplateFileReferenceNotFound, parameterFile), new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, PSRuleResources.TemplateFileNotFound, templateFile)) ); } return(true); }
public static IGetPipelineBuilder Get(Source[] source, PSRuleOption option) { var pipeline = new GetRulePipelineBuilder(source); pipeline.Configure(option); return(pipeline); }
public static PipelineContext New(PSRuleOption option, HostContext hostContext, TargetBinder binder, OptionContext baseline, IDictionary <string, ResourceRef> unresolved) { var context = new PipelineContext(option, hostContext, binder, baseline, unresolved); CurrentThread = context; return(context); }
public void BuildInvokePipeline() { var option = new PSRuleOption(); var builder = PipelineBuilder.Invoke(GetSource(), option); var pipeline = builder.Build(); Assert.NotNull(pipeline); }
private static ITemplatePipelineBuilder GetTemplatePipeline() { var option = PSRuleOption.FromFileOrDefault(GetSourcePath("ps-rule.yaml")); var builder = PipelineBuilder.Template(option); builder.PassThru(true); return(builder); }
protected PipelineBuilderBase(Source[] source) { Logger = new PipelineLogger(); Option = new PSRuleOption(); Source = source; _Output = (r, b) => { }; HostContext = new HostContext(); }
internal HelpWriter(PipelineWriter inner, PSRuleOption option, LanguageMode languageMode, bool inSession, bool online, bool full) : base(inner, option) { _LanguageMode = languageMode; _InSession = inSession; _ShouldOutput = !online; _TypeName = full ? OUTPUT_TYPENAME_FULL : null; }
internal FileOutputWriter(PipelineWriter inner, PSRuleOption option, Encoding encoding, string path, string defaultFile, ShouldProcess shouldProcess) : base(inner, option) { _Encoding = encoding; _Path = path; _DefaultFile = defaultFile; _ShouldProcess = shouldProcess; }
internal TemplatePipelineBuilder(PSRuleOption option) : base() { _DeploymentName = string.Concat(DEPLOYMENTNAME_PREFIX, Guid.NewGuid().ToString().Substring(0, 8)); _ResourceGroup = Data.Template.ResourceGroup.Default; _Subscription = Data.Template.Subscription.Default; Configure(option); }
private void PrepareInvokePipeline() { var option = new PSRuleOption(); option.Rule.Include = new string[] { "Benchmark" }; var builder = PipelineBuilder.Invoke(GetSource(), option); _InvokePipeline = builder.Build(); }
private void ProcessParameterFile(string parameterFile) { try { // Check that the JSON file is an ARM template parameter file var rootedParameterFile = PSRuleOption.GetRootedPath(parameterFile); var parameterObject = ReadFile <JObject>(rootedParameterFile); if (parameterObject == null || !IsParameterFile(parameterObject)) { return; } // Check if metadata property exists if (!((TryMetadata(parameterObject, rootedParameterFile, out JObject metadata, out string templateFile)) || TryTemplateByName(parameterFile, out templateFile))) { if (metadata == null && !_SkipUnlinked) { throw new InvalidTemplateLinkException(string.Format(CultureInfo.CurrentCulture, PSRuleResources.MetadataNotFound, parameterFile)); } if (templateFile == null && !_SkipUnlinked) { throw new InvalidTemplateLinkException(string.Format(CultureInfo.CurrentCulture, PSRuleResources.TemplateLinkNotFound, parameterFile)); } return; } var templateLink = new TemplateLink(templateFile, rootedParameterFile); // Populate remaining properties if (TryStringProperty(metadata, PROPERTYNAME_NAME, out string name)) { templateLink.Name = name; } if (TryStringProperty(metadata, PROPERTYNAME_DESCRIPTION, out string description)) { templateLink.Description = description; } Context.Writer.WriteObject(templateLink, false); } catch (InvalidOperationException ex) { Context.Writer.WriteError(ex, nameof(InvalidOperationException), ErrorCategory.InvalidOperation, parameterFile); } catch (FileNotFoundException ex) { Context.Writer.WriteError(ex, nameof(FileNotFoundException), ErrorCategory.ObjectNotFound, parameterFile); } catch (PipelineException ex) { Context.Writer.WriteError(ex, nameof(PipelineException), ErrorCategory.WriteError, parameterFile); } }
internal PSObject[] ProcessTemplate(string templateFile, string parameterFile) { var rootedTemplateFile = PSRuleOption.GetRootedPath(templateFile); if (!File.Exists(rootedTemplateFile)) { throw new FileNotFoundException(string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.TemplateFileNotFound, rootedTemplateFile), rootedTemplateFile); } var templateObject = ReadFile(rootedTemplateFile); var visitor = new RuleDataExportVisitor(); // Load context var context = new TemplateVisitor.TemplateContext(Context, _Subscription, _ResourceGroup); if (!string.IsNullOrEmpty(parameterFile)) { var rootedParameterFile = PSRuleOption.GetRootedPath(parameterFile); if (!File.Exists(rootedParameterFile)) { throw new FileNotFoundException(string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.ParameterFileNotFound, rootedParameterFile), rootedParameterFile); } try { var parametersObject = ReadFile(rootedParameterFile); context.Load(parametersObject); } catch (Exception inner) { throw new TemplateReadException(string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.TemplateExpandInvalid, templateFile, parameterFile, inner.Message), inner, templateFile, parameterFile); } } // Process try { visitor.Visit(context, _DeploymentName, templateObject); } catch (Exception inner) { throw new TemplateReadException(string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.TemplateExpandInvalid, templateFile, parameterFile, inner.Message), inner, templateFile, parameterFile); } // Return results var results = new List <PSObject>(); var serializer = new JsonSerializer(); serializer.Converters.Add(new PSObjectJsonConverter()); foreach (var resource in context.Resources) { results.Add(resource.ToObject <PSObject>(serializer)); } return(results.ToArray()); }
public virtual IPipelineBuilder Configure(PSRuleOption option) { if (option == null) { return(this); } Option.Output = new OutputOption(option.Output); return(this); }
private void PrepareGetHelpPipeline() { var option = new PSRuleOption(); option.Rule.Include = new string[] { "BenchmarkHelp" }; option.Output.Culture = new string[] { "en-ZZ" }; var builder = PipelineBuilder.GetHelp(GetSource(), option); _GetHelpPipeline = builder.Build(); }
private void PrepareInvokeSummaryPipeline() { var option = new PSRuleOption(); option.Rule.Include = new string[] { "Benchmark" }; option.Output.As = ResultFormat.Summary; var builder = PipelineBuilder.Invoke(GetSource(), option); _InvokeSummaryPipeline = builder.Build(); }
public void GetOptions() { var actual1 = PSRuleOption.FromFileOrDefault(null); var actual2 = PSRuleOption.FromFileOrDefault(GetSourcePath("ps-rule-options.yaml")); Assert.NotNull(actual1); Assert.Equal("PSRule Test Subscription", actual1.Configuration.Subscription.DisplayName); Assert.NotNull(actual2); Assert.Equal("Unit Test Subscription", actual2.Configuration.Subscription.DisplayName); }
public void Configuration() { var option = new PSRuleOption(); option.Configuration.Add("key1", "value1"); BuildPipeline(option); dynamic configuration = GetConfigurationHelper(); Assert.Equal("value1", configuration.key1); }
public override IPipelineBuilder Configure(PSRuleOption option) { if (option == null) { return(this); } Option.Output.As = ResultFormat.Detail; Option.Output.Culture = option.Output.Culture ?? new string[] { Thread.CurrentThread.CurrentCulture.ToString() }; Option.Output.Format = OutputFormat.None; return(this); }
public override IPipelineBuilder Configure(PSRuleOption option) { if (option == null) { return(this); } Option.Output.As = ResultFormat.Detail; Option.Output.Culture = GetCulture(option.Output.Culture); Option.Output.Format = OutputFormat.None; return(this); }
internal void Configure(PSRuleOption option) { if (option.Logging.LimitVerbose != null && option.Logging.LimitVerbose.Length > 0) { _VerboseFilter = new HashSet <string>(option.Logging.LimitVerbose); } if (option.Logging.LimitDebug != null && option.Logging.LimitDebug.Length > 0) { _DebugFilter = new HashSet <string>(option.Logging.LimitDebug); } }
public ConfigScope(ScopeType type, string moduleName, PSRuleOption option) : base(type, moduleName) { Field = option.Binding?.Field; IgnoreCase = option.Binding?.IgnoreCase; NameSeparator = option?.Binding?.NameSeparator; TargetName = option.Binding?.TargetName; TargetType = option.Binding?.TargetType; UseQualifiedName = option.Binding?.UseQualifiedName; Culture = option.Output?.Culture; Configuration = option.Configuration != null ? new Dictionary <string, object>(option.Configuration, StringComparer.OrdinalIgnoreCase) : new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); }
public void Credential(PSCredential credential) { if (_UseGitHubToken && credential == null) { if (PSRuleOption.TryGetEnvironmentVariableSecureString(GITHUB_TOKEN, out SecureString token)) { _Credential = new PSCredential("token", token); } return; } _Credential = credential; }
private PipelineContext(PSRuleOption option, HostContext hostContext, TargetBinder binder, OptionContext baseline, IDictionary <string, ResourceRef> unresolved) { Option = option; HostContext = hostContext; _LanguageMode = option.Execution.LanguageMode ?? ExecutionOption.Default.LanguageMode.Value; _NameTokenCache = new Dictionary <string, NameToken>(); LocalizedDataCache = new Dictionary <string, Hashtable>(); ExpressionCache = new Dictionary <string, object>(); ContentCache = new Dictionary <string, PSObject[]>(); Binder = binder; Baseline = baseline; _Unresolved = unresolved; }
/// <summary> /// Write output to file. /// </summary> /// <param name="path">The file path to write.</param> /// <param name="encoding">The file encoding to use.</param> /// <param name="o">The text to write.</param> private static void WriteToFile(string path, ShouldProcess shouldProcess, Encoding encoding, object o) { var rootedPath = PSRuleOption.GetRootedPath(path: path); var parentPath = Directory.GetParent(rootedPath); if (!parentPath.Exists && shouldProcess(target: parentPath.FullName, action: PSRuleResources.ShouldCreatePath)) { Directory.CreateDirectory(path: parentPath.FullName); } if (shouldProcess(target: rootedPath, action: PSRuleResources.ShouldWriteFile)) { File.WriteAllText(path: rootedPath, contents: o.ToString(), encoding: encoding); } }
public void Repository(string[] repository) { if (repository == null) { if (PSRuleOption.TryGetEnvironmentVariableString(GITHUB_REPOSITORY, out string repo)) { _Repository = new string[] { repo } } ; return; } _Repository = repository; }
public virtual IPipelineBuilder Configure(PSRuleOption option) { if (option == null) { return(this); } Option.Binding = new BindingOption(option.Binding); Option.Execution = new ExecutionOption(option.Execution); Option.Input = new InputOption(option.Input); Option.Input.Format = Option.Input.Format ?? InputOption.Default.Format; Option.Output = new OutputOption(option.Output); return(this); }