Skip to content

Commit

Permalink
Merge cc345bb into 85be402
Browse files Browse the repository at this point in the history
  • Loading branch information
logicminds committed Apr 4, 2019
2 parents 85be402 + cc345bb commit 1c9a981
Show file tree
Hide file tree
Showing 11 changed files with 481 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ group :release do
gem 'github_changelog_generator', :require => false, :git => 'https://github.com/github-changelog-generator/github-changelog-generator' if RUBY_VERSION >= '2.2.2'
gem 'puppet-blacksmith', :require => false
gem 'voxpupuli-release', :require => false, :git => 'https://github.com/voxpupuli/voxpupuli-release-gem'
gem 'puppet-strings', '>= 1.0', :require => false
gem 'puppet-strings', '>= 2.0', :require => false
end


Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#### Table of Contents

1. [Overview](#overview)
3. [Setup - The basics of getting started with extlib](#setup)
4. [Usage - Configuration options and additional functionality](#usage)
5. [Limitations - OS compatibility, etc.](#limitations)
6. [Development - Guide for contributing to the module](#development)
2. [Setup - The basics of getting started with extlib](#setup)
3. [Usage - Configuration options and additional functionality](#usage)
4. [Limitations - OS compatibility, etc.](#limitations)
5. [Development - Guide for contributing to the module](#development)

## Overview

Expand Down Expand Up @@ -44,6 +44,7 @@ the process, so, yes, please submit that pull request!

Reference documentation is generated using puppet-strings.
To regenerate it, please run the rake task as follows.

```console
% bundle exec rake strings:generate\['lib/puppet/functions/extlib/*.rb,,,,false,true']
% bundle exec puppet strings generate --format=markdown
```
217 changes: 217 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,101 @@

**Functions**

* [`cache_data`](#cache_data): DEPRECATED. Use the namespaced function [`extlib::cache_data`](#extlibcache_data) instead.
* [`default_content`](#default_content): DEPRECATED. Use the namespaced function [`extlib::default_content`](#extlibdefault_content) instead.
* [`dump_args`](#dump_args): DEPRECATED. Use the namespaced function [`extlib::dump_args`](#extlibdump_args) instead.
* [`echo`](#echo): DEPRECATED. Use the namespaced function [`extlib::echo`](#extlibecho) instead.
* [`extlib::cache_data`](#extlibcache_data): Retrieves data from a cache file, or creates it with supplied data if the file doesn't exist
* [`extlib::default_content`](#extlibdefault_content): Takes an optional content and an optional template name and returns the contents of a file.
* [`extlib::dir_split`](#extlibdir_split): Splits the given directory or directories into individual paths.
* [`extlib::dump_args`](#extlibdump_args): Prints the args to STDOUT in Pretty JSON format.
* [`extlib::echo`](#extlibecho): This function outputs the variable content and its type to the debug log. It's similiar to the `notice` function but provides a better output
* [`extlib::file_separator`](#extlibfile_separator): Returns the os specific file path separator.
* [`extlib::has_module`](#extlibhas_module): A function that lets you know whether a specific module is on your modulepath.
* [`extlib::ip_to_cron`](#extlibip_to_cron): Provides a "random" value to cron based on the last bit of the machine IP address. used to avoid starting a certain cron job at the same time
* [`extlib::mkdir_p`](#extlibmkdir_p): Like the unix command mkdir_p except with puppet code.
* [`extlib::path_join`](#extlibpath_join): Take one or more paths and join them together using the os specific separator.
* [`extlib::random_password`](#extlibrandom_password): A function to return a string of arbitrary length that contains randomly selected characters.
* [`extlib::resources_deep_merge`](#extlibresources_deep_merge): Deeply merge a "defaults" hash into a "resources" hash like the ones expected by `create_resources()`.
* [`extlib::sort_by_version`](#extlibsort_by_version): A function that sorts an array of version numbers.
* [`ip_to_cron`](#ip_to_cron): DEPRECATED. Use the namespaced function [`extlib::ip_to_cron`](#extlibip_to_cron) instead.
* [`random_password`](#random_password): DEPRECATED. Use the namespaced function [`extlib::random_password`](#extlibrandom_password) instead.
* [`resources_deep_merge`](#resources_deep_merge): DEPRECATED. Use the namespaced function [`extlib::resources_deep_merge`](#extlibresources_deep_merge) instead.

## Functions

### cache_data

Type: Ruby 4.x API

DEPRECATED. Use the namespaced function [`extlib::cache_data`](#extlibcache_data) instead.

#### `cache_data(Any *$args)`

DEPRECATED. Use the namespaced function [`extlib::cache_data`](#extlibcache_data) instead.

Returns: `Any`

##### `*args`

Data type: `Any`



### default_content

Type: Ruby 4.x API

DEPRECATED. Use the namespaced function [`extlib::default_content`](#extlibdefault_content) instead.

#### `default_content(Any *$args)`

DEPRECATED. Use the namespaced function [`extlib::default_content`](#extlibdefault_content) instead.

Returns: `Any`

##### `*args`

Data type: `Any`



### dump_args

Type: Ruby 4.x API

DEPRECATED. Use the namespaced function [`extlib::dump_args`](#extlibdump_args) instead.

#### `dump_args(Any *$args)`

DEPRECATED. Use the namespaced function [`extlib::dump_args`](#extlibdump_args) instead.

Returns: `Any`

##### `*args`

Data type: `Any`



### echo

Type: Ruby 4.x API

DEPRECATED. Use the namespaced function [`extlib::echo`](#extlibecho) instead.

#### `echo(Any *$args)`

DEPRECATED. Use the namespaced function [`extlib::echo`](#extlibecho) instead.

Returns: `Any`

##### `*args`

Data type: `Any`



### extlib::cache_data

Type: Ruby 4.x API
Expand Down Expand Up @@ -85,6 +168,26 @@ Data type: `Optional[String]`

The path to an .erb or .epp template file or `undef`.

### extlib::dir_split

Type: Puppet Language

Use this function when you need to split a absolute path into multiple absolute paths
that all descend from the given path.

#### `extlib::dir_split(Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]] $dirs)`

Use this function when you need to split a absolute path into multiple absolute paths
that all descend from the given path.

Returns: `Array[String]` - an array of absolute paths after being cut into individual paths.

##### `dirs`

Data type: `Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]]`

- either an absolute path or a array of absolute paths.

### extlib::dump_args

Type: Ruby 4.x API
Expand Down Expand Up @@ -193,6 +296,18 @@ Data type: `Optional[String]`

An optional comment to prepend to the debug output.

### extlib::file_separator

Type: Puppet Language

Returns the os specific file path separator.

#### `extlib::file_separator()`

The extlib::file_separator function.

Returns: `String` - The os specific path separator.

### extlib::has_module

Type: Ruby 4.x API
Expand Down Expand Up @@ -248,6 +363,54 @@ Data type: `Optional[Integer[1]]`

The number of seconds to use as the run interval

### extlib::mkdir_p

Type: Puppet Language

This creates file resources for all directories and utilizes the dir_split() function
to get a list of all the descendant directories. You will have no control over any other parameters
for the file resource. If you wish to control the file resources you can use the dir_split() function
and get an array of directories for use in your own code. Please note this does not use an exec resource.
* **Note** splits the given directories into paths that are then created using file resources


#### `extlib::mkdir_p(Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]] $dirs)`

This creates file resources for all directories and utilizes the dir_split() function
to get a list of all the descendant directories. You will have no control over any other parameters
for the file resource. If you wish to control the file resources you can use the dir_split() function
and get an array of directories for use in your own code. Please note this does not use an exec resource.

Returns: `Array[Stdlib::Absolutepath]`

##### `dirs`

Data type: `Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]]`

- the path(s) to create

### extlib::path_join

Type: Puppet Language

Because in how windows uses a different separator this function
will format a windows path into a equilivent unix like path. This type of unix like
path will work on windows.

#### `extlib::path_join(Array[String] $dirs)`

Because in how windows uses a different separator this function
will format a windows path into a equilivent unix like path. This type of unix like
path will work on windows.

Returns: `Stdlib::Absolutepath` - The joined path

##### `dirs`

Data type: `Array[String]`

- Joins two or more directories by file separator.

### extlib::random_password

Type: Ruby 4.x API
Expand Down Expand Up @@ -436,3 +599,57 @@ Data type: `Array[String]`

An array of version strings you want sorted.

### ip_to_cron

Type: Ruby 4.x API

DEPRECATED. Use the namespaced function [`extlib::ip_to_cron`](#extlibip_to_cron) instead.

#### `ip_to_cron(Any *$args)`

DEPRECATED. Use the namespaced function [`extlib::ip_to_cron`](#extlibip_to_cron) instead.

Returns: `Any`

##### `*args`

Data type: `Any`



### random_password

Type: Ruby 4.x API

DEPRECATED. Use the namespaced function [`extlib::random_password`](#extlibrandom_password) instead.

#### `random_password(Any *$args)`

DEPRECATED. Use the namespaced function [`extlib::random_password`](#extlibrandom_password) instead.

Returns: `Any`

##### `*args`

Data type: `Any`



### resources_deep_merge

Type: Ruby 4.x API

DEPRECATED. Use the namespaced function [`extlib::resources_deep_merge`](#extlibresources_deep_merge) instead.

#### `resources_deep_merge(Any *$args)`

DEPRECATED. Use the namespaced function [`extlib::resources_deep_merge`](#extlibresources_deep_merge) instead.

Returns: `Any`

##### `*args`

Data type: `Any`



25 changes: 25 additions & 0 deletions functions/dir_split.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# @summary Splits the given directory or directories into individual paths.
#
# Use this function when you need to split a absolute path into multiple absolute paths
# that all descend from the given path.
#
# @param dirs [Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]]] - either an absolute path or a array of absolute paths.
# @return [Array[String]] - an array of absolute paths after being cut into individual paths.
# @example calling the function
# extlib::dir_split('/opt/puppetlabs') => ['/opt', '/opt/puppetlabs']
function extlib::dir_split(Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]] $dirs) >> Array[String] {
$sep = extlib::file_separator()

$dirs_array = [$dirs].flatten.unique.map | Stdlib::Absolutepath $dir | {
$dir.split(shell_escape($sep)).reduce([]) |Array $acc, $value | {
$counter = $acc.length - 1
$acc_value = ($acc[$counter] =~ Undef) ? { true => '', false => $acc[$counter] }
unless empty($value) {
$acc + extlib::path_join([$acc_value, $value])
} else {
$acc
}
}
}
$dirs_array.flatten.unique
}
7 changes: 7 additions & 0 deletions functions/file_separator.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @summary Returns the os specific file path separator.
# @return [String] - The os specific path separator.
# @example Example of how to use
# extlib::file_separator() => '/'
function extlib::file_separator() >> String {
($::facts['kernel'] == 'windows' ) ? { true => "\\", false => '/' }
}
20 changes: 20 additions & 0 deletions functions/mkdir_p.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# @summary Like the unix command mkdir_p except with puppet code.
# This creates file resources for all directories and utilizes the dir_split() function
# to get a list of all the descendant directories. You will have no control over any other parameters
# for the file resource. If you wish to control the file resources you can use the dir_split() function
# and get an array of directories for use in your own code. Please note this does not use an exec resource.
#
# @param dirs [Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]]] - the path(s) to create
# @return [Array[Stdlib::Absolutepath]]
# @example How to use
# extlib::mkdir_p('/opt/puppetlabs/bin') => ['/opt', '/opt/puppetlabs', '/opt/puppetlabs/bin']
# @note splits the given directories into paths that are then created using file resources
# @note if you wish to create the directories manually you can use the extlib::dir_split() function in the same manner
function extlib::mkdir_p(Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]] $dirs) >> Array[Stdlib::Absolutepath] {
$dirs_array = extlib::dir_split($dirs)
@file{$dirs_array:
ensure => directory,
}
realize(File[$dirs_array])
$dirs_array
}
27 changes: 27 additions & 0 deletions functions/path_join.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# @summary Take one or more paths and join them together using the os specific separator.
# Because in how windows uses a different separator this function
# will format a windows path into a equilivent unix like path. This type of unix like
# path will work on windows.
#
# @param dirs [Array[String]] - Joins two or more directories by file separator.
# @return [Stdlib::Absolutepath] - The joined path
# @example extlib::path_join('/tmp', 'test', 'libs') returns '/tmp/test/libs'
# @example extlib::path_join('c:', 'test', 'libs') returns '/c/test/libs'
function extlib::path_join(Array[String] $dirs) >> Stdlib::Absolutepath {
$unix_sep = '/'
$sep_regex = /\/|\\/
$first_value = $dirs[0]
# when first value is absolute path, append all other elements
# by breaking the path into pieces first, then joining
if $first_value =~ Stdlib::Absolutepath {
$fixed_dirs = $first_value.split($sep_regex) + $dirs.delete($first_value)
} else {
$fixed_dirs = $dirs
}
$no_empty_dirs = $fixed_dirs.filter |$dir| { !empty($dir) }
$dirs_without_sep = $no_empty_dirs.map |String $dir | {
# remove : and file separator
$dir.regsubst($sep_regex, '').regsubst(':', '')
}
join([$unix_sep,$dirs_without_sep.join($unix_sep)])
}
Loading

0 comments on commit 1c9a981

Please sign in to comment.