EnsureUser will load the current admin config and ensure the given user exists.
(username string, pubKey *models.PublicKey)
| 137 | // EnsureUser will load the current admin config and ensure the given user |
| 138 | // exists. |
| 139 | func (c *Config) EnsureAdminUser(username string, pubKey *models.PublicKey) error { |
| 140 | adminRepo, err := c.openAdminRepo() |
| 141 | if err != nil { |
| 142 | return err |
| 143 | } |
| 144 | |
| 145 | // Ensure the base config before we try and add a user. |
| 146 | err = c.ensureAdminConfig(adminRepo) |
| 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | |
| 151 | // Ensure User |
| 152 | err = c.ensureAdminUser(adminRepo, username, pubKey) |
| 153 | if err != nil { |
| 154 | return err |
| 155 | } |
| 156 | |
| 157 | // Load config |
| 158 | err = c.loadConfig(adminRepo) |
| 159 | if err != nil { |
| 160 | return err |
| 161 | } |
| 162 | |
| 163 | // We only commit at the very end, after everything has been loaded. This |
| 164 | // ensures we have a valid config. |
| 165 | status, err := adminRepo.Worktree.Status() |
| 166 | if err != nil { |
| 167 | return err |
| 168 | } |
| 169 | |
| 170 | if !status.IsClean() { |
| 171 | err = adminRepo.Commit(fmt.Sprintf("Added %s to config as admin", username), nil) |
| 172 | if err != nil { |
| 173 | return err |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return nil |
| 178 | } |
| 179 | |
| 180 | func (c *Config) flatten() { |
| 181 | // Add all user public keys to the config. |
no test coverage detected