Class: SnapshotArchive::Repositories::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/snapshot_archive/repositories.rb

Constant Summary collapse

SCHEMA =
"0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:) ⇒ FileSystem

Returns a new instance of FileSystem.



6
7
8
# File 'lib/snapshot_archive/repositories.rb', line 6

def initialize(path:)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/snapshot_archive/repositories.rb', line 5

def path
  @path
end

Instance Method Details

#add(msg:, stores:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/snapshot_archive/repositories.rb', line 10

def add(msg:, stores:)
  id = SecureRandom.uuid
  timestamp = Time.now
  dir = mkdir(id)

   = Archives::Builder.call(id: id, dir: dir, stores: stores)

  Cfg.shell.debug("Using stores: #{stores.keys.join(", ")}")

  if .fetch(:stores).empty?
    Cfg.shell.warn("no data to save")
  else
     = {
      __schema__: SCHEMA,
      __gem_version__: SnapshotArchive::VERSION,
      id: id,
      message: msg,
      timestamp: timestamp.utc.iso8601,
      dir: dir,
      archive: 
    }

    Cfg.shell.debug("writing metadata: #{.to_json}")
    Cfg.shell.info("Saved snapshot: #{id}")
    File.write(File.join(dir, "metadata.json"), JSON.pretty_generate())
  end
end

#delete(id) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/snapshot_archive/repositories.rb', line 72

def delete(id)
  dir = File.join(path, id)
  md_path = File.join(dir, "metadata.json")
  raise ArgumentError.new("unknown snapshot: #{id}") unless File.exist?(md_path)
   = JSON.parse(File.read(md_path))

  Cfg.shell.info("Running delete hooks: #{id}")
  Archives::Delete.call(.dig("archive"))

  Cfg.shell.info("Removing snapshot: #{id}")
  FileUtils.rm_rf(dir)
  Cfg.shell.info("Removed snapshot: #{id}")
end

#list(older_than_days:) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/snapshot_archive/repositories.rb', line 45

def list(older_than_days:)
  snapshots = (
    Dir
      .glob(File.join(path, "**/metadata.json"))
      .map { || JSON.parse(File.read()) }
      .sort_by { || .fetch("timestamp") }
      .reverse
      .select { |m|
        Time.parse(m.fetch("timestamp")) < (Time.now - (older_than_days.to_i * 24 * 60 * 60))
      }
  )

  if snapshots.count > 0
    snapshots.each do ||
      Formatters::List.call()
    end
  else
    Cfg.shell.info("No snapshots found")
  end
end

#restore(id) ⇒ Object



38
39
40
41
42
43
# File 'lib/snapshot_archive/repositories.rb', line 38

def restore(id)
   = JSON.parse(File.read(File.join(path, id, "metadata.json")))

  Archives::Restore.call(.dig("archive"))
  Cfg.shell.info("Restored snapshot: #{id}")
end

#show(id) ⇒ Object



66
67
68
69
70
# File 'lib/snapshot_archive/repositories.rb', line 66

def show(id)
  Formatters::Show.call(
    JSON.parse(File.read(File.join(path, id, "metadata.json")))
  )
end