Class: Goodcheck::Commands::Init

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

Constant Summary collapse

CONFIG =
"rules:\n  # id, pattern, message are required attributes.\n  - id: example.github\n    pattern: Github\n    message: Do you want to write GitHub?\n    glob:\n- \"**/*.rb\"\n- \"**/*.{yaml,yml}\"\n- \"public/**/*.html\"\n    fail:\n- Signup via Github\n    pass:\n- Signup via GitHub\n\n  # You can have *justification* to explain the exceptional cases.\n  - id: example.localStorage\n    pattern:\ntoken: localStorage\n    message: |\nUsing localStorage may raise error (example: with Safari in private mode)\n    justification:\n- If you catch the errors.\n- When you implement admin console, where end users won't access.\n    glob:\n- \"**/*.js\"\n    fail:\n- |\n  localStorage.setItem(key, value);\n\n  # You can put `not` pattern to detect something is missing.\n  - id: example.strict-mode\n    not:\npattern: use strict\n    message: Use *strict mode* if possible.\n    glob: \"**/*.js\"\n    fail:\n- |\n  const var = \"This is *sloppy* mode.\"\n\n  # You can omit pattern, which prints the message on the files specified by glob.\n  - id: example.package-lock.json\n    message: Some of the packages are updated!\n    justification:\n- If you update some of the packages.\n    glob: \"package-lock.json\"\n\n# You can import rules.\n# import:\n#   - https://example.com/example-rules.yml\n\n# You can skip checking files.\n# exclude:\n#   - node_modules\n#   - vendor\n"

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