Class: S7::S7Cli

Inherits:
Object
  • Object
show all
Includes:
GetText
Defined in:
lib/s7/s7cli.rb,
lib/s7/s7cli/option.rb,
lib/s7/s7cli/command.rb,
lib/s7/s7cli/entry_command.rb,
lib/s7/s7cli/attribute_command.rb,
lib/s7/s7cli/entry_collection_command.rb

Defined Under Namespace

Modules: EntryCommandUtils Classes: ArrayOption, AttributeCopyCommand, BoolOption, Command, EntryAddCommand, EntryCollectionImportCommand, EntryDeleteCommand, EntryEditCommand, EntrySearchCommand, EntryShowCommand, HelpCommand, IntOption, LockCommand, Option, QuitCommand, SaveCommand, StringOption

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GetText

#N_, #_, bindtextdomain, included

Constructor Details

#initializeS7Cli

Returns a new instance of S7Cli.



78
79
80
# File 'lib/s7/s7cli.rb', line 78

def initialize
  @world = World.new
end

Instance Attribute Details

#worldObject

World クラスのインスタンス。



76
77
78
# File 'lib/s7/s7cli.rb', line 76

def world
  @world
end

Class Method Details

.input_boolean(prompt = "", default = false) ⇒ Object

prompt に指定した文字列を表示後、ユーザからの入力を得る。 それがyes、y、true、t、okのいずれかであれば true を返し、 そうでなければ false を返す。大小文字は区別しない。 ^D などにより何も入力しない場合、defualt で指定した真偽値を返す。



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/s7/s7cli.rb', line 58

def self.input_boolean(prompt = "", default = false)
  if input = Readline.readline(prompt, false)
    input.strip!
    if input.empty?
      return default
    end
    input.force_encoding("utf-8")
    if /\Ay|yes|t|true|ok\z/i =~ input
      return true
    else
      return false
    end
  else
    return default
  end
end

.input_string(prompt = "", add_history = true) ⇒ Object

prompt に指定した文字列を表示後、Readline ライブラリを利用してユー ザからの入力を得る。



29
30
31
32
33
# File 'lib/s7/s7cli.rb', line 29

def self.input_string(prompt = "", add_history = true)
  input = Readline.readline(prompt, add_history)
  input.force_encoding("utf-8") if input
  return input
end

.input_string_without_echo(prompt = "") ⇒ Object

prompt に指定した文字列を表示後、エコーバックせずにユーザからの入 力を得る。パスフレーズの入力を想定。



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/s7/s7cli.rb', line 37

def self.input_string_without_echo(prompt = "")
  print(prompt) if prompt.length > 0
  STDOUT.flush
  Utils.execute("stty -echo")
  begin
    input = STDIN.gets
  ensure
    Utils.execute("stty echo")
    puts("")
  end
  if input
    input.chomp!
    input.force_encoding("utf-8")
  end
  return input
end

.run(argv) ⇒ Object

最初に実行するメソッド。



21
22
23
24
25
# File 'lib/s7/s7cli.rb', line 21

def self.run(argv)
  obj = S7Cli.new
  obj.parse_command_line_argument(argv)
  obj.run
end

Instance Method Details

#parse_command_line_argument(argv) ⇒ Object

コマンドラインオプションを解析する。



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

def parse_command_line_argument(argv)
  option_parser = OptionParser.new { |opts|
    opts.banner = _("usage: s7cli [options]")
    opts.separator("")
    opts.separator(_("options:"))
    opts.on("-d", "--base-dir=DIR",
            _("Specify the base directory. Default is '%s'.") % world.base_dir) do |dir|
      world.base_dir = dir
    end
    opts.on("-v", "--version", _("Show version.")) do
      puts(_("s7cli, version %s") % S7::VERSION)
      exit(0)
    end
    opts.on("-h", "--help",
            _("Show help and exit.")) do
      puts(opts)
      exit(0)
    end
  }
  option_parser.parse(argv)
end

#runObject

メインの処理を行う。



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/s7/s7cli.rb', line 106

def run
  if File.exist?(world.base_dir)
    if !File.directory?(world.base_dir)
      Utils::shred_string(master_key)
      raise InvalidPath.new(world.base_dir)
    end
    world.start_logging
    begin
      master_key =
        S7Cli.input_string_without_echo(_("Enter the master key for s7: "))
      if master_key.nil?
        exit(1)
      end
      world.load(S7File.read(world.secrets_path, master_key))
      world.master_key = master_key
    rescue InvalidPassphrase => e
      puts(e.message)
      retry
    end
  else
    master_key =
      S7Cli.input_string_without_echo(_("Enter the master key for s7: "))
    if master_key.nil?
      exit(1)
    end
    confirmation =
      S7Cli.input_string_without_echo(_("Comfirm the master key for s7: "))
    if master_key != confirmation
      Utils::shred_string(master_key)
      Utils::shred_string(confirmation) if !confirmation.nil?
      raise InvalidPassphrase
    end
    world.master_key = master_key
    FileUtils.mkdir_p(world.base_dir)
    FileUtils.chmod(0700, world.base_dir)
    world.start_logging
    world.logger.info(_("created base directory: %s") % world.base_dir)
    world.save(true)
  end
  world.lock
  begin
    path = File.join(world.base_dir, "s7cli")
    if File.file?(path)
      begin
        hash = YAML.load(S7File.read(path, world.master_key))
        hash["history"].each_line do |line|
          Readline::HISTORY.push(line.chomp)
        end
      rescue
        puts(_("could not read: %s") % path)
        raise
      end
    end
    loop do
      begin
        prompt = "%ss7> " % (world.changed ? "*" : "")
        input = S7Cli.input_string(prompt, true)
        if !input
          puts("")
          break
        end
        name, argv = *Command.split_name_and_argv(input)
        if !name
          next
        end
        command = Command.create_instance(name, world)
        if command
          if !command.run(argv)
            break
          end
        else
          raise ApplicationError, _("Unknown command: %s") % name
        end
      rescue Interrupt
        puts("")
        retry
      rescue ApplicationError
        puts($!)
      rescue => e
        puts(e.message)
        puts("----- back trace -----")
        e.backtrace.each do |line|
          puts("  #{line}")
        end
      end
    end
  ensure
    begin
      world.save
      begin
        path = File.join(world.base_dir, "s7cli")
        # TODO: history の上限を設ける。
        hash = {
          "history" => Readline::HISTORY.to_a.join("\n"),
        }
        S7File.write(path, world.master_key,
                     world.configuration.cipher_type, hash.to_yaml)
      rescue
        puts(_("could not write: %s") % path)
        puts($!)
      end
    ensure
      world.unlock
    end
  end
end