From 1aecb2cb40314a80efcc8c4a9e37b19adf837fd6 Mon Sep 17 00:00:00 2001 From: weilirs Date: Fri, 27 Dec 2024 17:10:05 -0500 Subject: [PATCH 1/3] feat: wsh export-config command --- cmd/wsh/cmd/wshcmd-export-config.go | 88 +++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 cmd/wsh/cmd/wshcmd-export-config.go diff --git a/cmd/wsh/cmd/wshcmd-export-config.go b/cmd/wsh/cmd/wshcmd-export-config.go new file mode 100644 index 000000000..72f74e742 --- /dev/null +++ b/cmd/wsh/cmd/wshcmd-export-config.go @@ -0,0 +1,88 @@ +package cmd + +import ( + "archive/zip" + "fmt" + "io" + "os" + "path/filepath" + "strings" + + "github.com/spf13/cobra" +) + +func init() { + var exportConfigCmd = &cobra.Command{ + Use: "export-config [output-path]", + Short: "export Wave Terminal configuration", + Long: "Export Wave Terminal configuration files into a zip archive", + RunE: runExportConfig, + } + rootCmd.AddCommand(exportConfigCmd) +} + +func runExportConfig(cmd *cobra.Command, args []string) error { + if len(args) != 1 { + return fmt.Errorf("export-config requires an output path") + } + + outputPath := args[0] + if !strings.HasSuffix(outputPath, ".zip") { + outputPath += ".zip" + } + + configDir := getWaveConfigDir() + if err := zipDir(configDir, outputPath); err != nil { + return fmt.Errorf("export-config failed: %v", err) + } + + fmt.Printf("Configuration exported to %s\n", outputPath) + return nil +} + +func zipDir(sourceDir, zipPath string) error { + zipFile, err := os.Create(zipPath) + if err != nil { + return err + } + defer zipFile.Close() + + archive := zip.NewWriter(zipFile) + defer archive.Close() + + return filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + return nil + } + + relPath, err := filepath.Rel(sourceDir, path) + if err != nil { + return err + } + + file, err := os.Open(path) + if err != nil { + return err + } + defer file.Close() + + writer, err := archive.Create(relPath) + if err != nil { + return err + } + + _, err = io.Copy(writer, file) + return err + }) +} + +func getWaveConfigDir() string { + homeDir, err := os.UserHomeDir() + if err != nil { + return "" + } + return filepath.Join(homeDir, ".config", "waveterm") +} From c59ab85ec783e93f5ae65e0d72b48bf8ba422dc9 Mon Sep 17 00:00:00 2001 From: weilirs Date: Mon, 30 Dec 2024 09:50:20 -0500 Subject: [PATCH 2/3] fix: check config directory's existence --- cmd/wsh/cmd/wshcmd-export-config.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cmd/wsh/cmd/wshcmd-export-config.go b/cmd/wsh/cmd/wshcmd-export-config.go index 72f74e742..88e606a36 100644 --- a/cmd/wsh/cmd/wshcmd-export-config.go +++ b/cmd/wsh/cmd/wshcmd-export-config.go @@ -32,6 +32,11 @@ func runExportConfig(cmd *cobra.Command, args []string) error { } configDir := getWaveConfigDir() + + if stat, err := os.Stat(configDir); err != nil || !stat.IsDir() { + return fmt.Errorf("Wave config directory not found at %s", configDir) + } + if err := zipDir(configDir, outputPath); err != nil { return fmt.Errorf("export-config failed: %v", err) } From 9011102fe8d09ad2cc3a53818e56a38669b97b90 Mon Sep 17 00:00:00 2001 From: weilirs Date: Mon, 30 Dec 2024 11:05:23 -0500 Subject: [PATCH 3/3] doc for exportconfig --- cmd/wsh/cmd/wshcmd-export-config.go | 6 +++--- docs/docs/wsh-reference.mdx | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/wsh/cmd/wshcmd-export-config.go b/cmd/wsh/cmd/wshcmd-export-config.go index 88e606a36..3fe01d57d 100644 --- a/cmd/wsh/cmd/wshcmd-export-config.go +++ b/cmd/wsh/cmd/wshcmd-export-config.go @@ -13,7 +13,7 @@ import ( func init() { var exportConfigCmd = &cobra.Command{ - Use: "export-config [output-path]", + Use: "exportconfig [output-path]", Short: "export Wave Terminal configuration", Long: "Export Wave Terminal configuration files into a zip archive", RunE: runExportConfig, @@ -23,7 +23,7 @@ func init() { func runExportConfig(cmd *cobra.Command, args []string) error { if len(args) != 1 { - return fmt.Errorf("export-config requires an output path") + return fmt.Errorf("exportconfig requires an output path") } outputPath := args[0] @@ -38,7 +38,7 @@ func runExportConfig(cmd *cobra.Command, args []string) error { } if err := zipDir(configDir, outputPath); err != nil { - return fmt.Errorf("export-config failed: %v", err) + return fmt.Errorf("exportconfig failed: %v", err) } fmt.Printf("Configuration exported to %s\n", outputPath) diff --git a/docs/docs/wsh-reference.mdx b/docs/docs/wsh-reference.mdx index 2931c4683..f59875fe6 100644 --- a/docs/docs/wsh-reference.mdx +++ b/docs/docs/wsh-reference.mdx @@ -153,6 +153,16 @@ wsh editconfig presets/ai.json --- +## exportconfig + +You can export your Wave's config files into a zip archive using this command. + +``` +wsh exportconfig [output-path] +``` + +--- + ## setbg The `setbg` command allows you to set a background image or color for the current tab with various customization options.