Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
petersutter committed Dec 9, 2021
1 parent 284af9c commit 2e180a5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
4 changes: 1 addition & 3 deletions cmd/get_client_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ func init() {
klog.Errorf("could not determine home directory %v", err)
}

f := &util.FactoryImpl{
HomeDirectory: dir,
}
f := util.NewFactory(dir)

getClientCertificateCmd = NewCmdGetClientCertificate(f, ioStreams)

Expand Down
48 changes: 37 additions & 11 deletions internal/cmd/util/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ SPDX-License-Identifier: Apache-2.0
package util

import (
"fmt"

"github.com/gardener/gardenlogin/internal/certificatecache/store"

gardencoreclientset "github.com/gardener/gardener/pkg/client/core/clientset/versioned"
Expand All @@ -31,22 +33,31 @@ type Factory interface {
CertificateStore(dir string) store.Interface
}

// FactoryImpl implements util.Factory interface
type FactoryImpl struct {
HomeDirectory string
// factoryImpl implements util.Factory interface
type factoryImpl struct {
homeDirectory string

config *Config
}

var _ Factory = &FactoryImpl{}
var _ Factory = &factoryImpl{}

// NewFactory returns a new util.Factory.
func NewFactory(homeDirectory string) Factory {
return &factoryImpl{
homeDirectory: homeDirectory,
}
}

// Clock returns a clock that provides access to the current time.
func (f *FactoryImpl) Clock() Clock {
func (f *factoryImpl) Clock() Clock {
return &RealClock{}
}

// RESTClient returns the rest client for the garden cluster, identified by the garden cluster identity
func (f *FactoryImpl) RESTClient(gardenClusterIdentity string) (rest.Interface, error) {
config := &Config{}
if err := viper.Unmarshal(config); err != nil {
func (f *factoryImpl) RESTClient(gardenClusterIdentity string) (rest.Interface, error) {
config, err := f.getConfig()
if err != nil {
return nil, err
}

Expand Down Expand Up @@ -76,11 +87,26 @@ func (f *FactoryImpl) RESTClient(gardenClusterIdentity string) (rest.Interface,
}

// HomeDir returns the home directory for the executing user.
func (f *FactoryImpl) HomeDir() string {
return f.HomeDirectory
func (f *factoryImpl) HomeDir() string {
return f.homeDirectory
}

// CertificateStore returns a certificate store
func (f *FactoryImpl) CertificateStore(dir string) store.Interface {
func (f *factoryImpl) CertificateStore(dir string) store.Interface {
return &store.Store{Dir: dir}
}

func (f *factoryImpl) getConfig() (*Config, error) {
if f.config != nil {
return f.config, nil
}

config := &Config{}
if err := viper.Unmarshal(config); err != nil {
return nil, fmt.Errorf("failed to unmarshal viper config: %w", err)
}

f.config = config

return config, nil
}

0 comments on commit 2e180a5

Please sign in to comment.