Update updates the git credentials for a given hostname, first by rejecting any existing credentials and then approving the new credentials.
(hostname, username, password string)
| 16 | // Update updates the git credentials for a given hostname, first by rejecting any existing credentials and then |
| 17 | // approving the new credentials. |
| 18 | func (u *Updater) Update(hostname, username, password string) error { |
| 19 | ctx := context.TODO() |
| 20 | |
| 21 | // clear previous cached credentials |
| 22 | rejectCmd, err := u.GitClient.Command(ctx, "credential", "reject") |
| 23 | if err != nil { |
| 24 | return err |
| 25 | } |
| 26 | |
| 27 | rejectCmd.Stdin = bytes.NewBufferString(heredoc.Docf(` |
| 28 | protocol=https |
| 29 | host=%s |
| 30 | `, hostname)) |
| 31 | |
| 32 | _, err = rejectCmd.Output() |
| 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | |
| 37 | approveCmd, err := u.GitClient.Command(ctx, "credential", "approve") |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | |
| 42 | approveCmd.Stdin = bytes.NewBufferString(heredoc.Docf(` |
| 43 | protocol=https |
| 44 | host=%s |
| 45 | username=%s |
| 46 | password=%s |
| 47 | `, hostname, username, password)) |
| 48 | |
| 49 | _, err = approveCmd.Output() |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | return nil |
| 55 | } |