|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "sort" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + |
| 12 | + "github.com/open-source-cloud/devstack/internal/migrate" |
| 13 | +) |
| 14 | + |
| 15 | +// newImportCmd wires `devstack import <project.yaml>` (spec 14 §import): convert a |
| 16 | +// legacy devdock single-file project.yaml into a workspace.yaml + per-repo |
| 17 | +// devstack.yaml split, plus a lossless-or-loud conversion report. No-clobber by |
| 18 | +// default; `--force` backs up originals first; `--dry-run` writes nothing. |
| 19 | +func newImportCmd(g *GlobalOpts) *cobra.Command { |
| 20 | + var ( |
| 21 | + dryRun bool |
| 22 | + outDir string |
| 23 | + force bool |
| 24 | + ) |
| 25 | + cmd := &cobra.Command{ |
| 26 | + Use: "import <path/to/project.yaml>", |
| 27 | + Short: "Convert a legacy devdock project.yaml into workspace.yaml + per-repo devstack.yaml", |
| 28 | + Long: "import reads an old devdock single-file project.yaml and emits the clean-slate\n" + |
| 29 | + "two-file schema — a workspace.yaml (shared layer) plus a devstack.yaml per repo —\n" + |
| 30 | + "and a conversion report listing every field it could not convert (nothing is\n" + |
| 31 | + "dropped silently). It refuses to overwrite existing files without --force.", |
| 32 | + Args: cobra.ExactArgs(1), |
| 33 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 34 | + src, err := os.ReadFile(args[0]) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + if outDir == "" { |
| 39 | + outDir = "." |
| 40 | + } |
| 41 | + abs, _ := filepath.Abs(outDir) |
| 42 | + res, err := migrate.Convert(src, filepath.Base(abs)) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + type target struct { |
| 48 | + path string |
| 49 | + body []byte |
| 50 | + } |
| 51 | + targets := []target{{path: filepath.Join(outDir, "workspace.yaml"), body: res.WorkspaceYAML}} |
| 52 | + projNames := make([]string, 0, len(res.Projects)) |
| 53 | + for name := range res.Projects { |
| 54 | + projNames = append(projNames, name) |
| 55 | + } |
| 56 | + sort.Strings(projNames) |
| 57 | + for _, name := range projNames { |
| 58 | + targets = append(targets, target{path: filepath.Join(outDir, name, "devstack.yaml"), body: res.Projects[name]}) |
| 59 | + } |
| 60 | + reportPath := filepath.Join(outDir, "devstack-import-report.txt") |
| 61 | + report := migrate.RenderReport(res.Report) |
| 62 | + |
| 63 | + paths := make([]string, 0, len(targets)+1) |
| 64 | + for _, t := range targets { |
| 65 | + paths = append(paths, t.path) |
| 66 | + } |
| 67 | + paths = append(paths, reportPath) |
| 68 | + |
| 69 | + if dryRun { |
| 70 | + if g.JSON { |
| 71 | + return writeJSON(cmd, importSummary(paths, res.Report, true)) |
| 72 | + } |
| 73 | + w := cmd.OutOrStdout() |
| 74 | + for _, t := range targets { |
| 75 | + fmt.Fprintf(w, "\n--- %s ---\n%s", t.path, t.body) |
| 76 | + } |
| 77 | + fmt.Fprintf(w, "\n--- %s ---\n%s", reportPath, report) |
| 78 | + fmt.Fprintln(w, "\n(dry-run: nothing written)") |
| 79 | + return nil |
| 80 | + } |
| 81 | + |
| 82 | + // No-clobber unless --force (then back up the originals first). |
| 83 | + for _, t := range targets { |
| 84 | + if _, err := os.Stat(t.path); err == nil { |
| 85 | + if !force { |
| 86 | + return fmt.Errorf("%s already exists; pass --force to overwrite (originals are backed up)", t.path) |
| 87 | + } |
| 88 | + if err := os.Rename(t.path, fmt.Sprintf("%s.bak.%d", t.path, time.Now().Unix())); err != nil { |
| 89 | + return fmt.Errorf("back up %s: %w", t.path, err) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + for _, t := range targets { |
| 94 | + if err := os.MkdirAll(filepath.Dir(t.path), 0o755); err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + if err := os.WriteFile(t.path, t.body, 0o644); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + } |
| 101 | + if err := os.WriteFile(reportPath, []byte(report), 0o644); err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + if g.JSON { |
| 106 | + return writeJSON(cmd, importSummary(paths, res.Report, false)) |
| 107 | + } |
| 108 | + w := cmd.OutOrStdout() |
| 109 | + for _, p := range paths { |
| 110 | + fmt.Fprintf(w, "[ok] wrote %s\n", p) |
| 111 | + } |
| 112 | + fmt.Fprint(w, "\n"+report) |
| 113 | + if len(res.Report) > 0 { |
| 114 | + fmt.Fprintln(w, "\nReview the report above and the generated files before committing (see docs/MIGRATION.md).") |
| 115 | + } |
| 116 | + return nil |
| 117 | + }, |
| 118 | + } |
| 119 | + cmd.Flags().BoolVar(&dryRun, "dry-run", false, "print the converted files + report without writing anything") |
| 120 | + cmd.Flags().StringVar(&outDir, "out", "", "output directory (default: current directory)") |
| 121 | + cmd.Flags().BoolVar(&force, "force", false, "overwrite existing files (backs up originals first)") |
| 122 | + return cmd |
| 123 | +} |
| 124 | + |
| 125 | +func importSummary(paths []string, report []migrate.ReportEntry, dry bool) map[string]any { |
| 126 | + entries := make([]map[string]string, 0, len(report)) |
| 127 | + for _, e := range report { |
| 128 | + entries = append(entries, map[string]string{"path": e.Path, "value": e.Value, "reason": e.Reason}) |
| 129 | + } |
| 130 | + return map[string]any{"files": paths, "report": entries, "dryRun": dry} |
| 131 | +} |
0 commit comments