|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +func init() { |
| 15 | + var exportConfigCmd = &cobra.Command{ |
| 16 | + Use: "export-config [output-path]", |
| 17 | + Short: "export Wave Terminal configuration", |
| 18 | + Long: "Export Wave Terminal configuration files into a zip archive", |
| 19 | + RunE: runExportConfig, |
| 20 | + } |
| 21 | + rootCmd.AddCommand(exportConfigCmd) |
| 22 | +} |
| 23 | + |
| 24 | +func runExportConfig(cmd *cobra.Command, args []string) error { |
| 25 | + if len(args) != 1 { |
| 26 | + return fmt.Errorf("export-config requires an output path") |
| 27 | + } |
| 28 | + |
| 29 | + outputPath := args[0] |
| 30 | + if !strings.HasSuffix(outputPath, ".zip") { |
| 31 | + outputPath += ".zip" |
| 32 | + } |
| 33 | + |
| 34 | + configDir := getWaveConfigDir() |
| 35 | + if err := zipDir(configDir, outputPath); err != nil { |
| 36 | + return fmt.Errorf("export-config failed: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + fmt.Printf("Configuration exported to %s\n", outputPath) |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +func zipDir(sourceDir, zipPath string) error { |
| 44 | + zipFile, err := os.Create(zipPath) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + defer zipFile.Close() |
| 49 | + |
| 50 | + archive := zip.NewWriter(zipFile) |
| 51 | + defer archive.Close() |
| 52 | + |
| 53 | + return filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error { |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + if info.IsDir() { |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + relPath, err := filepath.Rel(sourceDir, path) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + file, err := os.Open(path) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + defer file.Close() |
| 71 | + |
| 72 | + writer, err := archive.Create(relPath) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + _, err = io.Copy(writer, file) |
| 78 | + return err |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +func getWaveConfigDir() string { |
| 83 | + homeDir, err := os.UserHomeDir() |
| 84 | + if err != nil { |
| 85 | + return "" |
| 86 | + } |
| 87 | + return filepath.Join(homeDir, ".config", "waveterm") |
| 88 | +} |
0 commit comments