File: state.rb

package info (click to toggle)
ruby-localhost 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 232 kB
  • sloc: ruby: 579; makefile: 4
file content (38 lines) | stat: -rw-r--r-- 1,081 bytes parent folder | download
1
2
3
4
5
6
7
8
9
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
37
38
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025, by Samuel Williams.

require "fileutils"

module Localhost
	# Represents a single public/private key pair for a given hostname.
	module State
		# Where to store the key pair on the filesystem. This is a subdirectory
		# of $XDG_STATE_HOME, or ~/.local/state/ when that's not defined.
		#
		# Ensures that the directory to store the certificate exists. If the legacy
		# directory (~/.localhost/) exists, it is moved into the new XDG Basedir
		# compliant directory.
		#
		# @parameter env [Hash] The environment to use for configuration.
		def self.path(env = ENV)
			path = File.expand_path("localhost.rb", env.fetch("XDG_STATE_HOME", "~/.local/state"))
			
			unless File.directory?(path)
				FileUtils.mkdir_p(path, mode: 0700)
			end
			
			return path
		end
		
		# Delete the directory where the key pair is stored.
		#
		# @parameter env [Hash] The environment to use for configuration.
		def self.purge(env = ENV)
			path = self.path(env)
			
			return FileUtils.rm_rf(path)
		end
	end
end