Commit 17b5226131b6903ab870132db517253a12cab6ff
1 parent
19a12f72d8
Exists in
master
Ajout d'un script d'application de config
Showing 1 changed file with 123 additions and 0 deletions Side-by-side Diff
wize.rb
| 1 | +#!/usr/bin/env ruby | |
| 2 | + | |
| 3 | +require 'optparse' | |
| 4 | + | |
| 5 | +# Variables globales $version, $auteur et $date | |
| 6 | +$version = "0.1" | |
| 7 | +$auteur = "William Daniau" | |
| 8 | +$date = "2019 10 11" | |
| 9 | + | |
| 10 | +# Parse arguments class | |
| 11 | +class ParseOptions | |
| 12 | + def self.parse(args) | |
| 13 | + options = Hash.new | |
| 14 | + | |
| 15 | + opt_parser = OptionParser.new do |opts| | |
| 16 | + opts.banner = "\nUtilisation : wize.rb [options]" | |
| 17 | + opts.separator "" | |
| 18 | + whatIDo = <<-AFP | |
| 19 | + | |
| 20 | + liste ou applique une configuartion de template wize pour zim | |
| 21 | + | |
| 22 | + AFP | |
| 23 | + | |
| 24 | + opts.separator whatIDo | |
| 25 | + | |
| 26 | + opts.on("-l", | |
| 27 | + "--list", | |
| 28 | + "Liste les configurations disponibles") do |l| | |
| 29 | + options[:list] = l | |
| 30 | + end | |
| 31 | + | |
| 32 | + opts.on("-a", | |
| 33 | + "--apply CONF", | |
| 34 | + "applique la config CONF") do |conf| | |
| 35 | + options[:conf] = conf | |
| 36 | + end | |
| 37 | + | |
| 38 | + opts.separator "" | |
| 39 | + opts.separator " Options standard:" | |
| 40 | + opts.on_tail("-h", "--help", "Affiche ce message") do | |
| 41 | + puts opts | |
| 42 | + puts "" | |
| 43 | + exit | |
| 44 | + end | |
| 45 | + opts.on_tail("-v", "--version", "Affiche la version") do | |
| 46 | + puts "Version : " + $version | |
| 47 | + puts "Auteur : " + $auteur | |
| 48 | + puts "Date : " + $date | |
| 49 | + puts "" | |
| 50 | + exit | |
| 51 | + end | |
| 52 | + end | |
| 53 | + | |
| 54 | + opt_parser.parse!(args) | |
| 55 | + options | |
| 56 | + end | |
| 57 | +end | |
| 58 | + | |
| 59 | +def listConfigs | |
| 60 | + a = `ls`.split(/\n/) | |
| 61 | + names = [] | |
| 62 | + r = /^common-(.+)$/ | |
| 63 | + a.each { |f| | |
| 64 | + if f =~ r | |
| 65 | + names.push($1) | |
| 66 | + end | |
| 67 | + } | |
| 68 | + names | |
| 69 | +end | |
| 70 | + | |
| 71 | +def applyConfig(name) | |
| 72 | + list = ["Wize", "Wize_with_index", "Wize_with_index_sod"] | |
| 73 | + list.each { |l| | |
| 74 | + com = "rm -f #{l}/common-spec" | |
| 75 | + system(com) | |
| 76 | + com = "ln -s ../common-#{name} #{l}/common-spec" | |
| 77 | + system(com) | |
| 78 | + } | |
| 79 | + system("rm -f print-common") | |
| 80 | + system("ln -s common-#{name} print-common") | |
| 81 | +end | |
| 82 | + | |
| 83 | +def configInList?(name, names) | |
| 84 | + names.include? name | |
| 85 | +end | |
| 86 | + | |
| 87 | +# Parse arguments using the class and get results in a hash | |
| 88 | +options = ParseOptions.parse(ARGV) | |
| 89 | +if options.length == 0 | |
| 90 | + puts "Utiliser -h pour l'aide" | |
| 91 | + exit | |
| 92 | +end | |
| 93 | + | |
| 94 | +# Directory definitions | |
| 95 | +zimShareDir = "#{ENV['HOME']}/.local/share/zim" | |
| 96 | +zimHtmlTemplateDir = "#{zimShareDir}/templates/html" | |
| 97 | +zimConfigDir = "#{ENV['HOME']}/.config/zim" | |
| 98 | + | |
| 99 | +# go into template dir | |
| 100 | +Dir.chdir(zimHtmlTemplateDir) | |
| 101 | + | |
| 102 | +# get existing configs | |
| 103 | +configs = listConfigs | |
| 104 | + | |
| 105 | +# List | |
| 106 | +if options[:list] | |
| 107 | + configs.each { |c| | |
| 108 | + puts c | |
| 109 | + } | |
| 110 | + exit | |
| 111 | +end | |
| 112 | + | |
| 113 | +# Apply | |
| 114 | +if options[:conf] | |
| 115 | + conf = options[:conf] | |
| 116 | + # is it a valid config? | |
| 117 | + if !configInList?(conf, configs) | |
| 118 | + puts "#{conf} n'est pas une configuration valide" | |
| 119 | + exit | |
| 120 | + end | |
| 121 | + # we have a valid config | |
| 122 | + applyConfig(conf) | |
| 123 | +end |