From be5209ae5e7043d09806bc37b376773400f9bfb1 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Tue, 16 Sep 2025 13:11:44 +0930 Subject: [PATCH 1/3] internal/profile: export profile loading and migration functions --- internal/profile/config.go | 2 +- internal/profile/migrations.go | 4 ++-- internal/profile/migrations_test.go | 8 ++++---- internal/profile/profile.go | 20 ++++++++++---------- internal/profile/profile_json.go | 2 +- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/internal/profile/config.go b/internal/profile/config.go index 2f3ed50ec3..c4c3edd862 100644 --- a/internal/profile/config.go +++ b/internal/profile/config.go @@ -15,7 +15,7 @@ import ( "github.com/elastic/elastic-package/internal/common" ) -const currentVersion = 1 +const CurrentVersion = 1 type config struct { settings common.MapStr diff --git a/internal/profile/migrations.go b/internal/profile/migrations.go index 49bd5e96c7..6b967f10c4 100644 --- a/internal/profile/migrations.go +++ b/internal/profile/migrations.go @@ -13,7 +13,7 @@ import ( "github.com/elastic/go-resource" ) -func (p *Profile) migrate(version uint) error { +func (p *Profile) Migrate(version uint) error { resourceManager := resource.NewManager() resourceManager.AddFacter(resource.StaticFacter{ "creation_date": p.metadata.DateCreated.Format(dateFormat), @@ -81,7 +81,7 @@ func (v *profileVersioner) Current() uint { } func (p *profileVersioner) Set(version uint) error { - if version != currentVersion { + if version != CurrentVersion { return fmt.Errorf("cannot set metadata version distinct to current version, found %d", version) } _, err := p.manager.Apply(resource.Resources{ diff --git a/internal/profile/migrations_test.go b/internal/profile/migrations_test.go index 2a9fd5e700..ec6e89a1e1 100644 --- a/internal/profile/migrations_test.go +++ b/internal/profile/migrations_test.go @@ -29,7 +29,7 @@ func TestMigrationsFromLegacy(t *testing.T) { assert.FileExists(t, filepath.Join(profilesDirPath, profileName, "stack", "snapshot.yml")) assert.NoFileExists(t, filepath.Join(profilesDirPath, profileName, "stack", "docker-compose.yml")) - profile, err := loadProfile(profilesDirPath, profileName) + profile, err := LoadProfileFrom(profilesDirPath, profileName) t.Log(homeDir, profileName) require.NoError(t, err) @@ -42,11 +42,11 @@ func TestMigrationsFromLegacy(t *testing.T) { } assert.Equal(t, expectedMeta, profile.metadata) - err = profile.migrate(currentVersion) + err = profile.Migrate(CurrentVersion) require.NoError(t, err) // Check that the in-memory profile is updated. - expectedMeta.Version = strconv.Itoa(currentVersion) + expectedMeta.Version = strconv.Itoa(CurrentVersion) assert.Equal(t, expectedMeta, profile.metadata) // Check some file that has been moved. @@ -54,7 +54,7 @@ func TestMigrationsFromLegacy(t *testing.T) { assert.FileExists(t, filepath.Join(profilesDirPath, profileName, "stack", "docker-compose.yml")) // Load it again to check that it is updated on disk too. - profile, err = loadProfile(profilesDirPath, profileName) + profile, err = LoadProfileFrom(profilesDirPath, profileName) require.NoError(t, err) assert.Equal(t, expectedMeta, profile.metadata) } diff --git a/internal/profile/profile.go b/internal/profile/profile.go index 5b7347dd4e..803eb250f1 100644 --- a/internal/profile/profile.go +++ b/internal/profile/profile.go @@ -71,7 +71,7 @@ func CreateProfile(options Options) error { } if !options.OverwriteExisting { - _, err := loadProfile(options.ProfilesDirPath, options.Name) + _, err := LoadProfileFrom(options.ProfilesDirPath, options.Name) if err == nil { return fmt.Errorf("profile %q already exists", options.Name) } @@ -218,7 +218,7 @@ func FetchAllProfiles(profilesDirPath string) ([]Metadata, error) { if !item.IsDir() { continue } - profile, err := loadProfile(profilesDirPath, item.Name()) + profile, err := LoadProfileFrom(profilesDirPath, item.Name()) if errors.Is(err, ErrNotAProfile) { continue } @@ -237,22 +237,22 @@ func LoadProfile(profileName string) (*Profile, error) { return nil, fmt.Errorf("error finding stack dir location: %w", err) } - profile, err := loadProfile(loc.ProfileDir(), profileName) + profile, err := LoadProfileFrom(loc.ProfileDir(), profileName) if err != nil { return nil, err } - err = profile.migrate(currentVersion) + err = profile.Migrate(CurrentVersion) if err != nil { - return nil, fmt.Errorf("error migrating profile to version %v: %w", currentVersion, err) + return nil, fmt.Errorf("error migrating profile to version %v: %w", CurrentVersion, err) } return profile, nil } -// loadProfile loads an existing profile -func loadProfile(profilesDirPath string, profileName string) (*Profile, error) { - profilePath := filepath.Join(profilesDirPath, profileName) +// LoadProfileFrom loads an existing profile from a provided directory. +func LoadProfileFrom(dir, name string) (*Profile, error) { + profilePath := filepath.Join(dir, name) metadata, err := loadProfileMetadata(filepath.Join(profilePath, PackageProfileMetaFile)) if errors.Is(err, os.ErrNotExist) { @@ -265,11 +265,11 @@ func loadProfile(profilesDirPath string, profileName string) (*Profile, error) { configPath := filepath.Join(profilePath, PackageProfileConfigFile) config, err := loadProfileConfig(configPath) if err != nil { - return nil, fmt.Errorf("error loading configuration for profile %q: %w", profileName, err) + return nil, fmt.Errorf("error loading configuration for profile %q: %w", name, err) } profile := Profile{ - ProfileName: profileName, + ProfileName: name, ProfilePath: profilePath, config: config, metadata: metadata, diff --git a/internal/profile/profile_json.go b/internal/profile/profile_json.go index 1129c8c6d8..4430299086 100644 --- a/internal/profile/profile_json.go +++ b/internal/profile/profile_json.go @@ -42,7 +42,7 @@ func profileMetadataContent(applyCtx resource.Context, w io.Writer) error { profileData := Metadata{ Name: profileName, DateCreated: creationDate, - Version: strconv.Itoa(currentVersion), + Version: strconv.Itoa(CurrentVersion), } enc := json.NewEncoder(w) From 6103f03b4f288f6a2c1c4903f7ad25cfacaf5e79 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 18 Sep 2025 08:10:14 +0930 Subject: [PATCH 2/3] Revert "internal/profile: export profile loading and migration functions" This reverts commit be5209ae5e7043d09806bc37b376773400f9bfb1. --- internal/profile/config.go | 2 +- internal/profile/migrations.go | 4 ++-- internal/profile/migrations_test.go | 8 ++++---- internal/profile/profile.go | 20 ++++++++++---------- internal/profile/profile_json.go | 2 +- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/internal/profile/config.go b/internal/profile/config.go index c4c3edd862..2f3ed50ec3 100644 --- a/internal/profile/config.go +++ b/internal/profile/config.go @@ -15,7 +15,7 @@ import ( "github.com/elastic/elastic-package/internal/common" ) -const CurrentVersion = 1 +const currentVersion = 1 type config struct { settings common.MapStr diff --git a/internal/profile/migrations.go b/internal/profile/migrations.go index 6b967f10c4..49bd5e96c7 100644 --- a/internal/profile/migrations.go +++ b/internal/profile/migrations.go @@ -13,7 +13,7 @@ import ( "github.com/elastic/go-resource" ) -func (p *Profile) Migrate(version uint) error { +func (p *Profile) migrate(version uint) error { resourceManager := resource.NewManager() resourceManager.AddFacter(resource.StaticFacter{ "creation_date": p.metadata.DateCreated.Format(dateFormat), @@ -81,7 +81,7 @@ func (v *profileVersioner) Current() uint { } func (p *profileVersioner) Set(version uint) error { - if version != CurrentVersion { + if version != currentVersion { return fmt.Errorf("cannot set metadata version distinct to current version, found %d", version) } _, err := p.manager.Apply(resource.Resources{ diff --git a/internal/profile/migrations_test.go b/internal/profile/migrations_test.go index ec6e89a1e1..2a9fd5e700 100644 --- a/internal/profile/migrations_test.go +++ b/internal/profile/migrations_test.go @@ -29,7 +29,7 @@ func TestMigrationsFromLegacy(t *testing.T) { assert.FileExists(t, filepath.Join(profilesDirPath, profileName, "stack", "snapshot.yml")) assert.NoFileExists(t, filepath.Join(profilesDirPath, profileName, "stack", "docker-compose.yml")) - profile, err := LoadProfileFrom(profilesDirPath, profileName) + profile, err := loadProfile(profilesDirPath, profileName) t.Log(homeDir, profileName) require.NoError(t, err) @@ -42,11 +42,11 @@ func TestMigrationsFromLegacy(t *testing.T) { } assert.Equal(t, expectedMeta, profile.metadata) - err = profile.Migrate(CurrentVersion) + err = profile.migrate(currentVersion) require.NoError(t, err) // Check that the in-memory profile is updated. - expectedMeta.Version = strconv.Itoa(CurrentVersion) + expectedMeta.Version = strconv.Itoa(currentVersion) assert.Equal(t, expectedMeta, profile.metadata) // Check some file that has been moved. @@ -54,7 +54,7 @@ func TestMigrationsFromLegacy(t *testing.T) { assert.FileExists(t, filepath.Join(profilesDirPath, profileName, "stack", "docker-compose.yml")) // Load it again to check that it is updated on disk too. - profile, err = LoadProfileFrom(profilesDirPath, profileName) + profile, err = loadProfile(profilesDirPath, profileName) require.NoError(t, err) assert.Equal(t, expectedMeta, profile.metadata) } diff --git a/internal/profile/profile.go b/internal/profile/profile.go index 803eb250f1..5b7347dd4e 100644 --- a/internal/profile/profile.go +++ b/internal/profile/profile.go @@ -71,7 +71,7 @@ func CreateProfile(options Options) error { } if !options.OverwriteExisting { - _, err := LoadProfileFrom(options.ProfilesDirPath, options.Name) + _, err := loadProfile(options.ProfilesDirPath, options.Name) if err == nil { return fmt.Errorf("profile %q already exists", options.Name) } @@ -218,7 +218,7 @@ func FetchAllProfiles(profilesDirPath string) ([]Metadata, error) { if !item.IsDir() { continue } - profile, err := LoadProfileFrom(profilesDirPath, item.Name()) + profile, err := loadProfile(profilesDirPath, item.Name()) if errors.Is(err, ErrNotAProfile) { continue } @@ -237,22 +237,22 @@ func LoadProfile(profileName string) (*Profile, error) { return nil, fmt.Errorf("error finding stack dir location: %w", err) } - profile, err := LoadProfileFrom(loc.ProfileDir(), profileName) + profile, err := loadProfile(loc.ProfileDir(), profileName) if err != nil { return nil, err } - err = profile.Migrate(CurrentVersion) + err = profile.migrate(currentVersion) if err != nil { - return nil, fmt.Errorf("error migrating profile to version %v: %w", CurrentVersion, err) + return nil, fmt.Errorf("error migrating profile to version %v: %w", currentVersion, err) } return profile, nil } -// LoadProfileFrom loads an existing profile from a provided directory. -func LoadProfileFrom(dir, name string) (*Profile, error) { - profilePath := filepath.Join(dir, name) +// loadProfile loads an existing profile +func loadProfile(profilesDirPath string, profileName string) (*Profile, error) { + profilePath := filepath.Join(profilesDirPath, profileName) metadata, err := loadProfileMetadata(filepath.Join(profilePath, PackageProfileMetaFile)) if errors.Is(err, os.ErrNotExist) { @@ -265,11 +265,11 @@ func LoadProfileFrom(dir, name string) (*Profile, error) { configPath := filepath.Join(profilePath, PackageProfileConfigFile) config, err := loadProfileConfig(configPath) if err != nil { - return nil, fmt.Errorf("error loading configuration for profile %q: %w", name, err) + return nil, fmt.Errorf("error loading configuration for profile %q: %w", profileName, err) } profile := Profile{ - ProfileName: name, + ProfileName: profileName, ProfilePath: profilePath, config: config, metadata: metadata, diff --git a/internal/profile/profile_json.go b/internal/profile/profile_json.go index 4430299086..1129c8c6d8 100644 --- a/internal/profile/profile_json.go +++ b/internal/profile/profile_json.go @@ -42,7 +42,7 @@ func profileMetadataContent(applyCtx resource.Context, w io.Writer) error { profileData := Metadata{ Name: profileName, DateCreated: creationDate, - Version: strconv.Itoa(CurrentVersion), + Version: strconv.Itoa(currentVersion), } enc := json.NewEncoder(w) From d45bb61021259c139d7adebc97be95eca39ce957 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 18 Sep 2025 08:15:29 +0930 Subject: [PATCH 3/3] change --- internal/profile/profile.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/profile/profile.go b/internal/profile/profile.go index 5b7347dd4e..05fc51d1c6 100644 --- a/internal/profile/profile.go +++ b/internal/profile/profile.go @@ -237,7 +237,12 @@ func LoadProfile(profileName string) (*Profile, error) { return nil, fmt.Errorf("error finding stack dir location: %w", err) } - profile, err := loadProfile(loc.ProfileDir(), profileName) + return LoadProfileFrom(loc.ProfileDir(), profileName) +} + +// LoadProfile loads an existing profile from the provided directory. +func LoadProfileFrom(dir, profileName string) (*Profile, error) { + profile, err := loadProfile(dir, profileName) if err != nil { return nil, err }