Skip to content
This repository was archived by the owner on Jan 27, 2023. It is now read-only.

Commit 980341e

Browse files
authored
Merge pull request #22 from cipherstash/handle-default-profile-exists-error
Add logic to update workspace used for default profile
2 parents 32fb1ed + 1da6097 commit 980341e

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

lib/cipherstash/client/profile.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,33 @@ def self.create(name, logger, **opts)
8989
Dir.mkdir(File.expand_path("~/.cipherstash/#{name}"))
9090
logger.debug("CipherStash::Client::Profile.create") { "Created ~/.cipherstash/#{name}" }
9191
rescue Errno::EEXIST
92-
raise Error::CreateProfileFailure, "Could not create profile #{name.inspect}: already exists"
92+
if name == "default"
93+
incoming_workspace_id = opts[:workspace]
94+
95+
96+
profile_config = File.read(File.expand_path("~/.cipherstash/#{name}/profile-config.json"))
97+
parsed_profile_config = JSON.parse(profile_config)
98+
99+
default_profile_workspace_id = parsed_profile_config["service"]["workspace"]
100+
101+
if incoming_workspace_id == default_profile_workspace_id
102+
raise Error::CreateProfileFailure, "Could not create profile #{name.inspect}: already exists"
103+
else
104+
logger.debug("CipherStash::Client::Profile.create") { "~/.cipherstash/#{name} created with Workspace ID: #{default_profile_workspace_id}. Updating #{name} profile to use Workspace ID: #{incoming_workspace_id}." }
105+
end
106+
else
107+
raise Error::CreateProfileFailure, "Could not create profile #{name.inspect}: already exists"
108+
end
109+
93110
rescue => ex
94111
raise Error::CreateProfileFailure, "Could not create profile directory ~/.cipherstash/#{name}: #{ex.message} (#{ex.class})"
95112
end
96113

97114
begin
115+
if name == "default" && File.exists?(File.expand_path("~/.cipherstash/#{name}/auth-token.json"))
116+
File.delete(File.expand_path("~/.cipherstash/#{name}/auth-token.json"))
117+
end
118+
98119
File.write(File.expand_path("~/.cipherstash/#{name}/profile-config.json"), default_profile.to_json)
99120
logger.debug("CipherStash::Client::Profile.create") { "Wrote ~/.cipherstash/#{name}/profile-config.json" }
100121
rescue => ex

0 commit comments

Comments
 (0)