Commit 45e2f78b6d

45e2f78b6da2997f10c5e99451d4bf3b5eaa7178

parent: 9f2baecd2e

Unsigned

Wraient <wraient.911@gmail.com> · 2024-10-22 17:56 UTC

Add: Edit config function

Layout: unified · split

cmd/curd/main.go +7 −1
@@ -51,7 +51,8 @@ func main() {
5151 flag.BoolVar(&userCurdConfig.SaveMpvSpeed, "save-mpv-speed", userCurdConfig.SaveMpvSpeed, "Save MPV speed setting (true/false)")
5252 flag.BoolVar(&userCurdConfig.DiscordPresence, "discord-presence", userCurdConfig.DiscordPresence, "Enable Discord presence (true/false)")
5353 continueLast := flag.Bool("c", false, "Continue last episode")
54
54 editConfig := flag.Bool("e", false, "Edit config")
55
5556 subFlag := flag.Bool("sub", false, "Watch sub version")
5657 dubFlag := flag.Bool("dub", false, "Watch dub version")
5758
@@ -66,6 +67,11 @@ func main() {
6667
6768 anime.Ep.ContinueLast = *continueLast
6869
70 if *editConfig {
71 internal.EditConfig(configFilePath)
72 return
73 }
74
6975 // Set SubOrDub based on the flags
7076 if *subFlag {
7177 userCurdConfig.SubOrDub = "sub"
internal/curd.go +27
@@ -4,6 +4,7 @@ import (
44 "encoding/json"
55 "fmt"
66 "os"
7 "os/exec"
78 "path/filepath"
89 "runtime"
910 "strconv"
@@ -24,6 +25,32 @@ func GetTokenFromFile(filePath string) (string, error) {
2425 return token, nil
2526}
2627
28func EditConfig(configFilePath string) {
29 // Get the user's preferred editor from the EDITOR environment variable
30 editor := os.Getenv("EDITOR")
31 if editor == "" {
32 // If EDITOR is not set, default to "vim"
33 editor = "vim"
34 }
35
36 // Construct the command to open the config file
37 cmd := exec.Command(editor, configFilePath)
38
39 // Set the command to run in the current terminal
40 cmd.Stdin = os.Stdin
41 cmd.Stdout = os.Stdout
42 cmd.Stderr = os.Stderr
43
44 // Run the editor command
45 err := cmd.Run()
46 if err != nil {
47 fmt.Printf("Error opening config file: %v\n", err)
48 return
49 }
50
51 fmt.Println("Config file edited successfully.")
52}
53
2754// ClearLogFile removes all contents from the specified log file
2855func ClearLogFile(logFile string) error {
2956 // Open the file with truncate flag to clear its contents