Class: DevArchive::Repositories::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/dev_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/dev_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/dev_archive/repositories.rb', line 5

def path
  @path
end

Instance Method Details

#add(message) ⇒ Object



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

def add(message)
  id = SecureRandom.uuid
  timestamp = Time.now
  dir = mkdir(id)

   = Archives::Builder.call(dir: dir)

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

    Cfg.shell.puts("writing metadata: #{.to_json}")
    File.write(File.join(dir, "metadata.json"), JSON.pretty_generate())
  end
end

#delete(id) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
# File 'lib/dev_archive/repositories.rb', line 57

def delete(id)
  dir = File.join(path, id)

  raise ArgumentError.new("unknown snapshot: #{id}") unless File.exist?(dir)

  FileUtils.rm_rf(dir)
end

#listObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/dev_archive/repositories.rb', line 40

def list
  Dir
    .glob(File.join(path, "**/metadata.json"))
    .map { || JSON.parse(File.read()) }
    .sort_by { || .fetch("timestamp") }
    .reverse
    .each do ||
      Formatters::List.call()
    end
end

#restore(id) ⇒ Object



34
35
36
37
38
# File 'lib/dev_archive/repositories.rb', line 34

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

  Archives::Restore.call(.dig("archive"))
end

#show(id) ⇒ Object



51
52
53
54
55
# File 'lib/dev_archive/repositories.rb', line 51

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