• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

foomo / posh / 14267892308

04 Apr 2025 02:41PM UTC coverage: 6.101% (-0.07%) from 6.172%
14267892308

push

github

web-flow
Merge pull request #110 from foomo/feature/v0.11.0

feat: v0.11.0

3 of 122 new or added lines in 9 files covered. (2.46%)

5 existing lines in 2 files now uncovered.

159 of 2606 relevant lines covered (6.1%)

0.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pkg/command/cache.go
1
package command
2

3
import (
4
        "context"
5
        "fmt"
6
        "sort"
7

8
        "github.com/foomo/posh/pkg/cache"
9
        "github.com/foomo/posh/pkg/command/tree"
10
        "github.com/foomo/posh/pkg/log"
11
        "github.com/foomo/posh/pkg/prompt/goprompt"
12
        "github.com/foomo/posh/pkg/readline"
13
        "github.com/foomo/posh/pkg/util/suggests"
14
        "github.com/pterm/pterm"
15
        "github.com/pterm/pterm/putils"
16
        "github.com/samber/lo"
17
)
18

19
type Cache struct {
20
        l     log.Logger
21
        tree  tree.Root
22
        cache cache.Cache
23
}
24

25
// ------------------------------------------------------------------------------------------------
26
// ~ Constructor
27
// ------------------------------------------------------------------------------------------------
28

29
func NewCache(l log.Logger, cache cache.Cache) *Cache {
×
30
        inst := &Cache{
×
31
                l:     l,
×
32
                cache: cache,
×
33
        }
×
34
        inst.tree = tree.New(&tree.Node{
×
35
                Name:        "cache",
×
36
                Description: "Manage the internal cache",
×
37
                Nodes: tree.Nodes{
×
38
                        {
×
39
                                Name:        "clear",
×
40
                                Description: "Clear caches",
×
41
                                Args: tree.Args{
×
42
                                        {
×
43
                                                Name:        "Namespace",
×
44
                                                Description: "Name of namespace to clear.",
×
45
                                                Repeat:      true,
×
46
                                                Optional:    true,
×
47
                                                Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest {
×
48
                                                        return suggests.List(lo.Keys(inst.cache.List()))
×
49
                                                },
×
50
                                        },
51
                                },
52
                                Execute: inst.clear,
53
                        },
54
                        {
55
                                Name:        "list",
56
                                Description: "List all caches",
57
                                Execute:     inst.list,
58
                        },
59
                },
60
        })
61
        return inst
×
62
}
63

64
// ------------------------------------------------------------------------------------------------
65
// ~ Public methods
66
// ------------------------------------------------------------------------------------------------
67

68
func (c *Cache) Name() string {
×
69
        return c.tree.Node().Name
×
70
}
×
71

72
func (c *Cache) Description() string {
×
73
        return c.tree.Node().Description
×
74
}
×
75

76
func (c *Cache) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
×
77
        return c.tree.Complete(ctx, r)
×
78
}
×
79

80
func (c *Cache) Execute(ctx context.Context, r *readline.Readline) error {
×
81
        return c.tree.Execute(ctx, r)
×
82
}
×
83

84
func (c *Cache) Help(ctx context.Context, r *readline.Readline) string {
×
85
        return `Manage the internal cache.
×
86

×
87
Usage:
×
88
  cache [command]
×
89

×
90
Available commands:
×
91
  list    List all caches
×
92
  clear   Clear all caches
×
93
`
×
94
}
×
95

96
// ------------------------------------------------------------------------------------------------
97
// ~ Private methods
98
// ------------------------------------------------------------------------------------------------
99

100
func (c *Cache) clear(ctx context.Context, r *readline.Readline) error {
×
101
        if r.Args().Len() > 1 {
×
102
                c.l.Info("clearing cache:")
×
103
                for _, value := range r.Args()[1:] {
×
104
                        c.l.Info("└ " + value)
×
105
                        c.cache.Get(value).Delete("")
×
106
                }
×
107
        } else {
×
108
                c.l.Info("clearing all caches")
×
NEW
109
                c.cache.Clear()
×
110
        }
×
111
        return nil
×
112
}
113

114
func (c *Cache) list(ctx context.Context, r *readline.Readline) error {
×
115
        // Create a fork of the default table, fill it with data and print it.
×
116
        // Data can also be generated and inserted later.
×
117
        list := pterm.LeveledList{}
×
118
        cacheList := c.cache.List()
×
119
        cacheListKeys := lo.Keys(cacheList)
×
120
        sort.Strings(cacheListKeys)
×
121
        for _, ns := range cacheListKeys {
×
122
                value := cacheList[ns]
×
123
                list = append(list, pterm.LeveledListItem{Level: 0, Text: ns})
×
124
                keys := value.Keys()
×
125
                sort.Strings(keys)
×
126
                for _, k := range keys {
×
127
                        list = append(list, pterm.LeveledListItem{Level: 1, Text: k})
×
128
                        if c.l.Level() == log.LevelTrace {
×
129
                                list = append(list, pterm.LeveledListItem{Level: 2, Text: fmt.Sprintf("%v", value.Get(k, nil))})
×
130
                        }
×
131
                }
132
        }
133
        // Generate tree from LeveledList.
134
        root := putils.TreeFromLeveledList(list)
×
135

×
136
        // Render TreePrinter
×
137
        return pterm.DefaultTree.WithRoot(root).Render()
×
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc