mirror of
https://github.com/kopia/kopia.git
synced 2026-03-17 05:38:34 -04:00
* cli: fixed remaining testability indirections for output and logging * cli: added cli.RunSubcommand() which is used in testing to execute a subcommand in the same process * tests: refactored most e2e tests to invoke kopia subcommands in-process * Makefile: enable code coverage for cli/ and internal/ * testing: pass 'testing' tag to unit tests which uses much faster (insecure) password hashing scheme * Makefile: push coverage from PRs again * tests: disable buffer management to reduce memory usage on ARM * cli: fixed misaligned atomic field on ARMHF also temporarily fixed statup-time benign race condition when setting default on the timeZone variable, which is the last global variable.
93 lines
1.7 KiB
Go
93 lines
1.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/internal/iocopy"
|
|
"github.com/kopia/kopia/internal/units"
|
|
)
|
|
|
|
// TODO - remove this global.
|
|
var timeZone = "local"
|
|
|
|
func showContentWithFlags(w io.Writer, rd io.Reader, unzip, indentJSON bool) error {
|
|
if unzip {
|
|
gz, err := gzip.NewReader(rd)
|
|
if err != nil {
|
|
return errors.Wrap(err, "unable to open gzip stream")
|
|
}
|
|
|
|
rd = gz
|
|
}
|
|
|
|
var buf1, buf2 bytes.Buffer
|
|
|
|
if indentJSON {
|
|
if _, err := iocopy.Copy(&buf1, rd); err != nil {
|
|
return errors.Wrap(err, "error copying data")
|
|
}
|
|
|
|
if err := json.Indent(&buf2, buf1.Bytes(), "", " "); err != nil {
|
|
return errors.Wrap(err, "errors indenting JSON")
|
|
}
|
|
|
|
rd = ioutil.NopCloser(&buf2)
|
|
}
|
|
|
|
if _, err := iocopy.Copy(w, rd); err != nil {
|
|
return errors.Wrap(err, "error copying data")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func maybeHumanReadableBytes(enable bool, value int64) string {
|
|
if enable {
|
|
return units.BytesStringBase10(value)
|
|
}
|
|
|
|
return fmt.Sprintf("%v", value)
|
|
}
|
|
|
|
func maybeHumanReadableCount(enable bool, value int64) string {
|
|
if enable {
|
|
return units.Count(value)
|
|
}
|
|
|
|
return fmt.Sprintf("%v", value)
|
|
}
|
|
|
|
func formatTimestamp(ts time.Time) string {
|
|
return convertTimezone(ts).Format("2006-01-02 15:04:05 MST")
|
|
}
|
|
|
|
func formatTimestampPrecise(ts time.Time) string {
|
|
return convertTimezone(ts).Format("2006-01-02 15:04:05.000 MST")
|
|
}
|
|
|
|
func convertTimezone(ts time.Time) time.Time {
|
|
switch timeZone {
|
|
case "local":
|
|
return ts.Local()
|
|
case "utc":
|
|
return ts.UTC()
|
|
case "original":
|
|
return ts
|
|
default:
|
|
loc, err := time.LoadLocation(timeZone)
|
|
if err == nil {
|
|
return ts.In(loc)
|
|
}
|
|
|
|
return ts
|
|
}
|
|
}
|