Class: OsCtl::Repo::Cli::Repo

Inherits:
Command
  • Object
show all
Defined in:
lib/osctl/repo/cli/repo.rb

Instance Method Summary collapse

Methods inherited from Command

run

Instance Method Details

#addObject



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
# File 'lib/osctl/repo/cli/repo.rb', line 37

def add
  require_args!('vendor', 'variant', 'arch', 'distribution', 'version')

  repo = Local::Repository.new(Dir.pwd)
  raise 'repository not found' unless repo.exist?

  vendor, variant, arch, distribution, version = args

  if vendor == 'default'
    raise GLI::BadCommandLine, 'unable to set vendor to default, name reserved'

  elsif variant == 'default'
    raise GLI::BadCommandLine, 'unable to set variant to default, name reserved'
  end

  image = {
    tar: opts[:archive],
    zfs: opts[:stream]
  }.select { |_, v| v }.to_h

  if image.empty?
    raise GLI::BadCommandLine, 'no image, use --archive or --stream'
  end

  repo.add(
    vendor,
    variant,
    arch,
    distribution,
    version,
    tags: opts[:tag],
    image:
  )
end

#fetchObject



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/osctl/repo/cli/repo.rb', line 132

def fetch
  require_args!(
    'repo', 'vendor', 'variant', 'arch', 'distribution', 'version|tag',
    'tar|zfs'
  )

  repo = Remote::Repository.new(args[0])
  repo.path = opts[:cache]

  dl = Downloader::Cached.new(repo)
  puts dl.get(*args[1..], force_check: true)
end

#initObject



6
7
8
9
10
11
# File 'lib/osctl/repo/cli/repo.rb', line 6

def init
  repo = Local::Repository.new(Dir.pwd)
  raise 'repository already exists' if repo.exist?

  repo.create
end

#local_get_pathObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/osctl/repo/cli/repo.rb', line 72

def local_get_path
  require_args!(
    'vendor', 'variant', 'arch', 'distribution', 'version|tag', 'tar|zfs'
  )

  repo = Local::Repository.new(Dir.pwd)
  raise 'repository not found' unless repo.exist?

  vendor, variant, arch, distribution, version, format = args
  img = repo.find(vendor, variant, arch, distribution, version)
  raise 'image not found' unless img
  raise 'image format not found' unless img.has_image?(format)

  puts img.version_image_path(format)
end

#local_listObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/osctl/repo/cli/repo.rb', line 13

def local_list
  repo = Local::Repository.new(Dir.pwd)
  raise 'repository not found' unless repo.exist?

  fmt = '%-18s %-18s %-10s %-20s %-10s %s'

  puts format(
    fmt,
    'VENDOR', 'VARIANT', 'ARCH', 'DISTRIBUTION', 'VERSION', 'TAGS'
  )

  repo.images.each do |t|
    puts format(
      fmt,
      t.vendor,
      t.variant,
      t.arch,
      t.distribution,
      t.version,
      t.tags.join(',')
    )
  end
end

#remote_get_commonObject (protected)



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/osctl/repo/cli/repo.rb', line 159

def remote_get_common
  require_args!(
    'repo', 'vendor', 'variant', 'arch', 'distribution', 'version|tag',
    'tar|zfs'
  )

  repo = Remote::Repository.new(args[0])

  if opts[:cache]
    repo.path = opts[:cache]
    Downloader::Cached.new(repo)
  else
    Downloader::Direct.new(repo)
  end
end

#remote_get_pathObject



145
146
147
148
# File 'lib/osctl/repo/cli/repo.rb', line 145

def remote_get_path
  dl = remote_get_common
  puts dl.get(*args[1..], force_check: opts['force-check'])
end

#remote_get_streamObject



150
151
152
153
154
155
# File 'lib/osctl/repo/cli/repo.rb', line 150

def remote_get_stream
  dl = remote_get_common
  dl.get(*args[1..], force_check: opts['force-check']) do |fragment|
    $stdout.write(fragment)
  end
end

#remote_listObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/osctl/repo/cli/repo.rb', line 117

def remote_list
  require_args!('repo')

  repo = Remote::Repository.new(args[0])

  if opts[:cache]
    repo.path = opts[:cache]
    dl = Downloader::Cached.new(repo)
  else
    dl = Downloader::Direct.new(repo)
  end

  puts dl.list.map(&:dump).to_json
end

#rmObject



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/osctl/repo/cli/repo.rb', line 105

def rm
  require_args!('vendor', 'variant', 'arch', 'distribution', 'version')

  repo = Local::Repository.new(Dir.pwd)
  raise 'repository not found' unless repo.exist?

  tpl = repo.find(*args)
  raise 'image not found' unless tpl

  repo.remove(tpl)
end

#set_defaultObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/osctl/repo/cli/repo.rb', line 88

def set_default
  require_args!('vendor', optional: %w[variant])

  repo = Local::Repository.new(Dir.pwd)
  raise 'repository not found' unless repo.exist?

  if args.count == 1
    repo.set_default_vendor(args[0])

  elsif args.count == 2
    repo.set_default_variant(args[0], args[1])

  else
    raise GLI::BadCommandLine, 'too many aguments'
  end
end