mirror of
https://github.com/StarFleetCPTN/GoMFT.git
synced 2026-10-11 00:37:28 +02:00
feat: Enhance Rclone command configuration with pre-rendered flags and region support
- Added fields for pre-rendering flags in the config form, allowing for better user experience during edits. - Updated Rclone command options to accept current command ID for pre-selection. - Introduced region input fields for MinIO source and destination forms, enhancing configuration flexibility. - Modified backend logic to handle region parameters when generating Rclone configurations. - Improved flag handling by parsing selected flags and values for better state management in the UI.
This commit is contained in:
1 parent
d4c07fdc8e
commit
f95720758e
7 files changed
+186
-57
No files matched your search
@@ -65,9 +65,43 @@ func (h *Handlers) HandleEditConfig(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch the initial command details for pre-rendering flags
|
||||
initialCommand, err := h.DB.GetRcloneCommandWithFlags(config.CommandID)
|
||||
if err != nil {
|
||||
// Log the error but proceed, the form might still be usable without pre-rendered flags
|
||||
log.Printf("Warning: Failed to get initial command flags for config %d: %v", config.ID, err)
|
||||
initialCommand = nil // Ensure it's nil if fetching failed
|
||||
}
|
||||
|
||||
// Parse the selected flags and values from the config
|
||||
selectedFlagsMap := make(map[uint]bool)
|
||||
if config.CommandFlags != "" {
|
||||
var selectedFlagIDs []uint
|
||||
// Use json.Unmarshal directly as CommandFlags should be a JSON array string
|
||||
if err := json.Unmarshal([]byte(config.CommandFlags), &selectedFlagIDs); err == nil {
|
||||
for _, id := range selectedFlagIDs {
|
||||
selectedFlagsMap[id] = true
|
||||
}
|
||||
} else {
|
||||
log.Printf("Warning: Failed to unmarshal CommandFlags for config %d: %v. JSON: %s", config.ID, err, config.CommandFlags)
|
||||
}
|
||||
}
|
||||
|
||||
selectedFlagValues := make(map[uint]string)
|
||||
if config.CommandFlagValues != "" {
|
||||
// Use json.Unmarshal directly as CommandFlagValues should be a JSON object string
|
||||
if err := json.Unmarshal([]byte(config.CommandFlagValues), &selectedFlagValues); err != nil {
|
||||
log.Printf("Warning: Failed to unmarshal CommandFlagValues for config %d: %v. JSON: %s", config.ID, err, config.CommandFlagValues)
|
||||
selectedFlagValues = make(map[uint]string) // Reset on error
|
||||
}
|
||||
}
|
||||
|
||||
data := components.ConfigFormData{
|
||||
Config: &config,
|
||||
IsNew: false,
|
||||
Config: &config,
|
||||
IsNew: false,
|
||||
InitialCommand: initialCommand,
|
||||
SelectedFlagsMap: selectedFlagsMap,
|
||||
SelectedFlagValues: selectedFlagValues,
|
||||
}
|
||||
components.ConfigForm(c.Request.Context(), data).Render(c, c.Writer)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -26,6 +27,14 @@ func NewRcloneHandler(db *db.DB) *RcloneHandler {
|
||||
|
||||
// RcloneCommandOptions renders the rclone command options for the config form
|
||||
func (h *RcloneHandler) RcloneCommandOptions(c *gin.Context) {
|
||||
// Get the current command ID if provided (for pre-selection)
|
||||
currentCommandIDStr := c.DefaultQuery("commandId", "1") // Default to 1 (copy)
|
||||
currentCommandID, err := strconv.ParseUint(currentCommandIDStr, 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing current command ID: %v, using default 1", err)
|
||||
currentCommandID = 1
|
||||
}
|
||||
|
||||
commands, err := h.DB.GetRcloneCommands()
|
||||
if err != nil {
|
||||
log.Printf("Error getting rclone commands: %v", err)
|
||||
@@ -47,12 +56,20 @@ func (h *RcloneHandler) RcloneCommandOptions(c *gin.Context) {
|
||||
categoryMap[cmd.Category] = append(categoryMap[cmd.Category], cmd)
|
||||
}
|
||||
|
||||
_ = common.RcloneCommandOptionsContent(categoryMap, categories).Render(c.Request.Context(), c.Writer)
|
||||
// Get the initial flag JSON strings passed from the placeholder's hx-vals
|
||||
commandFlagsJSON := c.DefaultQuery("commandFlags", "[]")
|
||||
commandFlagValuesJSON := c.DefaultQuery("commandFlagValues", "{}")
|
||||
|
||||
// Pass the current command ID and flag JSON strings to the template
|
||||
_ = common.RcloneCommandOptionsContent(categoryMap, categories, uint(currentCommandID), commandFlagsJSON, commandFlagValuesJSON).Render(c.Request.Context(), c.Writer)
|
||||
}
|
||||
|
||||
// RcloneCommandFlags renders the rclone command flags for the selected command
|
||||
func (h *RcloneHandler) RcloneCommandFlags(c *gin.Context) {
|
||||
commandIDStr := c.DefaultQuery("command_id", "")
|
||||
commandFlagsJSON := c.DefaultQuery("commandFlags", "[]") // Get selected flag IDs JSON
|
||||
commandFlagValuesJSON := c.DefaultQuery("commandFlagValues", "{}") // Get selected flag values JSON
|
||||
|
||||
if commandIDStr == "" {
|
||||
c.String(http.StatusBadRequest, "Command ID is required")
|
||||
return
|
||||
@@ -72,6 +89,25 @@ func (h *RcloneHandler) RcloneCommandFlags(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Parse the selected flags and values
|
||||
var selectedFlagIDs []uint
|
||||
if err := json.Unmarshal([]byte(commandFlagsJSON), &selectedFlagIDs); err != nil {
|
||||
log.Printf("Error unmarshaling commandFlags JSON: %v, JSON: %s", err, commandFlagsJSON)
|
||||
// Don't fail, just proceed with no flags selected
|
||||
selectedFlagIDs = []uint{}
|
||||
}
|
||||
selectedFlagsMap := make(map[uint]bool)
|
||||
for _, id := range selectedFlagIDs {
|
||||
selectedFlagsMap[id] = true
|
||||
}
|
||||
|
||||
var selectedFlagValues map[uint]string
|
||||
if err := json.Unmarshal([]byte(commandFlagValuesJSON), &selectedFlagValues); err != nil {
|
||||
log.Printf("Error unmarshaling commandFlagValues JSON: %v, JSON: %s", err, commandFlagValuesJSON)
|
||||
// Don't fail, just proceed with no values
|
||||
selectedFlagValues = make(map[uint]string)
|
||||
}
|
||||
|
||||
if command == nil {
|
||||
c.String(http.StatusNotFound, "Command not found")
|
||||
return
|
||||
@@ -82,7 +118,8 @@ func (h *RcloneHandler) RcloneCommandFlags(c *gin.Context) {
|
||||
return command.Flags[i].Name < command.Flags[j].Name
|
||||
})
|
||||
|
||||
_ = common.RcloneCommandFlagsContent(command).Render(c.Request.Context(), c.Writer)
|
||||
// Pass the parsed selected flags and values to the template
|
||||
_ = common.RcloneCommandFlagsContent(command, selectedFlagsMap, selectedFlagValues).Render(c.Request.Context(), c.Writer)
|
||||
}
|
||||
|
||||
// RcloneCommandUsage renders the usage information for a command
|
||||
|
||||
Reference in new issue
Block a user