MCPcopy Create free account
hub / github.com/Driver-C/tryssh / LoadConfigFromPath

Function LoadConfigFromPath

pkg/config/loader.go:18–44  ·  view source on GitHub ↗

LoadConfigFromPath loads the configuration from the specified paths, generating a new config file if one does not exist.

(configPath, knownHostsPath string)

Source from the content-addressed store, hash-verified

16// LoadConfigFromPath loads the configuration from the specified paths,
17// generating a new config file if one does not exist.
18func LoadConfigFromPath(configPath, knownHostsPath string) (*MainConfig, error) {
19 c := new(MainConfig)
20
21 if utils.CheckFileIsExist(configPath) {
22 conf, readErr := os.ReadFile(configPath) //nolint:gosec // G304: path is from known config constant
23 if readErr != nil {
24 return nil, fmt.Errorf("configuration file load failed: %w", readErr)
25 }
26 if unmarshalErr := yaml.Unmarshal(conf, c); unmarshalErr != nil {
27 return nil, fmt.Errorf("configuration file parsing failed: %w", unmarshalErr)
28 }
29 if err := decryptConfig(c); err != nil {
30 return nil, fmt.Errorf("configuration decryption failed: %w", err)
31 }
32 } else {
33 if genErr := generateConfig(configPath); genErr != nil {
34 return nil, genErr
35 }
36 }
37
38 if !utils.CheckFileIsExist(knownHostsPath) {
39 if createErr := utils.CreateFile(knownHostsPath, 0600); createErr != nil {
40 return nil, fmt.Errorf("the known_hosts file creation failed: %w", createErr)
41 }
42 }
43 return c, nil
44}
45
46func generateConfig(configPath string) error {
47 if err := utils.FileYamlMarshalAndWrite(configPath, &MainConfig{}); err != nil {

Calls 4

CheckFileIsExistFunction · 0.92
CreateFileFunction · 0.92
decryptConfigFunction · 0.85
generateConfigFunction · 0.85