github.com/qsis/helm@v3.0.0-beta.3+incompatible/internal/experimental/registry/util.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package registry // import "helm.sh/helm/internal/experimental/registry"
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"io"
    23  	"time"
    24  
    25  	orascontext "github.com/deislabs/oras/pkg/context"
    26  	units "github.com/docker/go-units"
    27  	"github.com/sirupsen/logrus"
    28  )
    29  
    30  // byteCountBinary produces a human-readable file size
    31  func byteCountBinary(b int64) string {
    32  	const unit = 1024
    33  	if b < unit {
    34  		return fmt.Sprintf("%d B", b)
    35  	}
    36  	div, exp := int64(unit), 0
    37  	for n := b / unit; n >= unit; n /= unit {
    38  		div *= unit
    39  		exp++
    40  	}
    41  	return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
    42  }
    43  
    44  // shortDigest returns first 7 characters of a sha256 digest
    45  func shortDigest(digest string) string {
    46  	if len(digest) == 64 {
    47  		return digest[:7]
    48  	}
    49  	return digest
    50  }
    51  
    52  // timeAgo returns a human-readable timestamp respresenting time that has passed
    53  func timeAgo(t time.Time) string {
    54  	return units.HumanDuration(time.Now().UTC().Sub(t))
    55  }
    56  
    57  // ctx retrieves a fresh context.
    58  // disable verbose logging coming from ORAS (unless debug is enabled)
    59  func ctx(out io.Writer, debug bool) context.Context {
    60  	if !debug {
    61  		return orascontext.Background()
    62  	}
    63  	ctx := orascontext.WithLoggerFromWriter(context.Background(), out)
    64  	orascontext.GetLogger(ctx).Logger.SetLevel(logrus.DebugLevel)
    65  	return ctx
    66  }