I’m Kenji Hanakawa from Rocro and I’d like to introduce you to one of the most useful features of Inspecode – input filters. First, let me explain what input filters are. By default, Rocro processes all the source code files found in a GitHub or BitBucket repository, including those located in subdirectories.
However, in some cases, you might want to avoid this behavior. For example, if your application uses a third-party library, its source code doesn’t need to be processed. Here is exactly where input filters come to help. With input filters, developers can easily specify which files and directories should be processed and which files and directories should be ignored.
How to Set-up the Input Filters
Rocro provides two directives that you can use in order to set-up input filters in the rocro.yml
configuration file:
input
: a file or directory included in aninput
directive is going to be processed.ignore
: conversely, a file or directory included in anignore
directive will be excluded from processing.
The syntax follows the .dockerignore
format which extends UNIX glob patterns. Let’s see how it actually works:
inspecode: gofmt: input: mycode # Process mycode file/directory located in the root directory. ignore: mycode/sample* #Do not process files and directories starting with sample and located in the mycode directory.
Note that, if input
directive is missing, Rocro will process all files and directories, except those that match the ignore
directive. It is important to mention that multiple patterns can be mixed:
inspecode: gofmt: ignore: - sample* # Do not process files and directories starting with sample and located in the root directory. - example* # Do not process files and directories starting with example and located in the root directory.
How to Use Wildcards
To provide maximum flexibility, Inspecode was engineered to offer support for wildcards. For example, you could use the *
wildcard to refer to a hierarchical directory tree:
inspecode:
gofmt:
ignore: */*/sample* # Do not process files and directories starting with sample and located in all directories in the second level.
Another useful pattern is **
. It matches any directory structure:
inspecode:
gofmt:
ignore: **/sample* # Do not process files and directories starting with sample, no matter the location.
Besides ignore
, developers can use !
to specify files and directories they want to exclude from processing. Basically, ignore
and !
have the same effect.
Here is how our first example can be written using !
instead of ignore
:
inspecode: gofmt: input: - mycode # process the files under the mycode directory located in the root directory. - !mycode/sample* # Do not process files and directories starting with sample and located in the mycode directory.
Note that, even if !
and ignore
can be used simultaneously in the same configuration file, it is quite complicated to do it. Also, the behavior is difficult to understand for most people. To keep your code easy to read and maintain, we advise you to avoid the use of !
within an input
clause.
How to Set-up Global Input Filters
Input filters can be applied at the global level too. By doing so, you will configure a set of rules that apply to all tools. Here is an example of how input settings can be applied at the global level:
inspecode: experimental: input: mycode # Applies to all tools. Process only the mycode file/directory in the root directory. ignore: **/sample* # Applies to all tools. Do not process files and directories starting with sample and located in the mycode directory.
Furthermore, it is possible to use both global settings and tool specific settings at the same time. In this scenario, the tool settings are going to be added to the global settings.
This approach is useful when you need apply several general settings to all tools and then further customize them as needed.
Let’s say you need to exclude from processing all directories starting with sample
. Additionally, gofmt
should ignore all files located in directories starting with gosample
. Here is how you can do it:
inspecode: experimental: input: mycode # We will process only the mycode file/directory, located in the root. It applies to all tools. ignore: **/sample* # Do not process files and directories starting with sample and located in the mycode directory. It applies to all tools. gofmt: input: **/gocode # Process only the gocode files and directories located in mycode directory. It only applies to gofmt. ignore: **/gosample* # Do not process files and directories starting with gosample. It only applies to gofmt.
By using both global level filters and tool level ones, you can build sophisticated and easy to understand configuration rules with just a few lines.