mirror of
https://github.com/fleetdm/fleet
synced 2026-05-04 05:48:26 +00:00
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves https://github.com/fleetdm/confidential/issues/13934 # Checklist for submitter If some of the following don't apply, delete the relevant line. - [ ] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. ## Testing - [x] Added/updated automated tests - [ ] QA'd all new/changed functionality manually
40 lines
1 KiB
Go
40 lines
1 KiB
Go
// Package installersize provides context handling for the maximum software installer size.
|
|
package installersize
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/docker/go-units"
|
|
)
|
|
|
|
const MaxSoftwareInstallerSize int64 = 10 * units.GiB
|
|
|
|
// Human formats a byte size into a human-readable string.
|
|
// It evaluates both SI units (KB, MB, GB) and binary units (KiB, MiB, GiB)
|
|
// and returns whichever representation is shorter.
|
|
func Human(bytes int64) string {
|
|
si := units.HumanSize(float64(bytes))
|
|
binary := units.BytesSize(float64(bytes))
|
|
|
|
if len(binary) < len(si) {
|
|
return binary
|
|
}
|
|
return si
|
|
}
|
|
|
|
type key struct{}
|
|
|
|
// NewContext returns a new context with the max installer size value.
|
|
func NewContext(ctx context.Context, maxSize int64) context.Context {
|
|
return context.WithValue(ctx, key{}, maxSize)
|
|
}
|
|
|
|
// FromContext returns the max installer size from the context if present.
|
|
// If not present, returns DefaultMaxInstallerSize.
|
|
func FromContext(ctx context.Context) int64 {
|
|
v, ok := ctx.Value(key{}).(int64)
|
|
if !ok {
|
|
return MaxSoftwareInstallerSize
|
|
}
|
|
return v
|
|
}
|