Class: Ki::UserPrefCommand

Inherits:
Object show all
Defined in:
lib/cmd/user_pref_cmd.rb

Overview

Sets user specific configurations

See Also:

Instance Method Summary collapse

Instance Method Details

#execute(ctx, args) ⇒ 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
# File 'lib/cmd/user_pref_cmd.rb', line 35

def execute(ctx, args)
  user_pref = ctx.user_pref
  pref = args.delete_at(0)
  if pref == "prefix"
    arr = user_pref.prefixes
    str = "Prefixes"
  elsif pref == "use"
    arr = user_pref.uses
    str = "Use"
  elsif pref == "require"
    arr = user_pref.requires
    str = "Require"
  elsif pref.nil?
    puts "User preferences:"
    user_pref.cached_data.each_pair do |key, values|
      if values && values.size > 0
        puts "#{key}: " + values.join(", ")
      end
    end
  else
    raise "not supported: " + pref
  end
  if arr && str
    args = opts(arr).parse(args)
    if args.size > 0
      if args[0] == "+"
        args.delete_at(0)
        arr.concat(args)
      elsif args[0] == "-"
        args.delete_at(0)
        args.each do |a|
          arr.delete(a)
        end
      else
        arr.clear
        arr.concat(args)
      end
      arr.uniq!
      user_pref.save
    end
    puts "#{str}: " + arr.join(", ")
  end
end

#helpObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cmd/user_pref_cmd.rb', line 92

def help
<<EOF
#{summary}
Syntax: #{shell_command} prefix|use parameters...

### Examples for command prefixes:
#{shell_command} prefix
- shows command prefixes, when a "ki command" is executed ki looks for the command with all prefix combinations
#{shell_command} prefix version package
- sets two command prefixes, looks for "command", "version-command" and "package-command"
#{shell_command} prefix + foo
- adds one command prefix to existing ones, looks for "command", "version-command", "package-command", "foo-command"
#{shell_command} prefix - package foo
- removes two command prefixes from list
#{shell_command} prefix -c
- clears command prefix list

### Examples for default script loading:
#{shell_command} use
- shows list of automatically loading scripts. when ki starts up, it looks for all defined versions and loads all files tagged with "ki"
#{shell_command} use ki/http ki/ftp/123:ki-extra
- scripts are loaded from two different version. ki/http uses latest available version and files tagged with "ki", ki/ftp uses specific version and files tagged with "ki-extra"
#{shell_command} use + ki/scp
- adds one more script package version
#{shell_command} use - ki/scp ki/ftp/123:ki-extra
- removes two configurations
#{shell_command} use -c
- clear use list

### Examples for default Ruby file requiring:
#{shell_command} require
#{shell_command} require hooves/default
#{shell_command} require + hooves/default
#{shell_command} require - hooves/default
#{shell_command} require -c
EOF
end

#opts(arr) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/cmd/user_pref_cmd.rb', line 79

def opts(arr)
  OptionParser.new do |opts|
    opts.banner = ""
    opts.on("-c", "--clear", "Clear existing preferences values for specified value") do |v|
      arr.clear
    end
  end
end

#summaryObject



88
89
90
# File 'lib/cmd/user_pref_cmd.rb', line 88

def summary
  "Sets user preferences"
end