Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions tools/oprt2/pkg/attunehooks/gpg/archive/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2025 Gravitational, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package archive

import (
"context"
"fmt"
"log/slog"

"github.com/gravitational/shared-workflows/tools/oprt2/pkg/config"
)

// FromConfig creates a new Provider instance from the provided config.
func FromConfig(ctx context.Context, config *config.GPGArchiveProvider, logger *slog.Logger) (*Provider, error) {
Comment thread
fheinecke marked this conversation as resolved.
opts := make([]ProviderOption, 0, 2)

if config.KeyID != nil {
Comment thread
fheinecke marked this conversation as resolved.
opts = append(opts, WithKeyID(*config.KeyID))
}

opts = append(opts, WithLogger(logger))

provider, err := NewProvider(ctx, config.Archive, opts...)
if err != nil {
return nil, fmt.Errorf("failed to create GPG archive provider: %w", err)
}

return provider, nil
}
44 changes: 44 additions & 0 deletions tools/oprt2/pkg/attunehooks/gpg/archive/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2025 Gravitational, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package archive

import (
"log/slog"

"github.com/gravitational/shared-workflows/tools/oprt2/pkg/logging"
)

// ProviderOption provides optional configuration to the Provider.
type ProviderOption func(p *Provider)

// WithKeyID sets the key ID to provide Attune. If not set and only one key exists in the
// archive, Attune will attempt to use it. Required when multiple keys exist in the archive.
func WithKeyID(keyID string) ProviderOption {
return func(p *Provider) {
p.keyID = keyID
}
}

// WithLogger configures the package manager with the provided logger.
func WithLogger(logger *slog.Logger) ProviderOption {
return func(p *Provider) {
if logger == nil {
logger = logging.DiscardLogger
}
p.logger = logger
}
}
67 changes: 67 additions & 0 deletions tools/oprt2/pkg/attunehooks/gpg/archive/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2025 Gravitational, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package archive

import (
"io"
"log/slog"
"testing"

"github.com/gravitational/shared-workflows/tools/oprt2/pkg/logging"
"github.com/stretchr/testify/assert"
)

func TestWithLogger(t *testing.T) {
testLogger := slog.New(slog.NewTextHandler(io.Discard, nil))

tests := []struct {
name string
providedLogger *slog.Logger
expectedLogger *slog.Logger
}{
{
name: "with nil logger",
expectedLogger: logging.DiscardLogger,
},
{
name: "with new logger",
providedLogger: testLogger,
expectedLogger: testLogger,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
provider := &Provider{}

opt := WithLogger(tt.providedLogger)
opt(provider)

assert.EqualValues(t, tt.expectedLogger, provider.logger)
})
}
}

func TestWithKeyID(t *testing.T) {
provider := &Provider{}
keyID := "test key ID"

opt := WithKeyID(keyID)
opt(provider)

assert.Equal(t, keyID, provider.keyID)
}
Loading
Loading