55 "fmt"
66 "os"
77 "path/filepath"
8+ "sort"
89
10+ "github.com/OpenSlides/openslides-cli/internal/constants"
911 "github.com/OpenSlides/openslides-cli/internal/k8s/client"
1012 "github.com/OpenSlides/openslides-cli/internal/logger"
1113 "github.com/OpenSlides/openslides-cli/internal/utils"
@@ -108,6 +110,7 @@ func applyDirectory(ctx context.Context, k8sClient *client.Client, dirPath strin
108110 return fmt .Errorf ("reading directory: %w" , err )
109111 }
110112
113+ var yamlFiles []os.DirEntry
111114 for _ , file := range files {
112115 if file .IsDir () {
113116 continue
@@ -117,7 +120,16 @@ func applyDirectory(ctx context.Context, k8sClient *client.Client, dirPath strin
117120 logger .Debug ("Skipping non-YAML file: %s" , file .Name ())
118121 continue
119122 }
123+ yamlFiles = append (yamlFiles , file )
124+ }
125+
126+ sort .Slice (yamlFiles , func (i , j int ) bool {
127+ kindI := getKindFromFile (filepath .Join (dirPath , yamlFiles [i ].Name ()))
128+ kindJ := getKindFromFile (filepath .Join (dirPath , yamlFiles [j ].Name ()))
129+ return constants .GetKindPriority (kindI ) < constants .GetKindPriority (kindJ )
130+ })
120131
132+ for _ , file := range yamlFiles {
121133 manifestPath := filepath .Join (dirPath , file .Name ())
122134 if _ , err := applyManifest (ctx , k8sClient , manifestPath ); err != nil {
123135 logger .Warn ("Failed to apply %s: %v" , file .Name (), err )
@@ -127,3 +139,18 @@ func applyDirectory(ctx context.Context, k8sClient *client.Client, dirPath strin
127139
128140 return nil
129141}
142+
143+ // getKindFromFile reads the Kind field from a YAML file
144+ func getKindFromFile (path string ) string {
145+ data , err := os .ReadFile (path )
146+ if err != nil {
147+ return ""
148+ }
149+
150+ var obj unstructured.Unstructured
151+ if err := yaml .Unmarshal (data , & obj ); err != nil {
152+ return ""
153+ }
154+
155+ return obj .GetKind ()
156+ }
0 commit comments