Skip to content

Commit

Permalink
Config package resolve comment
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
  • Loading branch information
JeyJeyGao committed Aug 2, 2022
1 parent 730bd31 commit 08d6a49
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 281 deletions.
50 changes: 4 additions & 46 deletions config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"os"
"path/filepath"
"sync"

"github.com/notaryproject/notation-go/dir"
)
Expand All @@ -14,56 +13,15 @@ var (
ConfigPath string
// SigningKeysPath is the path for signingkeys.json
SigningKeysPath string

// configInfo is the information of config.json
configInfo *Config
configInfoOnce sync.Once

// signingKeysInfo is the information of signingkeys.json
signingKeysInfo *SigningKeys
signingKeysInfoOnce sync.Once
)

func init() {
ConfigPath = dir.Path.Config()
SigningKeysPath = dir.Path.SigningKeyConfig()
}

// Configuration is the main config struct of notation-go
type Configuration struct {
Config
SigningKeys
}

// Save stores sub-configurations to files
func (c *Configuration) Save() error {
if err := c.Config.Save(); err != nil {
return err
}
return c.SigningKeys.Save()
}

// LoadOnce returns the previously read config file.
// If previous config file does not exist, it reads the config from file
// or return a default config if not found.
// The returned config is only suitable for read only scenarios for short-lived processes.
func LoadOnce() (*Configuration, error) {
configInfo, err := loadConfigOnce()
if err != nil {
return nil, err
}
signingKeysInfo, err := loadSigningKeysOnce()
if err != nil {
return nil, err
}
return &Configuration{
Config: *configInfo,
SigningKeys: *signingKeysInfo,
}, nil
}

// Save stores the cfg struct to file
func Save(filePath string, cfg interface{}) error {
// save stores the cfg struct to file
func save(filePath string, cfg interface{}) error {
dir := filepath.Dir(filePath)
if err := os.MkdirAll(dir, 0700); err != nil {
return err
Expand All @@ -78,8 +36,8 @@ func Save(filePath string, cfg interface{}) error {
return encoder.Encode(cfg)
}

// Load reads file, parses json and stores in cfg struct
func Load(filePath string, cfg interface{}) error {
// load reads file, parses json and stores in cfg struct
func load(filePath string, cfg interface{}) error {
file, err := os.Open(filePath)
if err != nil {
return err
Expand Down
56 changes: 0 additions & 56 deletions config/base_test.go

This file was deleted.

22 changes: 5 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (c CertificateReference) Is(name string) bool {
return c.Name == name
}

// Config reflects the config file.
// Config reflects the config.json file.
// Specification: https://github.com/notaryproject/notation/pull/76
type Config struct {
VerificationCertificates VerificationCertificates `json:"verificationCerts"`
Expand All @@ -39,13 +39,13 @@ func NewConfig() *Config {

// Save stores the config to file
func (c *Config) Save() error {
return Save(ConfigPath, c)
return save(ConfigPath, c)
}

// loadConfig reads the config from file or return a default config if not found.
func loadConfig() (*Config, error) {
// LoadConfig reads the config from file or return a default config if not found.
func LoadConfig() (*Config, error) {
var config Config
err := Load(ConfigPath, &config)
err := load(ConfigPath, &config)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return NewConfig(), nil
Expand All @@ -54,15 +54,3 @@ func loadConfig() (*Config, error) {
}
return &config, nil
}

// loadConfigOnce returns the previously read config file.
// If previous config file does not exist, it reads the config from file
// or return a default config if not found.
// The returned config is only suitable for read only scenarios for short-lived processes.
func loadConfigOnce() (*Config, error) {
var err error
configInfoOnce.Do(func() {
configInfo, err = loadConfig()
})
return configInfo, err
}
4 changes: 2 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestLoadFile(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ConfigPath = tt.args.filePath
got, err := loadConfig()
got, err := LoadConfig()
if (err != nil) != tt.wantErr {
t.Errorf("loadFile() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -81,7 +81,7 @@ func TestSaveFile(t *testing.T) {
root := t.TempDir()
ConfigPath = filepath.Join(root, "config.json")
sampleConfig.Save()
config, err := loadConfig()
config, err := LoadConfig()
if err != nil {
t.Fatal("Load config file from temp dir failed")
}
Expand Down
22 changes: 5 additions & 17 deletions config/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ func (k KeySuite) Is(name string) bool {
return k.Name == name
}

// SigningKeys is a collection of signing keys.
// SigningKeys reflects the signingkeys.json file.
type SigningKeys struct {
Default string `json:"default"`
Keys []KeySuite `json:"keys"`
}

// Save config to file
func (s *SigningKeys) Save() error {
return Save(SigningKeysPath, s)
return save(SigningKeysPath, s)
}

// NewSigningKeys creates a new signingkeys config file
func NewSigningKeys() *SigningKeys {
return &SigningKeys{Keys: []KeySuite{}}
}

// loadSigningKeys reads the config from file
// LoadSigningKeys reads the config from file
// or return a default config if not found.
func loadSigningKeys() (*SigningKeys, error) {
func LoadSigningKeys() (*SigningKeys, error) {
var config SigningKeys
err := Load(SigningKeysPath, &config)
err := load(SigningKeysPath, &config)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return NewSigningKeys(), nil
Expand All @@ -61,15 +61,3 @@ func loadSigningKeys() (*SigningKeys, error) {
}
return &config, nil
}

// loadSigningKeysOnce returns the previously read config file.
// If previous config file does not exist, it reads the config from file
// or return a default config if not found.
// The returned config is only suitable for read only scenarios for short-lived processes.
func loadSigningKeysOnce() (*SigningKeys, error) {
var err error
signingKeysInfoOnce.Do(func() {
signingKeysInfo, err = loadSigningKeys()
})
return signingKeysInfo, err
}
4 changes: 2 additions & 2 deletions config/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestLoadSigningKeysInfo(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SigningKeysPath = tt.args.filePath
got, err := loadSigningKeys()
got, err := LoadSigningKeys()
if err != nil {
t.Errorf("LoadSigningKeysInfo() error = %v", err)
return
Expand All @@ -90,7 +90,7 @@ func TestSaveSigningKeys(t *testing.T) {
root := t.TempDir()
SigningKeysPath = filepath.Join(root, "signingkeys.json")
sampleSigningKeysInfo.Save()
info, err := loadSigningKeys()
info, err := LoadSigningKeys()
if err != nil {
t.Fatal("Load signingkeys.json from temp dir failed.")
}
Expand Down
43 changes: 0 additions & 43 deletions config/util.go

This file was deleted.

89 changes: 0 additions & 89 deletions config/util_test.go

This file was deleted.

Loading

0 comments on commit 08d6a49

Please sign in to comment.