improve alias file overwriting
This commit is contained in:
parent
74a0550bfb
commit
fab84705e8
2 changed files with 14 additions and 12 deletions
|
|
@ -27,7 +27,7 @@ func addEmailAlias(gitFs *GitFS, to, fromDomain string) (string, error) {
|
||||||
return "", errors.Wrapf(err, fmt.Sprintf("failed to create new email aliases because domain for %s does not exist", to))
|
return "", errors.Wrapf(err, fmt.Sprintf("failed to create new email aliases because domain for %s does not exist", to))
|
||||||
}
|
}
|
||||||
aliasEmail := generateRandomEmail(fromDomain)
|
aliasEmail := generateRandomEmail(fromDomain)
|
||||||
glog.Infof("Adding %s -> %s alias to %s", aliasEmail, to, emailAliasesConfigFileRelative)
|
glog.Infof("adding %s -> %s alias to %s", aliasEmail, to, emailAliasesConfigFileRelative)
|
||||||
contents := fmt.Sprintf("%s %s", aliasEmail, to)
|
contents := fmt.Sprintf("%s %s", aliasEmail, to)
|
||||||
|
|
||||||
// Read existing contents
|
// Read existing contents
|
||||||
|
|
@ -39,7 +39,7 @@ func addEmailAlias(gitFs *GitFS, to, fromDomain string) (string, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.Wrapf(err, "failed to read existing aliases file")
|
return "", errors.Wrapf(err, "failed to read existing aliases file")
|
||||||
}
|
}
|
||||||
defer fRead.Close()
|
fRead.Close()
|
||||||
newContents := getAddedAliasContents(string(fileContentsBytes), aliasEmail, to)
|
newContents := getAddedAliasContents(string(fileContentsBytes), aliasEmail, to)
|
||||||
|
|
||||||
// Write new contents
|
// Write new contents
|
||||||
|
|
@ -49,9 +49,7 @@ func addEmailAlias(gitFs *GitFS, to, fromDomain string) (string, error) {
|
||||||
}
|
}
|
||||||
defer fWrite.Close()
|
defer fWrite.Close()
|
||||||
|
|
||||||
glog.Infof("writing new contents to file: %s", newContents)
|
glog.Infof("writing new contents to file: \n%s", newContents)
|
||||||
fRead.Write([]byte(newContents))
|
|
||||||
|
|
||||||
if _, err = fWrite.Write([]byte(contents)); err != nil {
|
if _, err = fWrite.Write([]byte(contents)); err != nil {
|
||||||
return "", errors.Wrapf(err, "failed to write config to file")
|
return "", errors.Wrapf(err, "failed to write config to file")
|
||||||
}
|
}
|
||||||
|
|
@ -62,20 +60,23 @@ func generateRandomEmail(fromDomain string) string {
|
||||||
return fmt.Sprintf("%s-%s-generated%s", strings.ToLower(gofakeit.Adverb()), strings.ToLower(gofakeit.FirstName()), fromDomain)
|
return fmt.Sprintf("%s-%s-generated%s", strings.ToLower(gofakeit.Adverb()), strings.ToLower(gofakeit.FirstName()), fromDomain)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPostFixAlias(from, to string) string {
|
func getPostfixAlias(from, to string) string {
|
||||||
return fmt.Sprintf("%s %s", from, to)
|
return fmt.Sprintf("%s %s", from, to)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAddedAliasContents(currentContents, from, to string) string {
|
func getAddedAliasContents(currentContents, from, to string) string {
|
||||||
glog.Infof("Existingcontent: %s", currentContents)
|
|
||||||
lines := strings.Split(currentContents, "\n")
|
lines := strings.Split(currentContents, "\n")
|
||||||
newLines := []string{}
|
newLines := []string{}
|
||||||
// If `to` already has an alias, overwrite it
|
|
||||||
for _, l := range lines {
|
for _, l := range lines {
|
||||||
if !strings.HasSuffix(l, to) {
|
l = strings.TrimSpace(l)
|
||||||
newLines = append(newLines, l)
|
if l == "" {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
if strings.HasSuffix(l, to) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newLines = append(newLines, l)
|
||||||
}
|
}
|
||||||
newLines = append(newLines, getPostFixAlias(from, to))
|
newLines = append(newLines, getPostfixAlias(from, to))
|
||||||
return strings.Join(newLines, "\n") + "\n"
|
return strings.Join(newLines, "\n") + "\n"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
accordingly-kelton-generated@viktorbarzin.me vbarzin@gmail.com
|
accordingly-kelton-generated@viktorbarzin.me vbarzin@gmail.com
|
||||||
|
accordingly-kelton-generated@viktorbarzin.me me@viktorbarzin.me
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue