-
Notifications
You must be signed in to change notification settings - Fork 23
/
cmd.go
74 lines (63 loc) · 1.93 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package info
import (
"github.com/spf13/cobra"
"github.com/open-component-model/ocm/v2/cmds/ocm/commands/cachecmds/names"
"github.com/open-component-model/ocm/v2/cmds/ocm/commands/verbs"
"github.com/open-component-model/ocm/v2/cmds/ocm/pkg/utils"
"github.com/open-component-model/ocm/v2/pkg/common/accessio"
"github.com/open-component-model/ocm/v2/pkg/contexts/clictx"
"github.com/open-component-model/ocm/v2/pkg/contexts/oci/attrs/cacheattr"
"github.com/open-component-model/ocm/v2/pkg/errors"
"github.com/open-component-model/ocm/v2/pkg/out"
)
var (
Names = names.Cache
Verb = verbs.Info
)
type Command struct {
utils.BaseCommand
cache accessio.BlobCache
}
// NewCommand creates a new artifact command.
func NewCommand(ctx clictx.Context, names ...string) *cobra.Command {
return utils.SetupCommand(&Command{BaseCommand: utils.NewBaseCommand(ctx)}, names...)
}
func (o *Command) ForName(name string) *cobra.Command {
return &cobra.Command{
Use: "",
Short: "show OCI blob cache information",
Long: `
Show details about the OCI blob cache (if given).
`,
Args: cobra.NoArgs,
Example: `
$ ocm cache info
`,
}
}
func (o *Command) Complete(args []string) error {
o.cache = cacheattr.Get(o.Context)
if o.cache == nil {
return errors.Newf("no blob cache configured")
}
return nil
}
func (o *Command) Run() error {
if r, ok := o.cache.(accessio.RootedCache); ok {
path, fs := r.Root()
out.Outf(o.Context, "Used cache directory %s [%s]\n", path, fs.Name())
}
if r, ok := o.cache.(accessio.CleanupCache); ok {
cnt, _, _, size, _, _, err := r.Cleanup(nil, nil, true)
if err != nil {
return err
}
out.Outf(o.Context, "Total cache size %d entries [%.3f MB]\n", cnt, float64(size)/1024/1024)
} else {
out.Outf(o.Context, "Cache does not support more info\n")
}
return nil
}