Merge pull request 'Ajouter autoseed pendant serverCmd' (#66) from feature/autoseed into main

Reviewed-on: #66
This commit is contained in:
Victor Lacasse-Beaudoin 2023-07-04 19:37:45 -05:00
commit 6a73b66d5a
2 changed files with 45 additions and 24 deletions

View file

@ -46,6 +46,18 @@ var serverCmd = &cobra.Command{
log.Fatal(err) log.Fatal(err)
} }
mediaClient, err := media.NewMediaClientFromViper()
if err != nil {
log.Fatal(err)
}
new_buckets, err := mediaClient.Seed()
if err != nil {
log.Fatal(err)
}
log.Printf("Seeded %d buckets.\n", len(new_buckets))
RunServer() RunServer()
}, },
} }
@ -241,29 +253,14 @@ func handleV1Seed(c echo.Context) error {
}) })
} }
var new_buckets []string new_buckets, err := mediaClient.Seed()
if err != nil {
for _, bucket := range cfg.Server.Documents.Buckets { return c.JSON(http.StatusInternalServerError, map[string]string{
exists, err := mediaClient.MinioClient.BucketExists(context.Background(), bucket) "message": "Error during mediaClient.Seed()",
if err != nil { "error": err.Error(),
return c.JSON(http.StatusInternalServerError, map[string]string{ })
"message": "Error during minio#BucketExists",
"error": err.Error(),
})
}
if exists {
continue
}
if err = mediaClient.MinioClient.MakeBucket(context.Background(), bucket, minio.MakeBucketOptions{}); err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Error during minio#MakeBucket",
"error": err.Error(),
})
}
new_buckets = append(new_buckets, bucket)
} }
var message string var message string
if len(new_buckets) == 0 { if len(new_buckets) == 0 {
message = "All buckets already exist" message = "All buckets already exist"

View file

@ -1,6 +1,7 @@
package media package media
import ( import (
"context"
"errors" "errors"
"git.agecem.com/agecem/agecem-org/config" "git.agecem.com/agecem/agecem-org/config"
@ -49,6 +50,29 @@ type MediaClient struct {
MinioClient minio.Client MinioClient minio.Client
} }
func (m *MediaClient) foo() string { func (m *MediaClient) Seed() ([]string, error) {
return "bar" var cfg config.Config
if err := viper.Unmarshal(&cfg); err != nil {
return nil, err
}
var new_buckets []string
for _, bucket := range cfg.Server.Documents.Buckets {
exists, err := m.MinioClient.BucketExists(context.Background(), bucket)
if err != nil {
return new_buckets, err
}
if exists {
continue
}
if err = m.MinioClient.MakeBucket(context.Background(), bucket, minio.MakeBucketOptions{}); err != nil {
return new_buckets, err
}
new_buckets = append(new_buckets, bucket)
}
return new_buckets, nil
} }