Class: Goodcheck::Commands::Init

Inherits:
Object
  • Object
show all
Includes:
ExitStatus
Defined in:
lib/goodcheck/commands/init.rb

Constant Summary collapse

CONFIG =
<<-EOC
rules:
  # id, pattern, message are required attributes.
  - id: example.github
    pattern: Github
    message: Do you want to write GitHub?
    glob:
- "**/*.rb"
- "**/*.{yaml,yml}"
- "public/**/*.html"
    fail:
- Signup via Github
    pass:
- Signup via GitHub

  # You can have *justification* to explain the exceptional cases.
  - id: example.localStorage
    pattern:
token: localStorage
    message: |
Using localStorage may raise error (example: with Safari in private mode)
    justification:
- If you catch the errors.
- When you implement admin console, where end users won't access.
    glob:
- "**/*.js"
    fail:
- |
  localStorage.setItem(key, value);

  # You can put `not` pattern to detect something is missing.
  - id: example.strict-mode
    not:
pattern: use strict
    message: Use *strict mode* if possible.
    glob: "**/*.js"
    fail:
- |
  const var = "This is *sloppy* mode."

  # You can omit pattern, which prints the message on the files specified by glob.
  - id: example.package-lock.json
    message: Some of the packages are updated!
    justification:
- If you update some of the packages.
    glob: "package-lock.json"

# You can import rules.
# import:
#   - https://example.com/example-rules.yml

# You can skip checking files.
# exclude:
#   - node_modules
#   - vendor
EOC

Constants included from ExitStatus

ExitStatus::EXIT_ERROR, ExitStatus::EXIT_MATCH, ExitStatus::EXIT_SUCCESS, ExitStatus::EXIT_TEST_FAILED

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout:, stderr:, path:, force:) ⇒ Init

Returns a new instance of Init.



68
69
70
71
72
73
# File 'lib/goodcheck/commands/init.rb', line 68

def initialize(stdout:, stderr:, path:, force:)
  @stdout = stdout
  @stderr = stderr
  @path = path
  @force = force
end

Instance Attribute Details

#forceObject (readonly)

Returns the value of attribute force.



66
67
68
# File 'lib/goodcheck/commands/init.rb', line 66

def force
  @force
end

#pathObject (readonly)

Returns the value of attribute path.



65
66
67
# File 'lib/goodcheck/commands/init.rb', line 65

def path
  @path
end

#stderrObject (readonly)

Returns the value of attribute stderr.



64
65
66
# File 'lib/goodcheck/commands/init.rb', line 64

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



63
64
65
# File 'lib/goodcheck/commands/init.rb', line 63

def stdout
  @stdout
end

Instance Method Details

#runObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/goodcheck/commands/init.rb', line 75

def run
  if path.file? && !force
    stderr.puts "#{path} already exists. Try --force option to overwrite the file."
    return EXIT_ERROR
  end

  path.open("w") do |io|
    io.print(CONFIG)
  end

  stdout.puts "Wrote #{path}. ✍️"

  EXIT_SUCCESS
end