首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于正则表达式检查include_controls或require_controls

基于正则表达式检查include_controls或require_controls
EN

Stack Overflow用户
提问于 2021-01-28 14:46:09
回答 1查看 41关注 0票数 0

有没有办法包含基于正则表达式的“依赖”配置文件的控件?如果没有,有没有一种方法可以包含所有控件,并在所有控件上覆盖固定值的影响?

代码应该看起来像这样,其中'controlname‘是需要以某种方式确定的变量:

代码语言:javascript
复制
include_controls 'depends-profile' do
  if controlname.match(/some regex/)    
     control *controlname*
     impact 1.0
  end
end

目标是避免必须单独添加所有控件。

稍微挖掘一下,我得到了这样的结论:

代码语言:javascript
复制
   include_controls 'dependent-profile' do
      list_of_controls = @conf['profile'].runner_context.rules.keys
      list_of_controls.map { |path| path.gsub(@conf['profile'].profile_name+'/','') }
      list_of_controls.each do |controlname|
          if controlname.match(/some regex/)
             control controlname do        # include and overwrite impact
                impact 0.1
             end
          end
          if controlname.match(/some other regex/)
             control controlname           # just include
          end
      end
   end

有没有办法以一种整洁和未来的方式来实现这一点?

EN

回答 1

Stack Overflow用户

发布于 2021-03-26 20:03:25

解决方案可能如下所示:

代码语言:javascript
复制
include_controls '<dependent-profile>' do

  # Scan through all the controls we pulled in
  profile_context.all_controls.each do |c|

    # Grab the control name from control c
    handled_control_name = c.instance_variable_get(:@__rule_id)

    # If it matches the regex 
    if handled_control_name =~ /<myRegex>/

      # Overwrite the impact and tags
      control handled_control_name do
        impact 'critical'
        tag 'myTag'
      end

    end
  end
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65932231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档