Skip to content

Commit

Permalink
Move logging back into pkg so migrator can be called outside the proj…
Browse files Browse the repository at this point in the history
…ect (#16)
  • Loading branch information
MissedTheMark authored Sep 23, 2024
1 parent b4a49d7 commit 92a06f7
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/helm-migrate-values/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"github.com/octopusdeploylabs/helm-migrate-values/internal"
"github.com/octopusdeploylabs/helm-migrate-values/pkg"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
"log"
Expand All @@ -18,7 +18,7 @@ func init() {

func main() {
var actionConfig = new(action.Configuration)
logger := *internal.NewLogger(settings.Debug)
logger := *pkg.NewLogger(settings.Debug)
logger.Debug("Debug mode enabled")

cmd, err := NewRootCmd(actionConfig, settings, os.Stdout, logger)
Expand Down
4 changes: 2 additions & 2 deletions cmd/helm-migrate-values/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The command will return an error if:
- no migrations are defined in the chart
`

func NewRootCmd(actionConfig *action.Configuration, settings *cli.EnvSettings, out io.Writer, log internal.Logger) (*cobra.Command, error) {
func NewRootCmd(actionConfig *action.Configuration, settings *cli.EnvSettings, out io.Writer, log pkg.Logger) (*cobra.Command, error) {
cmd := &cobra.Command{
Use: "migrate-values [RELEASE] [CHART] [flags]",
Short: "helm migrator for values schemas",
Expand All @@ -67,7 +67,7 @@ func NewRootCmd(actionConfig *action.Configuration, settings *cli.EnvSettings, o
return cmd, nil
}

func newRunner(actionConfig *action.Configuration, flags *pflag.FlagSet, settings *cli.EnvSettings, out io.Writer, outputFile *string, log internal.Logger) func(cmd *cobra.Command, args []string) error {
func newRunner(actionConfig *action.Configuration, flags *pflag.FlagSet, settings *cli.EnvSettings, out io.Writer, outputFile *string, log pkg.Logger) func(cmd *cobra.Command, args []string) error {
// We use the install action for locating the chart
var installAction = action.NewInstall(actionConfig)
var listAction = action.NewList(actionConfig)
Expand Down
3 changes: 2 additions & 1 deletion internal/chart_pull.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"github.com/octopusdeploylabs/helm-migrate-values/pkg"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/cli"
Expand All @@ -9,7 +10,7 @@ import (
)

// Locates the chart. If this is a remote (OCI/Repo URL) it downloads the chart and extract the tgz to a temporary file
func LocateChart(chart string, client *action.Install, settings *cli.EnvSettings, log Logger) (*string, bool, error) {
func LocateChart(chart string, client *action.Install, settings *cli.EnvSettings, log pkg.Logger) (*string, bool, error) {
err := setupRegistryClient(client, settings)
if err != nil {
return nil, false, err
Expand Down
3 changes: 1 addition & 2 deletions pkg/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pkg

import (
"github.com/octopusdeploylabs/helm-migrate-values/internal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"helm.sh/helm/v3/pkg/action"
Expand Down Expand Up @@ -71,7 +70,7 @@ func migrateCharts(t *testing.T, chartV1Path, chartV2Path string) {
req.NoError(err, "Error installing chart v1")

// Migrate the release user values (config)
migratedValues, err := MigrateFromPath(rel1.Config, 1, nil, "test-charts/v2/value-migrations/", *internal.NewLogger(false))
migratedValues, err := MigrateFromPath(rel1.Config, 1, nil, "test-charts/v2/value-migrations/", *NewLogger(false))
req.NoError(err, "Error migrating values")

// Load the v2 chart
Expand Down
2 changes: 1 addition & 1 deletion internal/logging.go → pkg/logging.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package pkg

import (
"fmt"
Expand Down
5 changes: 2 additions & 3 deletions pkg/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"bytes"
"fmt"
"github.com/Masterminds/sprig/v3"
"github.com/octopusdeploylabs/helm-migrate-values/internal"
"gopkg.in/yaml.v2"
"os"
"slices"
"strings"
"text/template"
)

func MigrateFromPath(currentConfig map[string]interface{}, vFrom int, vTo *int, migrationsDir string, log internal.Logger) (map[string]interface{}, error) {
func MigrateFromPath(currentConfig map[string]interface{}, vFrom int, vTo *int, migrationsDir string, log Logger) (map[string]interface{}, error) {

if len(currentConfig) == 0 {
log.Warning("No existing user-supplied values to migrate")
Expand Down Expand Up @@ -48,7 +47,7 @@ func MigrateFromPath(currentConfig map[string]interface{}, vFrom int, vTo *int,
return Migrate(currentConfig, vFrom, vTo, mp, log)
}

func Migrate(currentConfig map[string]interface{}, vFrom int, vTo *int, mp MigrationProvider, log internal.Logger) (map[string]interface{}, error) {
func Migrate(currentConfig map[string]interface{}, vFrom int, vTo *int, mp MigrationProvider, log Logger) (map[string]interface{}, error) {

log.Debug("migrating user-supplied values")
versions := slices.Sorted(mp.GetVersions())
Expand Down
3 changes: 1 addition & 2 deletions pkg/migrator_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pkg

import (
"github.com/octopusdeploylabs/helm-migrate-values/internal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
Expand Down Expand Up @@ -77,7 +76,7 @@ func TestMigrator_MigrateAcrossVersions(t *testing.T) {

ms := loadMigrationsToVersions(tc.includeMigrationsToVersions)

migrated, err := Migrate(currentConfig, tc.currentVersion, tc.versionTo, ms, *internal.NewLogger(false))
migrated, err := Migrate(currentConfig, tc.currentVersion, tc.versionTo, ms, *NewLogger(false))
req.NoError(err)

is.EqualValues(tc.expected, migrated)
Expand Down

0 comments on commit 92a06f7

Please sign in to comment.