-
Notifications
You must be signed in to change notification settings - Fork 785
/
file_config_handler.go
96 lines (87 loc) · 3.02 KB
/
file_config_handler.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
package auth
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"sigs.k8s.io/yaml"
)
// ServerKind indicates the server kind used to load the auth config from file
type ServerKind string
// GitServerKind indicate the server kind for git
const GitServerKind ServerKind = "git"
// NewFileAuthConfigService creates a new file config service
func NewFileAuthConfigService(filename string, serverKind string) (ConfigService, error) {
handler, err := newFileAuthConfigHandler(filename, serverKind)
return NewAuthConfigService(handler), err
}
// newFileAuthConfigHandler creates a new FileBasedAuthConfigService that stores its data under the given filename
// If the fileName is an absolute path, it will be used. If it is a simple filename, it will be stored in the default
// Config directory
func newFileAuthConfigHandler(fileName string, serverKind string) (ConfigHandler, error) {
svc := &FileAuthConfigHandler{
serverKind: serverKind,
}
// If the fileName is an absolute path, use that. Otherwise treat it as a config filename to be used in
if fileName == filepath.Base(fileName) {
dir, err := util.ConfigDir()
if err != nil {
return svc, err
}
svc.fileName = filepath.Join(dir, fileName)
} else {
svc.fileName = fileName
}
return svc, nil
}
// loadFileAuth loads the auth config from given file
func (s *FileAuthConfigHandler) loadFileAuth(fileName string) (*AuthConfig, error) {
if fileName == "" {
return nil, fmt.Errorf("empty file name for auth config")
}
exists, err := util.FileExists(fileName)
if err != nil {
return nil, fmt.Errorf("checking if the auth config file exists %s due to %s", fileName, err)
}
if !exists {
return nil, fmt.Errorf("auth config file %q does not exist", fileName)
}
data, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, errors.Wrapf(err, "loading the auth config from file %q", fileName)
}
config := &AuthConfig{}
if err := yaml.Unmarshal(data, config); err != nil {
return nil, errors.Wrapf(err, "unmarshaling the auth config YAML from file %q", fileName)
}
return config, nil
}
// LoadConfig loads the configuration from the users JX config directory
func (s *FileAuthConfigHandler) LoadConfig() (*AuthConfig, error) {
config, err := s.loadFileAuth(s.fileName)
if err != nil {
// Try to load the auth config from git credentials file
if s.serverKind == string(GitServerKind) {
gitConfig, err := loadGitCredentialsAuth()
if err != nil {
return nil, errors.Wrap(err, "loading the auth config from git credentials file")
}
return gitConfig, nil
}
return nil, errors.Wrapf(err, "loading the auth config from file %q", s.fileName)
}
return config, nil
}
// SaveConfig saves the configuration to disk
func (s *FileAuthConfigHandler) SaveConfig(config *AuthConfig) error {
fileName := s.fileName
if fileName == "" {
return fmt.Errorf("no filename defined")
}
data, err := yaml.Marshal(config)
if err != nil {
return err
}
return ioutil.WriteFile(fileName, data, util.DefaultWritePermissions)
}