| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package config provides the ability to load and save config.json and |
| 15 |
// signingkeys.json. |
| 16 |
package config |
| 17 |
|
| 18 |
import ( |
| 19 |
"errors" |
| 20 |
"io/fs" |
| 21 |
|
| 22 |
"github.com/notaryproject/notation-go/dir" |
| 23 |
) |
| 24 |
|
| 25 |
// Config reflects the config.json file. |
| 26 |
// Specification: https://github.com/notaryproject/notation/pull/76 |
| 27 |
type Config struct { |
| 28 |
InsecureRegistries []string `json:"insecureRegistries"` |
| 29 |
CredentialsStore string `json:"credsStore,omitempty"` |
| 30 |
CredentialHelpers map[string]string `json:"credHelpers,omitempty"` |
| 31 |
// SignatureFormat defines the signature envelope type for signing |
| 32 |
SignatureFormat string `json:"signatureFormat,omitempty"` |
| 33 |
} |
| 34 |
|
| 35 |
// NewConfig creates a new config file |
| 36 |
func NewConfig() *Config { |
| 37 |
return &Config{} |
| 38 |
} |
| 39 |
|
| 40 |
// Save stores the config to file |
| 41 |
func (c *Config) Save() error { |
| 42 |
path, err := dir.ConfigFS().SysPath(dir.PathConfigFile) |
| 43 |
if err != nil { |
| 44 |
return err |
| 45 |
} |
| 46 |
return save(path, c) |
| 47 |
} |
| 48 |
|
| 49 |
// LoadConfig reads the config from file or return a default config if not found. |
| 50 |
func LoadConfig() (*Config, error) { |
| 51 |
var config Config |
| 52 |
|
| 53 |
err := load(dir.PathConfigFile, &config) |
| 54 |
if err != nil { |
| 55 |
if errors.Is(err, fs.ErrNotExist) { |
| 56 |
return NewConfig(), nil |
| 57 |
} |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
return &config, nil |
| 61 |
} |