-
Notifications
You must be signed in to change notification settings - Fork 343
/
local_config.go
115 lines (88 loc) · 2.61 KB
/
local_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package repo
import (
"context"
"encoding/json"
"io"
"os"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/object"
)
// ClientOptions contains client-specific options that are persisted in local configuration file.
type ClientOptions struct {
Hostname string `json:"hostname"`
Username string `json:"username"`
ReadOnly bool `json:"readonly,omitempty"`
// Description is human-readable description of the repository to use in the UI.
Description string `json:"description,omitempty"`
}
// ApplyDefaults returns a copy of ClientOptions with defaults filled out.
func (o ClientOptions) ApplyDefaults(ctx context.Context, defaultDesc string) ClientOptions {
if o.Hostname == "" {
o.Hostname = GetDefaultHostName(ctx)
}
if o.Username == "" {
o.Username = GetDefaultUserName(ctx)
}
if o.Description == "" {
o.Description = defaultDesc
}
return o
}
// Override returns ClientOptions that overrides fields present in the provided ClientOptions.
func (o ClientOptions) Override(other ClientOptions) ClientOptions {
if other.Description != "" {
o.Description = other.Description
}
if other.Hostname != "" {
o.Hostname = other.Hostname
}
if other.Username != "" {
o.Username = other.Username
}
if other.ReadOnly {
o.ReadOnly = other.ReadOnly
}
return o
}
// LocalConfig is a configuration of Kopia stored in a configuration file.
type LocalConfig struct {
// APIServer is only provided for remote repository.
APIServer *APIServerInfo `json:"apiServer,omitempty"`
// Storage is only provided for direct repository access.
Storage *blob.ConnectionInfo `json:"storage,omitempty"`
Caching *content.CachingOptions `json:"caching,omitempty"`
ClientOptions
}
// repositoryObjectFormat describes the format of objects in a repository.
type repositoryObjectFormat struct {
content.FormattingOptions
object.Format
}
// Load reads local configuration from the specified reader.
func (lc *LocalConfig) Load(r io.Reader) error {
*lc = LocalConfig{}
return json.NewDecoder(r).Decode(lc)
}
// Save writes the configuration to the specified writer.
func (lc *LocalConfig) Save(w io.Writer) error {
b, err := json.MarshalIndent(lc, "", " ")
if err != nil {
return nil
}
_, err = w.Write(b)
return err
}
// loadConfigFromFile reads the local configuration from the specified file.
func loadConfigFromFile(fileName string) (*LocalConfig, error) {
f, err := os.Open(fileName) //nolint:gosec
if err != nil {
return nil, err
}
defer f.Close() //nolint:errcheck,gosec
var lc LocalConfig
if err := lc.Load(f); err != nil {
return nil, err
}
return &lc, nil
}