output.go
author Mikael Berthe <mikael@lilotux.net>
Sun, 14 Oct 2018 13:20:09 +0200
changeset 36 e918b7e63748
child 39 24ca1bf4a0bf
permissions -rw-r--r--
Add JSON output
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
/*
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
 * Copyright (C) 2014-2018 Mikael Berthe <mikael@lilotux.net>
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
 *
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
 * This program is free software; you can redistribute it and/or modify
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
 * it under the terms of the GNU General Public License as published by
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
 * the Free Software Foundation; either version 2 of the License, or (at
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
 * your option) any later version.
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
 *
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful, but
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
 * WITHOUT ANY WARRANTY; without even the implied warranty of
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
 * General Public License for more details.
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
 *
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
 * USA
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
 */
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
package main
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
import (
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
	"encoding/json"
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
	"fmt"
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
)
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
func displayResults(results Results, jsonOutput bool, summaryOnly bool) {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	if jsonOutput {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
		displayResultsJSON(results)
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
		return
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
	}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	if !summaryOnly {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
		for i, g := range results.Groups {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
			fmt.Printf("\nGroup #%d (%d files * %v):\n", i+1,
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
				len(g.Paths), formatSize(g.Size, true))
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
			for _, f := range g.Paths {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
				fmt.Println(f)
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
			}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
		}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
	}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
	// We're done if we do not display statistics
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	if myLog.verbosity < 1 && !summaryOnly {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
		return
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
	// Add a trailing newline
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
	if len(results.Groups) > 0 && myLog.verbosity > 0 {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
		fmt.Println()
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
	myLog.Println(0, "Final count:", results.Duplicates,
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
		"duplicate files in", len(results.Groups), "sets")
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
	myLog.Println(0, "Redundant data size:",
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
		formatSize(results.RedundantDataSize, false))
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
func displayResultsJSON(results Results) {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
	b, err := json.Marshal(results)
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
	if err != nil {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
		panic(err)
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
	}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
	fmt.Println(string(b))
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
}