|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/apiqube/cli/internal/manifests/depends" |
| 8 | + "github.com/apiqube/cli/internal/ui" |
| 9 | + "github.com/apiqube/cli/internal/yaml" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +func init() { |
| 14 | + applyCmd.Flags().StringP("file", "f", ".", "Path to manifest file") |
| 15 | + rootCmd.AddCommand(applyCmd) |
| 16 | +} |
| 17 | + |
| 18 | +var applyCmd = &cobra.Command{ |
| 19 | + Use: "apply", |
| 20 | + Short: "Apply resources from manifest file", |
| 21 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 22 | + ui.Init() |
| 23 | + defer func() { |
| 24 | + time.Sleep(time.Millisecond * 100) |
| 25 | + ui.Stop() |
| 26 | + }() |
| 27 | + |
| 28 | + file, err := cmd.Flags().GetString("file") |
| 29 | + if err != nil { |
| 30 | + ui.Errorf("Failed to parse --file: %s", err.Error()) |
| 31 | + return err |
| 32 | + } |
| 33 | + |
| 34 | + ui.Printf("Applying manifests from: %s", file) |
| 35 | + ui.Spinner(true, "Loading manifests") |
| 36 | + |
| 37 | + mans, err := yaml.LoadManifestsFromDir(file) |
| 38 | + if err != nil { |
| 39 | + ui.Errorf("Failed to load manifests: %s", err.Error()) |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + ui.Spinner(false) |
| 44 | + ui.Printf("Loaded %d manifests", len(mans)) |
| 45 | + |
| 46 | + slices.Reverse(mans) |
| 47 | + for i, man := range mans { |
| 48 | + ui.Printf("#%d ID: %s", i+1, man.GetID()) |
| 49 | + } |
| 50 | + |
| 51 | + ui.Spinner(true, "Generating execution plan") |
| 52 | + |
| 53 | + if err = depends.GeneratePlan("./examples/simple", mans); err != nil { |
| 54 | + ui.Errorf("Failed to generate plan: %s", err.Error()) |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + ui.Spinner(false) |
| 59 | + ui.Print("Execution plan generated successfully") |
| 60 | + ui.Spinner(true, "Saving manifests...") |
| 61 | + |
| 62 | + if err := yaml.SaveManifests(file, mans...); err != nil { |
| 63 | + ui.Error("Failed to save manifests: " + err.Error()) |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + ui.Spinner(false) |
| 68 | + ui.Println("Manifests applied successfully") |
| 69 | + |
| 70 | + return nil |
| 71 | + }, |
| 72 | +} |
0 commit comments