Skip to content

Commit

Permalink
Added --fix-config-file flag to solve the problem that config file al…
Browse files Browse the repository at this point in the history
…ways overwritten when launching `spf` #421
  • Loading branch information
yorukot committed Oct 12, 2024
1 parent b9c902e commit 71edf64
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func Run(content embed.FS) {
Usage: "Adds any missing hotkeys to the hotkey config file",
Value: false,
},
&cli.BoolFlag{
Name: "fix-config-file",
Aliases: []string{"fch"},
Usage: "Adds any missing hotkeys to the hotkey config file",
Value: false,
},
},
Action: func(c *cli.Context) error {
path := ""
Expand All @@ -63,6 +69,7 @@ func Run(content embed.FS) {
InitConfigFile()

variable.FixHotkeys = c.Bool("fix-hotkeys")
variable.FixConfigFile = c.Bool("fix-config-file")

firstUse := checkFirstUse()

Expand All @@ -74,6 +81,7 @@ func Run(content embed.FS) {
CheckForUpdates()
return nil
},

}

err := app.Run(os.Args)
Expand Down
1 change: 1 addition & 0 deletions src/config/fixed_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
ToggleDotFile string = SuperFileDataDir + "/toggleDotFile"
LogFile string = SuperFileStateDir + "/superfile.log"
FixHotkeys bool = false
FixConfigFile bool = false
)

const (
Expand Down
17 changes: 11 additions & 6 deletions src/internal/config_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ func loadConfigFile() {

_ = toml.Unmarshal(data, &tempForCheckMissingConfig)
err = toml.Unmarshal(data, &Config)
if err != nil {
log.Fatalf("Error decoding config file ( your config file may have misconfigured ): %v", err)
if err != nil && !variable.FixConfigFile {
fmt.Print(lipgloss.NewStyle().Foreground(lipgloss.Color("#F93939")).Render("Error") +
lipgloss.NewStyle().Foreground(lipgloss.Color("#00FFEE")).Render(" ┃ ") +
"Error decoding configuration file\n")
fmt.Println("To add missing fields to hotkeys directory automaticially run Superfile with the --fix-config-file flag `spf --fix-config-file`")
}

if !reflect.DeepEqual(Config, tempForCheckMissingConfig) {
Expand All @@ -86,9 +89,11 @@ func loadConfigFile() {
log.Fatalf("Error encoding config: %v", err)
}

err = os.WriteFile(variable.ConfigFile, tomlData, 0644)
if err != nil {
log.Fatalf("Error writing config file: %v", err)
if variable.FixConfigFile {
err = os.WriteFile(variable.ConfigFile, tomlData, 0644)
if err != nil {
log.Fatalf("Error writing config file: %v", err)
}
}
}
if (Config.FilePreviewWidth > 10 || Config.FilePreviewWidth < 2) && Config.FilePreviewWidth != 0 {
Expand Down Expand Up @@ -133,7 +138,7 @@ func loadHotkeysFile() {
fmt.Sprintf("Field \"%s\" is missing in hotkeys configuration\n", name))
}
}
fmt.Println("To add missing fields to hotkeys directory automaticially run Superfile with the --fix-hotkeys flag")
fmt.Println("To add missing fields to hotkeys directory automaticially run Superfile with the --fix-hotkeys flag `spf --fix-hotkeys`")
}

if hasMissingHotkeysInConfig && variable.FixHotkeys {
Expand Down

0 comments on commit 71edf64

Please sign in to comment.