0.19.3 • Published 1 year ago

replicated-lint v0.19.3

Weekly downloads
702
License
MIT
Repository
github
Last release
1 year ago

replicated-lint

CircleCI Code Climate Test Coverage

YAML linting tools for Replicated applications.

Usage

Install the CLI executable with

npm install -g replicated-lint

Lint with replicated-lint validate

replicated-lint validate -f my-app.yml

or pipe from stdin:

cat my-app.yml | replicated-lint validate -f -

Results that have issues will look something like:

{ type: 'info',
  rule: 'prop-configitem-testproc-run-on-save',
  message: 'If a config item\'s test_proc.run_on_save is not set to \'true\', test_proc\'s will not be checked automatically. Consider setting your test_proc\'s run_on_save to automatically validate inputs',
  positions:
   [ { path: 'config.1.items.2.test_proc',
       start: { position: 8130, line: 325, column: 4 },
       end: { position: 8322, line: 331, column: 0 } },
     { path: 'config.3.test_proc',
       start: { position: 8692, line: 346, column: 2 },
       end: { position: 9141, line: 365, column: 2 } } ],
  links: [ 'https://www.replicated.com/docs/packaging-an-application/test-procs/' ] }

# prop-configitem-testproc-run-on-save continued from line 321
322  
323    - name: phone_number
324      type: text
325      test_proc:
326        display_name: Is this a Phone Number?
327        command: regex_match
328        args:
329        - "([0-9]{3})[-]([0-9]{3})[-]([0-9]{4})$"
330        - "That doesn't seem to be a phone number!"
331  - name: auth
332    title: Authentication
333    description: Where will user accounts be provisioned
334    items:

Extending the CLI with custom rules

replicated-lint rules can be expressed as JSON, so it is easy to add your own custom rules.

If you have a custom rule set in no-latest.json, you can pass it to replicated-lint using

cat my-app.yml | replicated-lint validate -f - --extraRules no-latest.yaml

--extraRules can be specified multiple times. An example YAML rule set might look something like

---
- name: custom-no-latest
  type: error
  message: "Don't use `latest` for container versions."
  test:
    AnyOf:
      path: components
      pred:
        AnyOf: containers
        pred:
          Eq:
            path: version
            value: latest

replicated-lint validate supports the following options:

Options:
  --version         Show version number                                [boolean]
  --help            Show help                                          [boolean]
  --infile, -f      Input file to validate. Use "-" for stdin
                                                         [string] [default: "-"]
  --threshold, -t   Threshold of of issues to report
                  [string] [choices: "info", "warn", "error"] [default: "error"]
  --extraRules, -e  Path to file containing JSON definitions for additional yaml
                    rules. Can be specified multiple times.[array] [default: []]
  --reporter, -r    Output Format to use
                     [string] [choices: "console", "junit"] [default: "console"]
  --outputDir, -o   junit reporter only -- path to directory to output junit xml
                    reports                   [string] [default: "test-results"]

Developing

Installing Dependencies

We compile to ES5 and test on nodejs 7.8.0, but earlier versions of node should work as well.

npm install -g yarn
yarn

Running the tests

To run the tests once

yarn test

To watch files and re-run tests on changes, use the tdd script

./tdd

Before committing

Run

make project-import PROJECT=<project-name>

Where <project-name> is the project being updated and should be one of

  • replicated-supportbundle
  • replicated-entitlements
  • replicated-rbac
  • replicated-ship

    For more up to date list check the project directory listing.

Regenerating the documentation

replicated-lint docs gen will print reference documentation stdout as markdown. yarn docs will write it to docs/gen.md.

To update the docs in help center, grab everything in replicated-lint/docs/gen.md and paste it into help-center/.../yaml.md, replacing everything below <!-- the rest of this document is autogenerated-->.

Library Usage

replicated-lint is designed with extensibility in mind, making it suitable for general purpose YAML and JSON linting and policy definition

  • Flexible, modular rule sets
  • CLI
  • Autogenerate unit tests and documentation from rule definitions
npm install --save replicated-lint
import * as linter from "replicated-lint";

const yaml = `
---
replicated_api_version: 2.8.0
components:
  - name: ELK
    containers:
      - image: getelk/search`;


const ruleViolations = linter.defaultLint(yaml);
console.log(ruleViolations); // []

Custom Rule Sets

linter.rules.all can be substituted or extended with custom rule definitions. A rule's test field should return { matched: true } when the rule is triggered by invalid JSON.

import * as linter from "replicated-lint";

const yaml = `
---
foo:
  bar: baz`;

const testSpec: any = {
  Or: {
    preds: [
      {Eq: { path: "foo.bar", value: "baz"}},
      {Eq: { path: "foo.bar", value: "boz"}},
    ],
  }
}

const rules: linter.YAMLRule[] = [
  {
    name: "foo-bar-neq-baz",
    message: "foo.bar can't be baz!",
    test: testSpec,
    type: "error",
  }
];

const ruleViolations = linter.lint(yaml, { rules });
console.log(ruleViolations);  /*
[{
        type: "error",
        positions: [{
          path: "foo.bar",
          start: {
            position: 12,
            line: 3,
            column: 2,
          },
          end: {
            position: 20,
            line: 3,
            column: 10,
          },
        }],
        message: "foo.bar can't be baz!",
}]
*/

