Files
kopia/cli/show_utils.go
Jarek Kowalski fcd507a56d Refactored most of the CLI tests to run in-process as opposed to using sub-processes (#1059)
* 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.
2021-05-11 22:26:28 -07:00

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
}
}