From 6257f2906a697dc0ea41f57dfb2e86714abd362d Mon Sep 17 00:00:00 2001 From: Mikhail Konyakhin Date: Thu, 21 Jul 2016 17:08:23 +0300 Subject: [PATCH 1/2] Add validation fg, bg colors and styles (function validate_colors); Add feature git-prompt; Add feature set fg, bg colors and styles of present working directory; Rewrite templates/colorprompt.erb. --- README.md | 126 +++++++++---- .../parser/functions/validate_colors.rb | 50 +++++ manifests/init.pp | 176 ++++++++++++++++-- manifests/params.pp | 68 +++++-- manifests/validate_colors.pp | 32 ++++ templates/colorprompt.erb | 83 +++++---- templates/git-prompt.erb | 13 ++ 7 files changed, 446 insertions(+), 102 deletions(-) create mode 100644 lib/puppet/parser/functions/validate_colors.rb create mode 100644 manifests/validate_colors.pp create mode 100644 templates/git-prompt.erb diff --git a/README.md b/README.md index 3d800ba..fcd0747 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,23 @@ -#colorprompt +# colorprompt -##Overview +## Overview The colorprompt module. Colors your bash prompt. -##Module Description +## Module Description The colorprompt module creates /etc/profile.d/colorprompt.sh, which sets a colored prompt. Different colors can be set for (all and specific) users, server name and environment tag. -##Usage +## Usage All interaction with the colorprompt module can be done through the main colorprompt class. -###I just want a colored prompt, what's the minimum I need? +### I just want a colored prompt, what's the minimum I need? ```puppet include 'colorprompt' ``` -###I want a prompt for my production server with customised colors. +### I want a prompt for my production server with customised colors. ```puppet class { 'colorprompt': @@ -31,79 +31,131 @@ class { 'colorprompt': } ``` -###What does the example above look like? +### What does the example above look like? ![prompt](https://raw.githubusercontent.com/sgnl05/sgnl05-colorprompt/master/example.png) -###Great! What colors are available? - -* black -* red -* green -* yellow -* blue -* magenta -* cyan -* white - -A background color can also be defined by using `bg_(color)`. Foreground and background colors can be combined by using arrays instead of strings (see `env_color` in example above). - -##Reference - -###Classes - -####Public Classes +### Great! What colors are available? + +#### Default colors + +* Foreground colors (`$::colorprompt::params::fg_colors`): + * black + * red + * green + * yellow + * blue + * magenta + * cyan + * white + * empty +* Background colors (`$::colorprompt::params::bg_colors`): + * bg_black + * bg_red + * bg_green + * bg_yellow + * bg_blue + * bg_magenta + * bg_cyan + * bg_white + * empty +* Styles (`$::colorprompt::params::styles`): + * bright + * faint + * underline + * blink + * empty + +You can set your available settings in hashes. +A background color can also be defined by using bg_(color).Foreground and background colors can be combined by using arrays instead of strings (see env_color in example above). + +## Reference + +### Classes + +#### Public Classes * colorprompt: Main and only class. -###Parameters +### Parameters -####`ensure` +#### `ensure` String. Ensure if file /etc/profile.d/colorprompt.sh is present or absent. Defaults to present. -####`default_usercolor` +#### `default_usercolor` String or array. Sets the color for all users. Specific user colors can be overrided by 'custom_usercolors'. Defaults to 'cyan'. -####`custom_usercolors` +#### `custom_usercolors` Hash. Sets the color for specific users. Example: custom_usercolors => { 'apache' => 'blue', 'tomcat' => 'yellow' } Default is { 'root' => 'magenta' } -####`server_color` +#### `server_color` String or array. Sets the color for the server name. Defaults to unset. -####`env_name` +#### `pwd_color` + +String or array. Sets the color for pwd. +Defaults to unset. + +#### `env_name` String: Names an environment tag. Examples: 'PROD', 'QA', 'TEST', 'DEV'. Defaults to unset. -####`env_color` +#### `env_color` String or array. Sets the color for of the environment tag. Defaults to unset -####`prompt` +#### `git_prompt` + +Boolean. Enable or disable [git_prompt](https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh "git_promt on github"). +Default is false + +#### `prompt` String. Sets the final PS1 variable. This is an advanced setting, and should probably be left untouched unless you know what you're doing. :) Default varies with osfamily. -####`modify_skel` +#### `modify_skel` Boolean. Comments out PS1 variables in /etc/skel/.bashrc Default varies with osfamily. -####`modify_root` +#### `modify_root` Boolean. Comments out PS1 variables in /root/.bashrc Default varies with osfamily -##Limitations +#### `bash_completion_package` +String. Package name bash-completion (install only if git_prompt equal true) +Defaults to 'bash-completion' + +#### `git_package` +String. Package name git (install only if git_prompt equal true) +Defaults to 'git' + +#### `fg_colors` +Hash. Hash of foreground colors. +Default: value of `$::colorprompt::params::fg_colors`. + +#### `bg_colors` +Hash. Hash of background colors. +Default: value of `$::colorprompt::params::bg_colors.` + +#### `styles` +Hash. Hash of styles. +Default: value of `$::colorprompt::params::styles`. + + +## Limitations This module has been tested against Puppet 3.0 and higher. @@ -117,6 +169,6 @@ Ubuntu and Debian need modification to existing user ~/.bashrc files (comment ou ## Development -###Contributing +### Contributing Please use the issue tracker (https://github.com/sgnl05/sgnl05-colorprompt/issues) for any type of contribution. diff --git a/lib/puppet/parser/functions/validate_colors.rb b/lib/puppet/parser/functions/validate_colors.rb new file mode 100644 index 0000000..b357a01 --- /dev/null +++ b/lib/puppet/parser/functions/validate_colors.rb @@ -0,0 +1,50 @@ +# +# validate_colors.rb +# + +module Puppet::Parser::Functions + newfunction(:validate_colors, :type => :rvalue, :doc => <<-EOS + This function validate colors for user console. + EOS + ) do |arguments| + + raise(Puppet::ParseError, "validate_colors(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + colors = arguments[0] + error_message = arguments[1] + + fg_colors_keys = lookupvar('::colorprompt::fg_colors').keys + bg_colors_keys = lookupvar('::colorprompt::bg_colors').keys + styles_keys = lookupvar('::colorprompt::styles').keys + + case colors + when String + unless fg_colors_keys.include?(colors) + raise(Puppet::ParseError, "validate_colors(): #{error_message} '#{colors}' not found in array '#{fg_colors_keys}'!") + end + when Array + if colors.size == 0 or colors.size > 3 + raise(Puppet::ParseError, 'validate_colors(): Size array of colors must be not equal 0 and more 3!') + else + unless fg_colors_keys.include?(colors[0]) + raise(Puppet::ParseError, "validate_colors(): #{error_message} '#{colors[0]}' not found in array '#{fg_colors_keys}'!") + end + if colors[1] + unless bg_colors_keys.include?(colors[1]) + raise(Puppet::ParseError, "validate_colors(): #{error_message} '#{colors[1]}' not found in array '#{bg_colors_keys}'!") + end + end + if colors[2] + unless styles_keys.include?(colors[2]) + raise(Puppet::ParseError, "validate_colors(): #{error_message} '#{colors[2]}' not found in array '#{styles_keys}'!") + end + end + end + else + raise(Puppet::ParseError, 'validate_colors(): Requires array or string in first argumnet to work with!') + end + end +end + +# vim: set ts=2 sw=2 et : diff --git a/manifests/init.pp b/manifests/init.pp index 6c56e69..cce80e9 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -25,26 +25,36 @@ # Default: { 'root' => 'magenta' } # # [*server_color*] -# Ensure if present or absent. +# Sets the color for the server name. +# Type: string or array +# Default: undef +# +# [*pwd_color*] +# Sets the color for pwd. # Type: string or array # Default: undef # # [*env_name*] -# Ensure if present or absent. +# Sets the name of environment. # Type: string # Default: undef # # [*env_color*] -# Ensure if present or absent. +# Sets the color for environment. # Type: string or array # Default: undef # +# [*git_prompt*] +# Enable or disable git_prompt. +# Type: boolean +# Default: false +# # [*prompt*] # Sets the final $PS1 variable. Use ${env}, ${userColor} and ${serverColor} # Type: string # Default: -# '${env}[${userColor}\u\[\e[0m\]@${serverColor}\h\[\e[0m\] \w]\\$ ' on RedHat -# '${env}[${userColor}\u\[\e[0m\]@${serverColor}\h\[\e[0m\] \W]\\$ ' on Debian +# '${env}[${userColor}\u\[\e[0m\]@${serverColor}\h\[\e[0m\] ${pwdColor}\W\[\e[0m\]]\\$${git_prompt} ' on RedHat +# '${env}[${userColor}\u\[\e[0m\]@${serverColor}\h\[\e[0m\] ${pwdColor}\w\[\e[0m\]]\\$${git_prompt} ' on Debian # # [*modify_skel*] # Comments out PS1 variables in /etc/skel/.bashrc on Debian distributions @@ -56,6 +66,31 @@ # Type: boolean # Default: true on Debian, false on RedHat # +# [*bash_completion_package*] +# Package name bash-completion (install only if git_prompt equal true). +# Type: string +# Default: bash-completion +# +# [*git_package*] +# Package name git (install only if git_prompt equal true). +# Type: string +# Default: git +# +# [*fg_colors*] +# Hash of foreground colors { name => code } +# Type: Hash +# Default: value of $::colorprompt::params::fg_colors. +# +# [*bg_colors*] +# Hash of background colors { name => code } +# Type: Hash +# Default: value of $::colorprompt::params::bg_colors. +# +# [*styles*] +# Hash of styles { name => code } +# Type: Hash +# Default: value of $::colorprompt::params::styles. +# # === Examples # # class { 'colorprompt': @@ -63,10 +98,12 @@ # env_name => 'PROD', # env_color => ['white', 'bg_red'], # server_color => 'red', +# pwd_color => [ 'blue', 'empty', 'bright'], # default_usercolor => 'cyan', # custom_usercolors => { # 'root' => 'magenta', # }, +# git_prompt => true, # } # # === Authors @@ -78,16 +115,24 @@ # Copyright 2014-2015 Gjermund Jensvoll # class colorprompt ( - $ensure = $colorprompt::params::ensure, - $path = $colorprompt::params::path, - $default_usercolor = $colorprompt::params::default_usercolor, - $custom_usercolors = $colorprompt::params::custom_usercolors, - $server_color = $colorprompt::params::server_color, - $env_name = $colorprompt::params::env_name, - $env_color = $colorprompt::params::env_color, - $prompt = $colorprompt::params::prompt, - $modify_skel = $colorprompt::params::modify_skel, - $modify_root = $colorprompt::params::modify_root, + $ensure = $colorprompt::params::ensure, + $path = $colorprompt::params::path, + $default_usercolor = $colorprompt::params::default_usercolor, + $custom_usercolors = $colorprompt::params::custom_usercolors, + $server_color = $colorprompt::params::server_color, + $pwd_color = $colorprompt::params::pwd_color, + $env_name = $colorprompt::params::env_name, + $env_color = $colorprompt::params::env_color, + $git_prompt = $colorprompt::params::git_prompt, + $git_prompt_path = $colorprompt::params::git_prompt_path, + $prompt = $colorprompt::params::prompt, + $modify_skel = $colorprompt::params::modify_skel, + $modify_root = $colorprompt::params::modify_root, + $bash_completion_package = $colorprompt::params::bash_completion_package, + $git_package = $colorprompt::params::git_package, + $fg_colors = $colorprompt::params::fg_colors, + $bg_colors = $colorprompt::params::bg_colors, + $styles = $colorprompt::params::styles, ) inherits colorprompt::params { validate_re($ensure, '^(present|absent)$', @@ -95,17 +140,87 @@ validate_absolute_path( $path, + $git_prompt_path, ) validate_string( - $prompt + $prompt, + $bash_completion_package, + $git_package, ) validate_bool( + $git_prompt, $modify_skel, - $modify_root + $modify_root, + ) + + validate_hash( + $custom_usercolors, + $fg_colors, + $bg_colors, + $styles, ) + if empty($custom_usercolors) { + fail('Value of parameter $custom_usercolors must be not empty!') + } else { + if ( versioncmp($::puppetversion, '4.0.0') >= 0 ) { + $custom_usercolors.each | $user, $colors | { + validate_colors($colors, "Values of \$custom_usercolors not correct for user '${user}'! Value") + } + } + } + + unless is_string($default_usercolor) or is_array($default_usercolor) { + fail('Value of parameter $default_usercolor must be string or array!') + } else { + validate_colors($default_usercolor, "Values of \$default_usercolor not correct! Value") + } + + if empty($default_usercolor) { + fail('Value of parameter $default_usercolor must be not empty!') + } + + if $server_color { + if empty($server_color) { + fail('Value of parameter $server_color must be not empty!') + } + unless is_string($server_color) or is_array($server_color) { + fail('Value of parameter $server_color must be string or array!') + } else { + validate_colors($server_color, "Values of \$server_color not correct! Value") + } + } + + if $pwd_color { + if empty($pwd_color) { + fail('Value of parameter $pwd_color must be not empty!') + } + unless is_string($pwd_color) or is_array($pwd_color) { + fail('Value of parameter $pwd_color must be string or array!') + } else { + validate_colors($pwd_color, "Values of \$pwd_color not correct! Value") + } + } + + if $env_name { + validate_string($env_name) + if empty($env_name) { + fail('Value of parameter $env_name must be not empty!') + } + if $env_color { + if empty($env_color) { + fail('Value of parameter $env_color must be not empty!') + } + unless is_string($env_color) or is_array($env_color) { + fail('Value of parameter $env_color must be string or array!') + } else { + validate_colors($env_color, "Values of \$env_color not correct! Value") + } + } + } + file { 'colorprompt.sh': ensure => $ensure, path => $path, @@ -115,6 +230,33 @@ content => template('colorprompt/colorprompt.erb'), } + if $git_prompt { + unless defined(Package[$bash_completion_package]) { + package { $bash_completion_package: + ensure => present, + } + } + unless defined(Package[$git_package]) { + package { $git_package: + ensure => present, + } + } + } + file { "${::colorprompt::params::bash_completion_dir}/git-prompt": + ensure => $git_prompt ? { + true => present, + default => absent, + }, + owner => 'root', + group => 'root', + mode => '0644', + content => template('colorprompt/git-prompt.erb'), + require => [ + Package[$bash_completion_package], + Package[$git_package], + ], + } + if $modify_skel { exec { 'modify_skel': command => 'sed -i \'/^if \[ "\$color_prompt" = yes \]; then/,/fi/s/^/#/\' /etc/skel/.bashrc', diff --git a/manifests/params.pp b/manifests/params.pp index 0dcdaf1..edbd73d 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -1,26 +1,33 @@ # Default parameters class colorprompt::params { - $ensure = present - $path = '/etc/profile.d/colorprompt.sh' - $default_usercolor = 'cyan' - $custom_usercolors = { 'root' => 'magenta' } - $server_color = undef - $env_name = undef - $env_color = undef + $ensure = present + $path = '/etc/profile.d/colorprompt.sh' + $default_usercolor = 'cyan' + $custom_usercolors = { 'root' => 'magenta' } + $server_color = undef + $pwd_color = undef + $env_name = undef + $env_color = undef + $git_prompt = false + $bash_completion_dir = '/etc/bash_completion.d' + $bash_completion_package = 'bash-completion' + $git_package = 'git' case $::osfamily { 'RedHat': { - $prompt = '${env}[${userColor}\u\[\e[0m\]@${serverColor}\h\[\e[0m\] \W]\\$ ' - $modify_skel = false - $modify_root = false + $git_prompt_path = '/usr/share/git-core/contrib/completion/git-prompt.sh' + $prompt = '${env}[${userColor}\u\[\e[0m\]@${serverColor}\h\[\e[0m\] ${pwdColor}\W\[\e[0m\]]\\$${git_prompt} ' + $modify_skel = false + $modify_root = false } 'Debian': { - $prompt = '${env}[${userColor}\u\[\e[0m\]@${serverColor}\h\[\e[0m\] \w]\\$ ' - $modify_skel = true - $modify_root = true + $git_prompt_path = '/usr/lib/git-core/git-sh-prompt' + $prompt = '${env}[${userColor}\u\[\e[0m\]@${serverColor}\h\[\e[0m\] ${pwdColor}\w\[\e[0m\]]\\$${git_prompt} ' + $modify_skel = true + $modify_root = true } default: { @@ -29,4 +36,39 @@ } + # first list colors + $fg_colors = { + black => ';30', + red => ';31', + green => ';32', + yellow => ';33', + blue => ';34', + magenta => ';35', + cyan => ';36', + white => ';37', + empty => '', + } + + # second list colors + $bg_colors = { + bg_black => ';40', + bg_red => ';41', + bg_green => ';42', + bg_yellow => ';43', + bg_blue => ';44', + bg_magenta => ';45', + bg_cyan => ';46', + bg_white => ';47', + empty => '', + } + + # third list colors + $styles = { + bright => ';1', + faint => ';2', + underline => ';4', + blink => ';5', + empty => '', + } + } diff --git a/manifests/validate_colors.pp b/manifests/validate_colors.pp new file mode 100644 index 0000000..4a47113 --- /dev/null +++ b/manifests/validate_colors.pp @@ -0,0 +1,32 @@ +# == Define: validate_colors +# +# Define for validate color values in array or string. +# +define colorprompt::validate_colors ( + $colors = $title, +) { + if is_string($colors) { + unless member($fg_colors_keys, $colors) { + fail("Color '${colors}' not found value of hash \$fg_colors in class colorprompt::params!") + } + } elsif is_array($colors) { + if size($colors) == 4 { + fail("Array of colors '${colors}' size must be 1,2 or 3 elements!") + } + unless member($fg_colors_keys, $colors[0]) { + fail("Color '${colors[0]}' not found value of hash \$fg_colors in class colorprompt::params!") + } + if $colors[1] { + unless member($bg_colors_keys, $colors[1]) { + fail("Color '${colors[1]}' not found value of hash \$bg_colors in class colorprompt::params!") + } + } + if $colors[2] { + unless member($thid_colors_keys, $colors[1]) { + fail("Color '${colors[1]}' not found value of hash \$styles in class colorprompt::params!") + } + } + } else { + fail("Colors '${colors}' array or stirng!") + } +} diff --git a/templates/colorprompt.erb b/templates/colorprompt.erb index 597611a..da723a5 100644 --- a/templates/colorprompt.erb +++ b/templates/colorprompt.erb @@ -1,54 +1,67 @@ # Managed by Puppet -if [ -z "$PS1" ]; then +if [ -z "${PS1}" ]; then # Probably non-interactive shell.. return 0 fi -black=";30" -red=";31" -green=";32" -yellow=";33" -blue=";34" -magenta=";35" -cyan=";36" -white=";37" - -bg_black=";40" -bg_red=";41" -bg_green=";42" -bg_yellow=";43" -bg_blue=";44" -bg_magenta=";45" -bg_cyan=";46" -bg_white=";47" - -bright=";1" -faint=";2" -underline=";4" -blink=";5" - - -<%- if @custom_usercolors.is_a?(Hash) -%><%- @custom_usercolors.each do |user, colors| -%> -if [ "$USER" = "<%= user %>" ]; then +# First list colors +<% @fg_colors.each do |name, code| -%> +<%= name %>='<%= code %>' +<% end -%> + +# Second list colors +<% @bg_colors.each do |name, code| -%> +<%= name %>='<%= code %>' +<% end -%> + +# Third list colors +<% @styles.each do |name, code| -%> +<%= name %>='<%= code %>' +<% end -%> + +# User colors settings +case "${USER}" in +<% @custom_usercolors.each do |user, colors| -%> + "<%= user %>") userColor="\[\e[0<%- if colors.is_a?(Array) -%><% colors.each do |color| -%>${<%= color %>}<% end -%><%- else -%>${<%= colors %>}<%- end -%>m\]" -el<%- end -%>se + ;; +<% end -%> + *) userColor="\[\e[0<%- if @default_usercolor.is_a?(Array) -%><% @default_usercolor.each do |color| -%>${<%= color %>}<% end -%><%- else -%>${<%= @default_usercolor %>}<%- end -%>m\]" -fi -<% else -%>userColor="\[\e[0<%- if @default_usercolor.is_a?(Array) -%><% @default_usercolor.each do |color| -%>${<%= color %>}<% end -%><%- else -%>${<%= @default_usercolor %>}<%- end -%>m\]"<%- end -%> + ;; +esac -<% if @server_color and ! @server_color.empty? %> +<% if @server_color -%> +# Server colors settings serverColor="\[\e[0<%- if @server_color.is_a?(Array) -%><%- @server_color.each do |color| -%>${<%= color %>}<%- end -%><%- else -%>${<%= @server_color %>}<%- end -%>m\]" + +<% end -%> +<% if @pwd_color -%> +# PWD colors settings +pwdColor="\[\e[0<%- if @pwd_color.is_a?(Array) -%><%- @pwd_color.each do |color| -%>${<%= color %>}<%- end -%><%- else -%>${<%= @pwd_color %>}<%- end -%>m\]" + <% end -%> -<% if @env_name and ! @env_name.empty? %> +<% if @env_name -%> +# Environment colors settings env="\[\e[0<%- if @env_color.is_a?(Array) -%><% @env_color.each do |color| -%>${<%= color %>}<%- end -%><%- else -%>${<%= @env_color %>}<%- end -%>m\]<%= @env_name %>\[\e[0m\] " -<% end %> + +<% end -%> +<% if @git_prompt -%> +# Enable git_promt +git_prompt='`__git_ps1`' + +<% else -%> +# Disable git_promt +git_prompt='' + +<% end -%> # Set prompt -if [ $TERM != "dumb" ]; then +if [ "${TERM}" != "dumb" ]; then PS1="<%= @prompt %>" fi # Remove variables from the environment -unset userColor serverColor blue red green yellow blue magenta cyan white +unset userColor serverColor blue red green yellow blue magenta cyan white empty unset bg_black bg_red bg_green bg_yellow bg_blue bg_magenta bg_cyan bg_white unset bright faint underline blink diff --git a/templates/git-prompt.erb b/templates/git-prompt.erb new file mode 100644 index 0000000..115c2fa --- /dev/null +++ b/templates/git-prompt.erb @@ -0,0 +1,13 @@ +# Managed by Puppet! +# +# In git versions < 1.7.12, this shell library was part of the +# git completion script. +# +# Some users rely on the __git_ps1 function becoming available +# when bash-completion is loaded. Continue to load this library +# at bash-completion startup for now, to ease the transition to a +# world order where the prompt function is requested separately. +# +if [[ -e <%= @git_prompt_path %> ]]; then + . <%= @git_prompt_path %> +fi From 7f75cbeab4952469b1d5ed68a007fd26091b1269 Mon Sep 17 00:00:00 2001 From: Mikhail Konyakhin Date: Thu, 12 Oct 2017 12:01:25 +0300 Subject: [PATCH 2/2] Fix code. --- manifests/init.pp | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/manifests/init.pp b/manifests/init.pp index cce80e9..c8763b4 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -165,10 +165,8 @@ if empty($custom_usercolors) { fail('Value of parameter $custom_usercolors must be not empty!') } else { - if ( versioncmp($::puppetversion, '4.0.0') >= 0 ) { - $custom_usercolors.each | $user, $colors | { - validate_colors($colors, "Values of \$custom_usercolors not correct for user '${user}'! Value") - } + $custom_usercolors.each | $user, $colors | { + validate_colors($colors, "Values of \$custom_usercolors not correct for user '${user}'! Value") } } @@ -231,16 +229,8 @@ } if $git_prompt { - unless defined(Package[$bash_completion_package]) { - package { $bash_completion_package: - ensure => present, - } - } - unless defined(Package[$git_package]) { - package { $git_package: - ensure => present, - } - } + $packages = [$bash_completion_package, $git_package] + ensure_packages($packages) } file { "${::colorprompt::params::bash_completion_dir}/git-prompt": ensure => $git_prompt ? {