Register new rules with linter.enginer.register. Rules should implement JSONReadable<Predicate<any>>, usually as a static method

import * as linter from "replicated-lint";


// rule MyRule checks if root object has property "spam" equal to "eggs"
class MyRule implements linter.Predicate<any> {
  test(obj: any) {
    const matched = obj.spam !== "eggs"; // fail when spam != eggs
    const paths = ["spam"];              // if matched == true, optionally include a path where a rule was violated
    return { matched, paths };
  }

  public static fromJson(obj: any, registry: linter.engine.Registry) {
    return new MyRule();
  }
}

linter.engine.register(MyRule);

const testSpec: linter.Test = { MyRule: {}};

const rules: linter.YAMLRule[] = [
  {
    name: "spam-eq-eggs",
    message: "spam must be equal to eggs!",
    test: testSpec,
    type: "error",
  }
];

const yaml = `
---
spam: eggs`;

const ruleViolations = linter.lint(yaml, { rules });
console.log(ruleViolations); // []

Custom implementations of engine.Registry can also be passed as a third argument to linter.lint, otherwise a default registry will be used.

0.19.2

1 year ago

0.19.3

1 year ago

0.19.0

2 years ago

0.19.1

2 years ago

0.18.9

3 years ago

0.18.8

3 years ago

0.18.7

4 years ago

0.18.6

4 years ago

0.18.5

4 years ago

0.18.4

4 years ago

0.18.3

4 years ago

0.18.2

4 years ago

0.18.1

4 years ago

0.18.0

5 years ago

0.17.0

5 years ago

0.16.1

5 years ago

0.16.0

5 years ago

0.15.1

5 years ago

0.15.0

5 years ago

0.14.20

5 years ago

0.14.19

5 years ago

0.14.18

5 years ago

0.14.17

5 years ago

0.14.16

5 years ago

0.14.15

5 years ago

0.14.14

5 years ago

0.14.13

5 years ago

0.14.12

5 years ago

0.14.11

5 years ago

0.14.10

5 years ago

0.14.9

5 years ago

0.14.8

5 years ago

0.14.7

5 years ago

0.14.6

5 years ago

0.14.5

5 years ago

0.14.4

5 years ago

0.14.3

5 years ago

0.13.22

6 years ago

0.13.21

6 years ago

0.13.20

6 years ago

0.13.19

6 years ago

0.13.18

6 years ago

0.13.17

6 years ago

0.13.16

6 years ago

0.13.15

6 years ago

0.13.14

6 years ago

0.13.13

6 years ago

0.13.12

6 years ago

0.13.11

6 years ago

0.13.10

6 years ago

0.13.8

6 years ago

0.13.7

6 years ago

0.13.6

6 years ago

0.13.5

6 years ago

0.13.4

6 years ago

0.13.3

6 years ago

0.13.2

6 years ago

0.13.1

6 years ago

0.13.0

6 years ago

0.12.12

6 years ago

0.12.11

6 years ago

0.12.10

6 years ago

0.12.9

6 years ago

0.12.8

6 years ago

0.12.7

6 years ago

0.12.6

6 years ago

0.12.5

6 years ago

0.12.4

6 years ago

0.12.3

6 years ago

0.12.2

6 years ago

0.12.1

6 years ago

0.12.0

6 years ago

0.11.15

6 years ago

0.11.14

6 years ago

0.11.13

6 years ago

0.11.12

6 years ago

0.11.11

6 years ago

0.11.10

6 years ago

0.11.9

6 years ago

0.11.8

6 years ago

0.11.7

6 years ago

0.11.6

6 years ago

0.11.5

6 years ago

0.11.4

6 years ago

0.11.3

6 years ago

0.11.2

6 years ago

0.11.1

6 years ago

0.11.0

6 years ago

0.10.8

6 years ago

0.10.7

6 years ago

0.10.6

6 years ago

0.10.5

6 years ago

0.10.4

6 years ago

0.10.3

6 years ago

0.10.2

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.6

6 years ago

0.9.5

6 years ago

0.9.4

6 years ago

0.9.3

6 years ago

0.9.2

6 years ago

0.9.1

6 years ago

0.8.4

7 years ago

0.8.3

7 years ago

0.8.2

7 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.3

7 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.37

7 years ago

0.6.36

7 years ago

0.6.35

7 years ago

0.6.34

7 years ago

0.6.33

7 years ago

0.6.32

7 years ago

0.6.31

7 years ago

0.6.30

7 years ago

0.6.29

7 years ago

0.6.28

7 years ago

0.6.27

7 years ago

0.6.26

7 years ago

0.6.25

7 years ago

0.6.24

7 years ago

0.6.23

7 years ago

0.6.22

7 years ago

0.6.21

7 years ago

0.6.20

7 years ago

0.6.19

7 years ago

0.6.18

7 years ago

0.6.17

7 years ago

0.6.16

7 years ago

0.6.15

7 years ago

0.6.14

7 years ago

0.6.13

7 years ago

0.6.12

7 years ago

0.6.11

7 years ago

0.6.10

7 years ago

0.6.9

7 years ago

0.6.8

7 years ago

0.6.7

7 years ago

0.6.6

7 years ago

0.6.5

7 years ago

0.6.4

7 years ago

0.6.3

7 years ago

0.6.2

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago