Module: Docscribe::CLI::Init

Defined in:
lib/docscribe/cli/init.rb

Class Method Summary collapse

Class Method Details

.run(argv) ⇒ Integer

Create or print a starter Docscribe configuration file.

Supported behaviors:

  • write ‘docscribe.yml` (default)

  • write to a custom path via ‘–config`

  • overwrite an existing file via ‘–force`

  • print the template to STDOUT via ‘–stdout`

Parameters:

  • argv (Array<String>)

    command-line arguments for ‘docscribe init`

Returns:

  • (Integer)

    process exit code



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/docscribe/cli/init.rb', line 20

def run(argv)
  opts = {
    config: 'docscribe.yml',
    force: false,
    stdout: false
  }

  OptionParser.new do |o|
    o.banner = 'Usage: docscribe init [options]'
    o.on('--config PATH', 'Where to write the config (default: docscribe.yml)') { |v| opts[:config] = v }
    o.on('-f', '--force', 'Overwrite if the file already exists') { opts[:force] = true }
    o.on('--stdout', 'Print config template to STDOUT instead of writing a file') { opts[:stdout] = true }
    o.on('-h', '--help', 'Show this help') do
      puts o
      return 0
    end
  end.parse!(argv)

  yaml = Docscribe::Config.default_yaml

  if opts[:stdout]
    puts yaml
    return 0
  end

  path = opts[:config]
  if File.exist?(path) && !opts[:force]
    warn "Config already exists: #{path} (use --force to overwrite)"
    return 1
  end

  File.write(path, yaml)
  puts "Created: #{path}"
  0
end