Class: Spreen::Wiki::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/spreen/wiki/cli.rb,
sig/generated/spreen/wiki/cli.rbs

Overview

Command line interface behind the spreen executable: spreen <update|count-report|llm-export> [options].

Constant Summary collapse

HELP =

Returns:

  • (::String)
<<~HELP
  Usage: spreen <command> [options]

  Commands:
    update        Regenerate Home.md and _Sidebar.md from the wiki tree
    count-report  Export a count report of the unknown-namespace wikis
    llm-export    Export the unknown-namespace wiki list for an LLM
    version       Print the version

  Run `spreen <command> --help` to list the command's options.
HELP
COMMON_OPTIONS =

Flags shared by every command, mapped to the keyword arguments of the underlying classes. --exclude and the per-command flags are wired up separately because they need a type or a fixed value.

Returns:

  • (Object)
{
  '--path PATH' => [:base_path, 'Path to the wiki clone (default: current directory)'],
  '--group-by GROUP_BY' => [:group_by, 'Grouping criteria (default: Owner)'],
  '--language LANGUAGE' => [:language, 'Language of the labels (default: English)'],
  '--org ORGANISATION' => [:organisation, 'GitHub organisation/user hosting the wiki'],
  '--repo REPOSITORY' => [:repository, 'GitHub repository whose wiki is organised'],
  '--wiki-url URL' => [:wiki_url, 'Wiki URL (overrides the one derived from --org/--repo)'],
  '--template-dir DIR' => [:template_dir, 'Directory of <group_by>/<language>.md Home templates'],
  '--config FILE' => [:config_path, 'Config file path (default: <path>/.spreen.yml)']
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.

Parameters:

  • argv (Array[String])


50
51
52
53
# File 'lib/spreen/wiki/cli.rb', line 50

def initialize(argv)
  @argv    = argv.dup
  @options = { base_path: Dir.pwd, group_by: 'Owner', language: 'English', home_overflow: 'false' }
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.

Returns:

  • (Object)


68
69
70
# File 'lib/spreen/wiki/cli.rb', line 68

def argv
  @argv
end

#optionsObject (readonly)

Returns the value of attribute options.

Returns:

  • (Object)


68
69
70
# File 'lib/spreen/wiki/cli.rb', line 68

def options
  @options
end

Class Method Details

.start(argv = ARGV) ⇒ Integer

Parameters:

  • argv (Array[String]) (defaults to: ARGV)

Returns:

  • (Integer)


44
45
46
# File 'lib/spreen/wiki/cli.rb', line 44

def self.start(argv = ARGV)
  new(argv).run
end

Instance Method Details

#add_command_options(opt, command) ⇒ void

This method returns an undefined value.

Parameters:

  • opt (OptionParser)
  • command (String)


146
147
148
149
150
151
152
153
154
155
156
# File 'lib/spreen/wiki/cli.rb', line 146

def add_command_options(opt, command)
  if command == 'update'
    opt.on('--overflow', 'Split Home into per-namespace pages under wikis-by-owner/') do
      options[:home_overflow] = 'true'
    end
  else
    opt.on('--output FILENAME', 'Export filename (relative to --path unless absolute)') do |filename|
      options[:output] = filename
    end
  end
end

#add_common_options(opt) ⇒ void

This method returns an undefined value.

Parameters:

  • opt (OptionParser)


134
135
136
137
138
139
140
141
# File 'lib/spreen/wiki/cli.rb', line 134

def add_common_options(opt)
  COMMON_OPTIONS.each do |flag, (key, description)|
    opt.on(flag, description) { |value| options[key] = value }
  end
  opt.on('--exclude DIR1,DIR2', Array, 'Directories to skip while scanning') do |dirs|
    options[:excluded_dirs] = dirs
  end
end

#count_reportvoid

This method returns an undefined value.



111
112
113
114
115
# File 'lib/spreen/wiki/cli.rb', line 111

def count_report
  count_list_by_namespace, path_to_export = UnknownWikiCountListExporter.run(**options)
  puts count_list_by_namespace
  puts "Exported the unknown wiki count report to '#{path_to_export}'."
end

#execute(command) ⇒ Integer

Parameters:

  • command (String)

Returns:

  • (Integer)


72
73
74
75
76
77
78
79
# File 'lib/spreen/wiki/cli.rb', line 72

def execute(command)
  parser(command).parse!(argv)
  __send__(command.tr('-', '_'))
  0
rescue ArgumentError, OptionParser::ParseError => e
  warn e.message
  1
end

#llm_exportvoid

This method returns an undefined value.



118
119
120
121
# File 'lib/spreen/wiki/cli.rb', line 118

def llm_export
  path_to_export = UnknownWikiListExporterForLLM.run(**options)
  puts "Exported the unknown wiki list for LLM to '#{path_to_export}'."
end

#parser(command) ⇒ OptionParser

Parameters:

  • command (String)

Returns:

  • (OptionParser)


125
126
127
128
129
130
# File 'lib/spreen/wiki/cli.rb', line 125

def parser(command)
  OptionParser.new("Usage: spreen #{command} [options]") do |opt|
    add_common_options(opt)
    add_command_options(opt, command)
  end
end

Parameters:

  • command (String, nil)

Returns:

  • (Integer)


89
90
91
92
# File 'lib/spreen/wiki/cli.rb', line 89

def print_help(command)
  puts HELP
  command.nil? ? 1 : 0
end

Returns:

  • (Integer)


82
83
84
85
# File 'lib/spreen/wiki/cli.rb', line 82

def print_version
  puts VERSION
  0
end

#runInteger

Returns:

  • (Integer)


56
57
58
59
60
61
62
63
64
# File 'lib/spreen/wiki/cli.rb', line 56

def run
  command = argv.shift
  case command
  when 'update', 'count-report', 'llm-export' then execute(command)
  when 'version', '--version', '-v'           then print_version
  when nil, 'help', '--help', '-h'            then print_help(command)
  else unknown_command(command)
  end
end

#unknown_command(command) ⇒ Integer

Parameters:

  • command (String)

Returns:

  • (Integer)


96
97
98
99
100
# File 'lib/spreen/wiki/cli.rb', line 96

def unknown_command(command)
  warn "Unknown command: `#{command}`"
  warn HELP
  1
end

#updatevoid

This method returns an undefined value.



103
104
105
106
107
108
# File 'lib/spreen/wiki/cli.rb', line 103

def update
  wiki_url = Home.run(**options)
  Sidebar.run(**options)
  puts 'Updated Home.md and _Sidebar.md.'
  puts "Check them out at '#{wiki_url}' !!" if wiki_url
end