Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wsh export-config command #1641

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions cmd/wsh/cmd/wshcmd-export-config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cmd

import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
)

func init() {
var exportConfigCmd = &cobra.Command{
Use: "exportconfig [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("exportconfig requires an output path")
}

outputPath := args[0]
if !strings.HasSuffix(outputPath, ".zip") {
outputPath += ".zip"
}

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("exportconfig 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not reliable as the user may have a different config directory defined, such as on dev where it's ~/.config/waveterm-dev. You should use wavebase.GetWaveConfigDir instead.

Copy link
Member

@esimkowitz esimkowitz Jan 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, because this is called from the wsh side, which doesn't have the environment variables defined, you should instead call the wshclient.PathCommand function, since that will get you whatever the value is in wavesrv

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, this won't work at all if you're on a remote machine. The logic for zipping the config should be moved to wavesrv and called through the wshserver mechanism, you can look at the other commands to see how we do this. Then, if the user is invoking this command from a remote machine, we should copy the zipped archive to the output path on the remote machine

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To copy the zipped archive to the output path on the remote machine, should I use the filestore.WFS.WriteFile or something else? I am using filestore.WFS.WriteFile but ls doesn't show anything on the remote machine. thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm working on a more flexible filesystem traversal mechanism right now, targeting end of this week. Once this is implemented, I'll let you know so you can integrate it here. For now, let's hold on this PR

homeDir, err := os.UserHomeDir()
if err != nil {
return ""
}
return filepath.Join(homeDir, ".config", "waveterm")
}
10 changes: 10 additions & 0 deletions docs/docs/wsh-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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]
```

---
Comment on lines +156 to +164
Copy link
Contributor

@coderabbitai coderabbitai bot Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance documentation for the exportconfig command.

While the basic documentation is clear, it would be more helpful to users if it included:

  1. Default behavior when no output path is specified
  2. List of configuration files that get included in the zip
  3. Usage examples
  4. Any available command flags/options

Consider expanding the documentation like this:

 ## exportconfig
 
-You can export your Wave's config files into a zip archive using this command.
+You can export your Wave Terminal's configuration files into a zip archive. This is useful for backing up your settings or transferring them to another machine.
 

wsh exportconfig [output-path]

+
+If no output path is specified, it defaults to `wave-config-<timestamp>.zip` in the current directory.
+
+The exported archive includes:
+- settings.json
+- presets.json
+- widgets.json
+- AI presets
+- Other configuration files
+
+Examples:
+```bash
+# Export to the default location
+wsh exportconfig
+
+# Export to a specific file
+wsh exportconfig ~/backups/wave-config.zip
+
+# Export to a specific directory (filename will be auto-generated)
+wsh exportconfig ~/backups/
+```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good suggestion @weilirs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!


## setbg

The `setbg` command allows you to set a background image or color for the current tab with various customization options.
Expand Down