Skip to content

Commit

Permalink
only depricate NewFor
Browse files Browse the repository at this point in the history
Signed-off-by: robinbraemer <[email protected]>
  • Loading branch information
robinbraemer committed Oct 18, 2024
1 parent b89898c commit 437748b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
6 changes: 1 addition & 5 deletions cmd/porter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ const (

func main() {
run := func() int {
p, err := porter.NewWith(porter.Options{})
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(cli.ExitCodeErr)
}
p := porter.New()

ctx, cancel := handleInterrupt(context.Background(), p)
defer cancel()
Expand Down
5 changes: 2 additions & 3 deletions cmd/porter/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import (
"get.porter.sh/porter/pkg/porter"
)

func buildRootCommand(t *testing.T) *cobra.Command {
p, err := porter.NewWith(porter.Options{})
require.NoError(t, err)
func buildRootCommand(_ *testing.T) *cobra.Command {
p := porter.New()
return buildRootCommandFrom(p)
}

Expand Down
51 changes: 23 additions & 28 deletions pkg/porter/porter.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,45 +63,30 @@ type Porter struct {

// Options for configuring a new Porter client passed to NewWith.
type Options struct {
Config *config.Config // Optional. Defaults to a config.New.
Secrets secrets.Store // Optional. Defaults to a secrets.NewPluginAdapter(secretsplugin.NewStore).
Signer signing.Signer // Optional. Defaults to a signing.NewPluginAdapter(signingplugin.NewSigner).
Config *config.Config // Optional. Defaults to a config.New.
Secrets secrets.Store // Optional. Defaults to a secrets.NewPluginAdapter(secretsplugin.NewStore).
Signer signing.Signer // Optional. Defaults to a signing.NewPluginAdapter(signingplugin.NewSigner).
Installations storage.InstallationProvider // Optional. Defaults to a storage.NewInstallationStore for MongoDB or storage.NewInstallationStoreSQL for SQL.
Credentials storage.CredentialSetProvider // Optional. Defaults to a storage.NewCredentialStore for MongoDB or storage.NewCredentialStoreSQL for SQL.
Parameters storage.ParameterSetProvider // Optional. Defaults to a storage.NewParameterStore for MongoDB or storage.NewParameterStoreSQL for SQL.
Sanitizer *storage.Sanitizer // Optional. Defaults to a storage.NewSanitizer for MongoDB or storage.NewSanitizer for SQL.
}

// NewWith creates a new Porter client with useful defaults that can be overridden with the provided options.
//
// Porter.Connect must be called before using the Porter client and Porter.Close must be called when done.
func NewWith(opt Options) (*Porter, error) {
if opt.Config == nil {
opt.Config = config.New()
}
if opt.Secrets == nil {
opt.Secrets = secrets.NewPluginAdapter(secretsplugin.NewStore(opt.Config))
}
if opt.Signer == nil {
opt.Signer = signing.NewPluginAdapter(signingplugin.NewSigner(opt.Config))
}

// storage initialization is deferred until Connect is called where we certainly have the config loaded
return newWith(opt.Config, nil, nil, nil, opt.Secrets, opt.Signer, nil, nil), nil
func NewWith(opts Options) *Porter {
return newWith(opts.Config, opts.Installations, opts.Credentials, opts.Parameters, opts.Secrets, opts.Signer, opts.Sanitizer, nil)
}

// New porter client, initialized with useful defaults.
//
// Deprecated: Use NewWith instead. New does not support SQL storage backends.
// Use NewWith to override the defaults.
func New() *Porter {
c := config.New()

secretStorage := secrets.NewPluginAdapter(secretsplugin.NewStore(c))
signer := signing.NewPluginAdapter(signingplugin.NewSigner(c))

storage := storage.NewPluginAdapter(storageplugin.NewStore(c))
return NewFor(c, storage, secretStorage, signer)
// differ full initialization to Connect
return newWith(nil, nil, nil, nil, nil, nil, nil, nil)
}

// NewFor creates a new Porter client with the provided configuration and storage backend.
//
// Deprecated: Use NewWith instead. NewFor does not support SQL storage backends.
// Deprecated: Use NewWith instead. NewFor does not support newer sql storage backends.
func NewFor(
c *config.Config,
store storage.Store,
Expand Down Expand Up @@ -129,6 +114,16 @@ func newWith(
sanitizerService *storage.Sanitizer,
storageManager storage.Provider,
) *Porter {
if c == nil {
c = config.New()
}
if secretStorage == nil {
secretStorage = secrets.NewPluginAdapter(secretsplugin.NewStore(c))
}
if signer == nil {
signer = signing.NewPluginAdapter(signingplugin.NewSigner(c))
}

var cnab cnabprovider.CNABProvider
if installationStorage != nil && credStorage != nil && paramStorage != nil && secretStorage != nil && sanitizerService != nil {
cnab = cnabprovider.NewRuntime(c, installationStorage, credStorage, paramStorage, secretStorage, sanitizerService)
Expand Down

0 comments on commit 437748b

Please sign in to comment.