2016-09-20 11:18:20 +00:00
|
|
|
# Copyright:: Copyright (c) 2015 Chef Software, Inc.
|
|
|
|
# License:: Apache License, Version 2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
require "fileutils"
|
2016-09-22 12:46:42 +00:00
|
|
|
|
2023-01-31 16:01:06 +00:00
|
|
|
DOCS_DIR = "docs-chef-io/content/inspec".freeze
|
|
|
|
|
|
|
|
MENU_MD = <<~MENU.freeze
|
|
|
|
+++
|
|
|
|
title = "InSpec CLI"
|
|
|
|
draft = false
|
|
|
|
gh_repo = "inspec"
|
|
|
|
|
|
|
|
[menu]
|
|
|
|
[menu.inspec]
|
|
|
|
title = "InSpec Executable"
|
|
|
|
identifier = "inspec/reference/cli.md InSpec Executable"
|
|
|
|
parent = "inspec/reference"
|
|
|
|
weight = 10
|
|
|
|
+++
|
|
|
|
|
|
|
|
MENU
|
2018-08-02 15:43:46 +00:00
|
|
|
|
2016-09-20 11:22:10 +00:00
|
|
|
class Markdown
|
|
|
|
class << self
|
|
|
|
def h1(msg)
|
|
|
|
"# #{msg}\n\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def h2(msg)
|
|
|
|
"## #{msg}\n\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def h3(msg)
|
|
|
|
"### #{msg}\n\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def code(msg, syntax = nil)
|
|
|
|
"```#{syntax}\n"\
|
|
|
|
"#{msg}\n"\
|
|
|
|
"```\n\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def li(msg)
|
|
|
|
"* #{msg.gsub("\n", "\n ")}\n"
|
|
|
|
end
|
|
|
|
|
2023-01-31 16:01:06 +00:00
|
|
|
def dl(msg)
|
2023-02-02 07:15:25 +00:00
|
|
|
"<dl>\n#{msg}</dl>\n\n"
|
2023-01-31 16:01:06 +00:00
|
|
|
end
|
|
|
|
|
2016-09-20 11:22:10 +00:00
|
|
|
def ul(msg)
|
|
|
|
msg + "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def p(msg)
|
|
|
|
"#{msg}\n\n"
|
|
|
|
end
|
|
|
|
|
2016-09-22 12:56:20 +00:00
|
|
|
def a(name, dst = nil)
|
|
|
|
dst ||= name
|
|
|
|
"[#{name}](#{dst})"
|
|
|
|
end
|
|
|
|
|
2016-09-20 11:22:10 +00:00
|
|
|
def suffix
|
2019-06-11 22:24:35 +00:00
|
|
|
".md"
|
2016-09-20 11:22:10 +00:00
|
|
|
end
|
2016-09-20 11:44:44 +00:00
|
|
|
|
|
|
|
def meta(opts)
|
|
|
|
o = opts.map { |k, v| "#{k}: #{v}" }.join("\n")
|
2023-01-31 16:01:06 +00:00
|
|
|
"+++\n#{o}\n+++\n\n"
|
2016-09-20 11:44:44 +00:00
|
|
|
end
|
2016-09-20 11:22:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-20 11:18:20 +00:00
|
|
|
class RST
|
|
|
|
class << self
|
|
|
|
def h1(msg)
|
|
|
|
"=====================================================\n"\
|
|
|
|
"#{msg}\n"\
|
|
|
|
"=====================================================\n\n"\
|
|
|
|
end
|
|
|
|
|
|
|
|
def h2(msg)
|
|
|
|
"#{msg}\n"\
|
|
|
|
"=====================================================\n\n"\
|
|
|
|
end
|
|
|
|
|
|
|
|
def h3(msg)
|
|
|
|
"#{msg}\n"\
|
|
|
|
"-----------------------------------------------------\n\n"\
|
|
|
|
end
|
|
|
|
|
|
|
|
def code(msg, syntax = nil)
|
2016-09-20 11:22:10 +00:00
|
|
|
".. code-block:: #{syntax}\n\n"\
|
2016-09-20 11:18:20 +00:00
|
|
|
" #{msg.gsub("\n", "\n ")}\n\n"
|
|
|
|
end
|
|
|
|
|
2016-09-20 11:22:10 +00:00
|
|
|
def li(msg)
|
|
|
|
"#{msg.gsub("\n", "\n ")}\n\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def ul(msg)
|
|
|
|
msg
|
|
|
|
end
|
|
|
|
|
2016-09-20 11:18:20 +00:00
|
|
|
def p(msg)
|
|
|
|
"#{msg}\n\n"
|
|
|
|
end
|
2016-09-20 11:22:10 +00:00
|
|
|
|
2016-09-22 12:56:20 +00:00
|
|
|
def a(name, _dst = nil)
|
|
|
|
# FIXME: needs link handling
|
|
|
|
"`#{name}`_"
|
|
|
|
end
|
|
|
|
|
2016-09-20 11:22:10 +00:00
|
|
|
def suffix
|
2019-06-11 22:24:35 +00:00
|
|
|
".rst"
|
2016-09-20 11:22:10 +00:00
|
|
|
end
|
2016-09-20 11:44:44 +00:00
|
|
|
|
|
|
|
def meta(_o)
|
2019-06-11 22:24:35 +00:00
|
|
|
"" # ignore for now
|
2016-09-20 11:44:44 +00:00
|
|
|
end
|
2016-09-20 11:18:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-21 07:49:41 +00:00
|
|
|
namespace :docs do # rubocop:disable Metrics/BlockLength
|
2019-06-11 22:24:35 +00:00
|
|
|
desc "Create cli docs"
|
2016-09-20 11:18:20 +00:00
|
|
|
task :cli do
|
2016-09-22 11:44:09 +00:00
|
|
|
# formatter for the output file
|
2016-09-22 11:41:20 +00:00
|
|
|
f = Markdown
|
2016-09-22 11:44:09 +00:00
|
|
|
# list of subcommands we ignore; these are e.g. plugins
|
|
|
|
skip_commands = %w{scap}
|
|
|
|
|
2023-01-31 16:01:06 +00:00
|
|
|
res = ""
|
|
|
|
res << MENU_MD
|
|
|
|
res << f.p("<!-- markdownlint-disable MD024 -->\n\nUse the InSpec Command Line Interface (CLI) to run tests and audits against targets "\
|
2019-06-11 22:24:35 +00:00
|
|
|
"using local, SSH, WinRM, or Docker connections.")
|
2016-09-20 11:18:20 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
require "inspec/cli"
|
2016-09-20 11:18:20 +00:00
|
|
|
cmds = Inspec::InspecCLI.all_commands
|
|
|
|
cmds.keys.sort.each do |key|
|
2016-09-22 11:44:09 +00:00
|
|
|
next if skip_commands.include? key
|
2019-07-09 00:20:30 +00:00
|
|
|
|
2016-09-20 11:18:20 +00:00
|
|
|
cmd = cmds[key]
|
|
|
|
|
|
|
|
res << f.h2(cmd.usage.split.first)
|
|
|
|
res << f.p(cmd.description.capitalize)
|
2018-06-29 12:43:34 +00:00
|
|
|
if cmd.long_description
|
|
|
|
res << f.p(cmd.long_description)
|
|
|
|
end
|
2016-09-20 11:18:20 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
res << f.h3("Syntax")
|
|
|
|
res << f.p("This subcommand has the following syntax:")
|
2023-01-31 16:01:06 +00:00
|
|
|
res << f.code("inspec #{cmd.usage}", "bash")
|
2016-09-20 11:18:20 +00:00
|
|
|
|
2017-11-21 07:49:41 +00:00
|
|
|
opts = cmd.options.reject { |_, o| o.hide }
|
2016-09-20 11:18:20 +00:00
|
|
|
unless opts.empty?
|
2023-01-31 16:01:06 +00:00
|
|
|
res << f.h3("Options") + f.p("This subcommand has the following additional options:")
|
2016-09-20 11:18:20 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
list = ""
|
2016-09-20 11:18:20 +00:00
|
|
|
opts.keys.sort.each do |option|
|
|
|
|
opt = cmd.options[option]
|
|
|
|
# TODO: remove when UX of help is reworked 1.0
|
2019-06-11 22:24:35 +00:00
|
|
|
usage = opt.usage.split(", ")
|
2019-07-09 00:20:30 +00:00
|
|
|
.map { |x| x.tr("[]", "") }
|
|
|
|
.map { |x| x.start_with?("-") ? x : "-" + x }
|
2023-02-01 11:02:00 +00:00
|
|
|
.map { |x| "<code>" + x + "</code>" }
|
|
|
|
msg = "<dt>#{usage.join(", ")}</dt>\n"
|
2023-02-02 07:15:25 +00:00
|
|
|
msg << "<dd>#{opt.description}</dd>\n\n" if opt.description && !opt.description.empty?
|
|
|
|
list << msg
|
2016-09-20 11:22:10 +00:00
|
|
|
end.join
|
2023-02-02 07:15:25 +00:00
|
|
|
res << f.dl(list)
|
2016-09-20 11:18:20 +00:00
|
|
|
end
|
2016-09-20 11:22:10 +00:00
|
|
|
|
|
|
|
# FIXME: for some reason we have extra lines in our RST; needs investigation
|
|
|
|
res << "\n\n" if f == RST
|
2016-09-20 11:18:20 +00:00
|
|
|
end
|
|
|
|
|
2023-01-31 16:01:06 +00:00
|
|
|
dst = File.join(pwd, DOCS_DIR , "cli#{f.suffix}")
|
2016-09-20 11:18:20 +00:00
|
|
|
File.write(dst, res)
|
|
|
|
puts "Documentation generated in #{dst.inspect}"
|
|
|
|
end
|
2016-09-22 11:41:20 +00:00
|
|
|
end
|
|
|
|
|
2023-01-31 15:07:38 +00:00
|
|
|
# NOTE: Many of the docs tasks were removed in PR #6367 (https://github.com/inspec/inspec/pull/6367)
|