mirror of
https://github.com/czhu12/canine.git
synced 2025-12-30 15:49:54 -06:00
updated environment variables via file paste
This commit is contained in:
19
app/actions/environment_variables/parse_text_content.rb
Normal file
19
app/actions/environment_variables/parse_text_content.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
class EnvironmentVariables::ParseTextContent
|
||||
extend LightService::Action
|
||||
|
||||
expects :params
|
||||
promises :params
|
||||
|
||||
executed do |context|
|
||||
unless context.params[:text_content].present?
|
||||
next
|
||||
end
|
||||
|
||||
context.params[:environment_variables] = context.params[:text_content].strip.split("\n").map do |line|
|
||||
name, value = line.split("=")
|
||||
# If the value is in quotes, remove them
|
||||
value = value.gsub(/^"|"$/, "") if value.present?
|
||||
{ name:, value: }
|
||||
end
|
||||
end
|
||||
end
|
||||
10
app/actions/environment_variables/update.rb
Normal file
10
app/actions/environment_variables/update.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
class EnvironmentVariables::Update
|
||||
extend LightService::Organizer
|
||||
|
||||
def self.call(project:, params:, current_user:)
|
||||
with(project:, params:, current_user:).reduce(
|
||||
EnvironmentVariables::ParseTextContent,
|
||||
EnvironmentVariables::BulkUpdate
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -8,17 +8,25 @@ class Projects::EnvironmentVariablesController < Projects::BaseController
|
||||
end
|
||||
|
||||
def create
|
||||
EnvironmentVariables::BulkUpdate.execute(project: @project, params:, current_user:)
|
||||
@project.updated!
|
||||
result = EnvironmentVariables::Update.call(
|
||||
project: @project,
|
||||
params:,
|
||||
current_user:
|
||||
)
|
||||
if result.success?
|
||||
@project.updated!
|
||||
|
||||
if @project.current_deployment.present?
|
||||
Projects::DeploymentJob.perform_later(@project.current_deployment)
|
||||
@project.events.create(user: current_user, eventable: @project.last_build, event_action: :update)
|
||||
redirect_to project_environment_variables_path(@project),
|
||||
notice: "Deployment started to apply new environment variables."
|
||||
if @project.current_deployment.present?
|
||||
Projects::DeploymentJob.perform_later(@project.current_deployment)
|
||||
@project.events.create(user: current_user, eventable: @project.last_build, event_action: :update)
|
||||
redirect_to project_environment_variables_path(@project),
|
||||
notice: "Restarting services with new environment variables."
|
||||
else
|
||||
redirect_to project_environment_variables_path(@project),
|
||||
notice: "Environment variables will be applied on the next deployment."
|
||||
end
|
||||
else
|
||||
redirect_to project_environment_variables_path(@project),
|
||||
notice: "Environment variables will be applied on the next deployment."
|
||||
redirect_to project_environment_variables_path(@project), alert: "Failed to update environment variables."
|
||||
end
|
||||
end
|
||||
|
||||
@@ -30,10 +38,4 @@ class Projects::EnvironmentVariablesController < Projects::BaseController
|
||||
end
|
||||
render turbo_stream: turbo_stream.remove("environment_variable_#{@environment_variable.id}")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def environment_variable_params
|
||||
params.require(:environment_variable).permit(:name, :value)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -19,6 +19,17 @@ export default class extends Controller {
|
||||
})
|
||||
}
|
||||
|
||||
download() {
|
||||
const vars = JSON.parse(this.varsValue)
|
||||
const env = vars.map(v => `${v.name}=${v.value}`).join("\n")
|
||||
const blob = new Blob([env], { type: "text/plain" })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement("a")
|
||||
a.href = url
|
||||
a.download = ".env"
|
||||
a.click()
|
||||
}
|
||||
|
||||
add(e) {
|
||||
e.preventDefault();
|
||||
this._add("", "")
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { application } from "./application"
|
||||
import TextareaAutogrow from 'stimulus-textarea-autogrow'
|
||||
|
||||
import controllers from './**/*_controller.js'
|
||||
|
||||
application.register('textarea-autogrow', TextareaAutogrow)
|
||||
controllers.forEach((controller) => {
|
||||
application.register(controller.name, controller.module.default)
|
||||
})
|
||||
|
||||
@@ -7,8 +7,37 @@
|
||||
>
|
||||
<%= form_with(url: project_environment_variables_path(@project), method: :post) do |form| %>
|
||||
<div data-environment-variables-target="container"></div>
|
||||
<button class="btn btn-neutral btn-outline" type="button" data-action="environment-variables#add">Add New Environment Variable</button>
|
||||
<%= form.submit "Save", class: "btn btn-primary" %>
|
||||
<div class="flex flex-row justify-between">
|
||||
<div>
|
||||
<button class="btn btn-neutral btn-outline" type="button" data-action="environment-variables#add">Add new environment variable</button>
|
||||
<%= form.submit "Save", class: "btn btn-primary" %>
|
||||
</div>
|
||||
<button type="button" class="btn btn-outline" data-action="environment-variables#download">Download as .env</button>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<div class="mt-12">
|
||||
<div class="collapse bg-base-200">
|
||||
<input aria-label="Accordion radio" type="checkbox" name="accordion" class="w-full">
|
||||
<div class="collapse-title text-lg font-medium">Add from .env file</div>
|
||||
<div class="collapse-content">
|
||||
<div class="">
|
||||
<%= form_with(url: project_environment_variables_path(@project), method: :post) do |form| %>
|
||||
<%= form.text_area(
|
||||
:text_content,
|
||||
class: "textarea textarea-bordered w-full",
|
||||
placeholder: "KEY1=value1\nKEY2=value2",
|
||||
rows: 6,
|
||||
data: {
|
||||
controller: "textarea-autogrow",
|
||||
'textarea-autogrow-resize-debounce-delay-value': "500"
|
||||
}
|
||||
) %>
|
||||
<%= form.submit "Add variables", class: "btn btn-primary" %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
"prettier-plugin-tailwindcss": "^0.6.8",
|
||||
"sass": "^1.79.3",
|
||||
"stimulus-flatpickr": "^3.0.0-0",
|
||||
"stimulus-textarea-autogrow": "^4.1.0",
|
||||
"tailwindcss": "^3.4.1",
|
||||
"tailwindcss-stimulus-components": "^5.1.0",
|
||||
"tippy.js": "^6.3.7",
|
||||
|
||||
@@ -1384,6 +1384,11 @@ stimulus-flatpickr@^3.0.0-0:
|
||||
dependencies:
|
||||
"@hotwired/stimulus" "^3.0.0"
|
||||
|
||||
stimulus-textarea-autogrow@^4.1.0:
|
||||
version "4.1.0"
|
||||
resolved "https://registry.yarnpkg.com/stimulus-textarea-autogrow/-/stimulus-textarea-autogrow-4.1.0.tgz#faa2f7952062c4508b1566478dc203af7f24f632"
|
||||
integrity sha512-hYz+xN3VZiO+eLl+zok4yvhH+mW5WkvOJEExyVP3J97dWQzc/qPDFdAM1EDJvfUIuRzJVq/WJynqqQwiied5iw==
|
||||
|
||||
"string-width-cjs@npm:string-width@^4.2.0":
|
||||
version "4.2.3"
|
||||
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
|
||||
|
||||
Reference in New Issue
Block a user