github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/cli/app_metadata_migration.go (about) 1 package cli 2 3 import ( 4 "context" 5 6 "github.com/hashicorp/go-multierror" 7 "github.com/pyroscope-io/pyroscope/pkg/model/appmetadata" 8 "github.com/sirupsen/logrus" 9 ) 10 11 type AppNamesGetter interface { 12 GetAppNames(ctx context.Context) []string 13 } 14 15 type AppMetadataSaver interface { 16 CreateOrUpdate(_ context.Context, _ appmetadata.ApplicationMetadata) error 17 List(context.Context) ([]appmetadata.ApplicationMetadata, error) 18 } 19 20 type AppMetadataMigrator struct { 21 logger *logrus.Logger 22 appNamesGetter AppNamesGetter 23 appMetadataSaver AppMetadataSaver 24 } 25 26 func NewAppMetadataMigrator(logger *logrus.Logger, appNamesGetter AppNamesGetter, appMetadataSaver AppMetadataSaver) *AppMetadataMigrator { 27 return &AppMetadataMigrator{ 28 logger: logger, 29 appNamesGetter: appNamesGetter, 30 appMetadataSaver: appMetadataSaver, 31 } 32 } 33 34 // Migrate creates Applications given a list of app names 35 func (m *AppMetadataMigrator) Migrate() (err error) { 36 ctx := context.Background() 37 38 // Get all app names 39 appNamesFromOrigin := m.appNamesGetter.GetAppNames(ctx) 40 apps, err := m.appMetadataSaver.List(ctx) 41 if err != nil { 42 return err 43 } 44 45 // TODO skip if not necessary 46 47 // Convert slice -> map 48 appMap := make(map[string]appmetadata.ApplicationMetadata) 49 for _, a := range apps { 50 appMap[a.FQName] = a 51 } 52 53 // If they don't exist already 54 for _, a := range appNamesFromOrigin { 55 if _, ok := appMap[a]; !ok { 56 logrus.Info("Migrating app: ", a) 57 // Write to MetadataSaver 58 saveErr := m.appMetadataSaver.CreateOrUpdate(ctx, appmetadata.ApplicationMetadata{ 59 FQName: a, 60 }) 61 if err != nil { 62 err = multierror.Append(err, saveErr) 63 } 64 } 65 } 66 67 return err 68 }