MCPcopy
hub / github.com/spacecloud-io/space-cloud / StoreCredentials

Function StoreCredentials

space-cli/cmd/utils/creds.go:36–75  ·  view source on GitHub ↗

StoreCredentials stores the credential in the accounts config file

(account *model.Account)

Source from the content-addressed store, hash-verified

34
35// StoreCredentials stores the credential in the accounts config file
36func StoreCredentials(account *model.Account) error {
37 yamlFile, err := file.File.ReadFile(getAccountConfigPath())
38 if err != nil {
39 // accounts.yaml file doesn't exist create new one
40 credential := model.Credential{
41 Accounts: []*model.Account{account},
42 SelectedAccount: account.ID,
43 }
44 if err := GenerateAccountsFile(&credential); err != nil {
45 logrus.Errorf("error in checking credentials unable to create accounts yaml file - %v", err)
46 return err
47 }
48 return nil
49 }
50 // file already exists, read data from accounts.yaml file
51 credential := new(model.Credential)
52 if err := yaml.Unmarshal(yamlFile, credential); err != nil {
53 return err
54 }
55 for _, val := range credential.Accounts {
56 // update account if already exists
57 if val.ID == account.ID {
58 val.ID, val.UserName, val.Key, val.ServerURL, val.DefaultProject = account.ID, account.UserName, account.Key, account.ServerURL, account.DefaultProject
59 credential.SelectedAccount = account.ID
60 if err := GenerateAccountsFile(credential); err != nil {
61 logrus.Errorf("error in checking credentials unable to update accounts yaml file - %v", err)
62 return err
63 }
64 return nil
65 }
66 }
67 // add new account to already existing accounts.yaml file
68 credential.Accounts = append(credential.Accounts, account)
69 credential.SelectedAccount = account.ID
70 if err := GenerateAccountsFile(credential); err != nil {
71 logrus.Errorf("error in checking credentials unable to update accounts yaml file - %v", err)
72 return err
73 }
74 return nil
75}
76
77// GetCredentials get all the stored credentials
78func GetCredentials() (*model.Credential, error) {

Callers 3

TestStoreCredentialsFunction · 0.85
SetDefaultProjectFunction · 0.85
LoginStartFunction · 0.85

Calls 4

getAccountConfigPathFunction · 0.85
GenerateAccountsFileFunction · 0.85
ReadFileMethod · 0.65
UnmarshalMethod · 0.45

Tested by 1

TestStoreCredentialsFunction · 0.68