Class: Puppet2conf::DocPusher

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet2conf.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_file: '~/.puppet2conf.yaml', username: nil, password: nil, url: nil, space: nil, ancestor: nil) ⇒ DocPusher

Pass the following variables as parameters:

  • username

  • password

  • url

  • space

  • ancestor

or provide them in configuration file in YAML format. Configuration file location can be overridden by config_file parameter.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/puppet2conf.rb', line 19

def initialize(config_file: '~/.puppet2conf.yaml', username: nil, password: nil, url: nil, space: nil, ancestor: nil)
  params             = {}
  params['username'] = username if username
  params['password'] = password if password
  params['url']      = url if url
  params['space']    = space if space
  params['ancestor'] = ancestor if ancestor

  if File.file? File.expand_path(config_file)
    @config = YAML.load_file(File.expand_path(config_file))
  end

  @config.merge! params
  @client = Conf::Api::Client.new(@config['username'], @config['password'], @config['url'])
end

Instance Method Details

#gendocs(title_page, strings: true) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/puppet2conf.rb', line 83

def gendocs(title_page, strings: true)
  module_html = Md2conf.parse_markdown File.read('README.md')
  push_page(title_page, module_html, @config['ancestor'])
  Dir.glob('*.md').each do |md_file|
    next if md_file.eql? 'README.md'
    html       = if md_file.eql? 'CHANGELOG.md'
      Md2conf.parse_markdown(File.read(md_file), max_toc_level: 2)
    else
      Md2conf.parse_markdown File.read(md_file)
    end
    page_title = "#{title_page} #{File.basename(md_file).sub('.md', '').split('_').map(&:capitalize).join(' ')}"
    push_page(page_title, html, title_page)
  end
  if strings
    PuppetStrings.generate(PuppetStrings::DEFAULT_SEARCH_PATTERNS, json: true, path: 'puppet_strings.json')
    reference_html = Strings2conf.convert(File.read('puppet_strings.json'))
    push_page("#{title_page} Reference", reference_html, title_page)
  end
end

#push_page(page_title, html, ancestor) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/puppet2conf.rb', line 35

def push_page(page_title, html, ancestor)
  page = @client.get(spaceKey: @config['space'], title: page_title)[0]
  if ancestor
    parent_page = @client.get(spaceKey: @config['space'], title: ancestor)[0]
    if parent_page
      ancestors = [{ type: 'page', id: parent_page['id'] }]
    else
      warn "Couldn't find parent page #{ancestor}"
      exit 1
    end
  end
  if page.nil?
    puts "Page '#{page_title}' doesn't exist, creating it"
    response = @client.create(
      type:      'page',
      title:     page_title,
      space:     { key: @config['space'] },
      ancestors: ancestors,
      body:      { storage: { value: html, representation: 'storage' } }
    )
    if response['statusCode'].eql? 400
      warn 'An error occurred while creating:'
      warn response['message']
      warn 'This can happen when a page exists with the same name, but different capitalization. Please fix it manually.'
      exit 1
    end
  else
    page = @client.get_by_id(page['id'])
    puts "Page '#{page_title}' exists. Updating it"
    version  = page['version']['number'] || 1
    response = @client.update(
      page['id'],
      type:      'page',
      id:        page['id'],
      title:     page_title,
      space:     { key: @config['space'] },
      ancestors: ancestors,
      version:   { number: version + 1 },
      body:      { storage: { value: html, representation: 'storage' } }
    )
    if response['statusCode'].eql? 400
      warn 'An error occurred while updating:'
      warn response['message']
      exit 1
    end
  end
end