fix: count

This commit is contained in:
Urko 2023-08-28 11:18:50 +02:00
parent f43fae8c3d
commit ed63ededf1
3 changed files with 18 additions and 18 deletions

View File

@ -1,6 +1,6 @@
---- Definitions ----
license means right to use
author means who had initial idea, who research about jurisprudence and the who who started, in this case : Urko: Bein.
author means who had initial idea, who research about jurisprudence and who started, in this case : Urko: Bein.
contributors means every man who has helped to improve this software

View File

@ -76,17 +76,17 @@ var Check = &cobra.Command{
go b.CompareConcurrent(ctx, backupDir, bucketName, msgChan)
reportBuilder := strings.Builder{}
countLocalFiles := 0
countLocal := 0
countOK := 0
reportBuilder.WriteString(fmt.Sprintf("Local files within `%s` path already in `%s` bucket:\n", backupDir, bucketName))
cloudBuilder := strings.Builder{}
cloudBuilder.WriteString(fmt.Sprintf("List of B2 files within `%s` bucket not found in local path `%s`\n", bucketName, backupDir))
countNotInLocalBuffer := 0
countNotInLocal := 0
localBuilder := strings.Builder{}
localBuilder.WriteString(fmt.Sprintf("List of local files in `%s` not found in B2 bucket `%s`\n", backupDir, bucketName))
countNotInCloudBuffer := 0
countNotInCloud := 0
loop:
for {
@ -102,19 +102,19 @@ var Check = &cobra.Command{
break loop
case msg := <-msgChan:
if msg.Err == nil && msg.File == "" {
countLocalFiles = msg.LocalCount
countLocal = msg.LocalCount
break loop
}
if errors.Is(msg.Err, backblaze.ErrCloudNotInLocal) {
logger.Debug(msg.File + ": B2 file not found in local")
cloudBuilder.WriteString(msg.File + "\n")
countNotInLocalBuffer++
countNotInLocal++
continue
}
if errors.Is(msg.Err, backblaze.ErrLocalNotInCloud) {
logger.Debug(msg.File + ": local file not found in B2")
localBuilder.WriteString(msg.File + "\n")
countNotInCloudBuffer++
countNotInCloud++
continue
}
reportBuilder.WriteString(msg.File + " OK" + "\n")
@ -122,11 +122,11 @@ var Check = &cobra.Command{
}
}
if countNotInCloudBuffer > 0 {
if countNotInCloud > 0 {
reportBuilder.WriteString("\n")
reportBuilder.WriteString(cloudBuilder.String())
}
if countNotInLocalBuffer > 0 {
if countNotInLocal > 0 {
reportBuilder.WriteString("\n")
reportBuilder.WriteString(localBuilder.String())
}
@ -135,10 +135,10 @@ var Check = &cobra.Command{
To: cfg.MailTo,
Bucket: bucketName,
BackupDir: backupDir,
CountLocal: countNotInLocalBuffer,
CountCloud: countNotInCloudBuffer,
CountNotInLocal: countNotInLocal,
CountNotInCloud: countNotInCloud,
CountOK: countOK,
CountLocalFiles: countLocalFiles,
CountLocal: countLocal,
Attachments: []email.EmailAttachment{{File: bytes.NewReader([]byte(reportBuilder.String())), Title: fmt.Sprintf("%s-check-report.txt", bucketName)}},
}); err != nil {
panic(fmt.Errorf("error while send email: %w", err))

View File

@ -32,10 +32,10 @@ type EmailWithAttachments struct {
To string
Bucket string
BackupDir string
CountLocal int
CountNotInLocal int
CountOK int
CountCloud int
CountLocalFiles int
CountNotInCloud int
CountLocal int
Attachments []EmailAttachment
}
@ -75,10 +75,10 @@ func NewMailService(config MailServiceConfig) *EmailService {
func (e *EmailService) SendOK(emailData EmailWithAttachments) error {
template := strings.Replace(htmlTemplate, "{{bucket}}", emailData.Bucket, -1)
template = strings.Replace(template, "{{local_backup_path}}", emailData.BackupDir, -1)
template = strings.Replace(template, "{{count_ErrCloudNotInLocal}}", fmt.Sprint(emailData.CountCloud), -1)
template = strings.Replace(template, "{{count_ErrLocalNotInCloud}}", fmt.Sprint(emailData.CountLocal), -1)
template = strings.Replace(template, "{{count_ErrCloudNotInLocal}}", fmt.Sprint(emailData.CountNotInLocal), -1)
template = strings.Replace(template, "{{count_ErrLocalNotInCloud}}", fmt.Sprint(emailData.CountNotInCloud), -1)
template = strings.Replace(template, "{{count_ok}}", fmt.Sprint(emailData.CountOK), -1)
template = strings.Replace(template, "{{count_local}}", fmt.Sprint(emailData.CountLocalFiles), -1)
template = strings.Replace(template, "{{count_local}}", fmt.Sprint(emailData.CountLocal), -1)
msg, err := newMessage(e.from, emailData.To, subjectReport).
withAttachments(template, emailData.Attachments)