Module: Ssync::Helpers

Included in:
Command, Setup, Sync
Defined in:
lib/ssync/helpers.rb

Instance Method Summary collapse

Instance Method Details

#aquire_lock!Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ssync/helpers.rb', line 49

def aquire_lock!
  return unless lock_path
  # better way is to write out the pid ($$) and read it back in, to make sure it's the same
  e! "Found a lock at #{lock_path}, is another instance of Ssync running?" if File.exist?(lock_path)

  begin
    system "touch #{lock_path}" unless ssync_filename.empty?
    yield
  ensure
    system "rm -f #{lock_path}"
  end
end

#ask(config_item, question) ⇒ Object



19
20
21
22
23
# File 'lib/ssync/helpers.rb', line 19

def ask(config_item, question)
  print(question + " [#{config_item}]: ")
  a = $stdin.readline.chomp
  a.empty? ? config_item : a
end

#config_exists?(path = config_path) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/ssync/helpers.rb', line 25

def config_exists?(path = config_path)
  File.exist?(path)
end

#config_path(filename = nil) ⇒ Object



41
42
43
# File 'lib/ssync/helpers.rb', line 41

def config_path(filename = nil)
  "#{ssync_homedir}/#{filename || ssync_filename}.yml"
end

#create_homedir!Object



87
88
89
# File 'lib/ssync/helpers.rb', line 87

def create_homedir!
  `mkdir #{ssync_homedir}` unless File.exists?(ssync_homedir)
end

#default_config_pathObject



37
38
39
# File 'lib/ssync/helpers.rb', line 37

def default_config_path
  "#{ssync_homedir}/defaults.yml"
end

#display(message) ⇒ Object



3
4
5
# File 'lib/ssync/helpers.rb', line 3

def display(message)
  puts("[#{Time.now}] #{message}")
end

#display_error(message) ⇒ Object Also known as: e



7
8
9
# File 'lib/ssync/helpers.rb', line 7

def display_error(message)
  display("Error! " + message)
end

#exit_with_error!(message) ⇒ Object Also known as: e!



11
12
13
14
# File 'lib/ssync/helpers.rb', line 11

def exit_with_error!(message)
  display_error(message)
  exit
end

#lock_pathObject



45
46
47
# File 'lib/ssync/helpers.rb', line 45

def lock_path
  "#{ssync_homedir}/#{ssync_filename}.lock"
end

#read_configObject



62
63
64
65
66
67
68
# File 'lib/ssync/helpers.rb', line 62

def read_config
  begin
    open(config_path, "r") { |f| YAML::load(f) }
  rescue
    {}
  end
end

#read_default_configObject



70
71
72
73
74
75
76
# File 'lib/ssync/helpers.rb', line 70

def read_default_config
  begin
    open(default_config_path, "r") { |f| YAML::load(f) }
  rescue
    {}
  end
end

#ssync_filenameObject



33
34
35
# File 'lib/ssync/helpers.rb', line 33

def ssync_filename
  "#{read_default_config[:last_used_bucket]}"
end

#ssync_homedirObject



29
30
31
# File 'lib/ssync/helpers.rb', line 29

def ssync_homedir
  "#{ENV['HOME']}/.ssync"
end

#write_config!(config) ⇒ Object



78
79
80
# File 'lib/ssync/helpers.rb', line 78

def write_config!(config)
  open(config_path, "w") { |f| YAML::dump(config, f) }
end

#write_default_config!(config) ⇒ Object



82
83
84
85
# File 'lib/ssync/helpers.rb', line 82

def write_default_config!(config)
  create_homedir!
  open(default_config_path, "w") { |f| YAML::dump(config, f) }
end