Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.

Commit d5f8481

Browse files
authored
Add Remote cache (#77)
* Add Remote cache * lint
1 parent 923f0bd commit d5f8481

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

cmd/tools/cli/list.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"fmt"
45
"github.com/kf5i/k3ai-core/internal/plugins"
56
"github.com/spf13/cobra"
67
)
@@ -13,27 +14,50 @@ func newListCommand() *cobra.Command {
1314
RunE: func(cmd *cobra.Command, args []string) error {
1415
group, _ := cmd.Flags().GetBool(plugins.GroupType)
1516
if group {
16-
var grs plugins.Groups
17-
err := grs.List(repo + plugins.GroupsDir)
17+
18+
var cache plugins.Cache
19+
err := cache.Encode(repo, "cache_groups.yaml")
1820
if err != nil {
19-
return err
21+
fmt.Printf("Can't load cache:%s will use remote\n", err)
22+
var grs plugins.Groups
23+
err := grs.List(repo + plugins.GroupsDir)
24+
if err != nil {
25+
return err
26+
}
27+
PrintFormat("Name", "Description")
28+
for _, p := range grs.Items {
29+
PrintFormat(p.GroupName, p.GroupDescription)
30+
}
31+
return nil
2032
}
2133
PrintFormat("Name", "Description")
22-
for _, p := range grs.Items {
23-
PrintFormat(p.GroupName, p.GroupDescription)
34+
for _, p := range cache.Items {
35+
PrintFormat(p.Name, p.Description)
2436
}
2537

2638
return nil
2739
}
28-
var pls plugins.Plugins
29-
err := pls.List(repo + plugins.PluginDir)
40+
41+
var cache plugins.Cache
42+
err := cache.Encode(repo, "cache_plugins.yaml")
3043
if err != nil {
31-
return err
32-
}
44+
fmt.Printf("Can't load cache:%s will use remote\n", err)
45+
46+
var pls plugins.Plugins
47+
err = pls.List(repo + plugins.PluginDir)
48+
if err != nil {
49+
return err
50+
}
3351

52+
PrintFormat("Name", "Description")
53+
for _, p := range pls.Items {
54+
PrintFormat(p.PluginName, p.PluginDescription)
55+
}
56+
return nil
57+
}
3458
PrintFormat("Name", "Description")
35-
for _, p := range pls.Items {
36-
PrintFormat(p.PluginName, p.PluginDescription)
59+
for _, p := range cache.Items {
60+
PrintFormat(p.Name, p.Description)
3761
}
3862

3963
return nil

internal/plugins/cache.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package plugins
2+
3+
import "github.com/kf5i/k3ai-core/internal/shared"
4+
5+
// CacheItem the item cache
6+
type CacheItem struct {
7+
Name string `yaml:"name"`
8+
Description string `yaml:"description"`
9+
}
10+
11+
// Cache for plugins and groups
12+
type Cache struct {
13+
Name string `yaml:"name"`
14+
Type string `yaml:"type"`
15+
Items []CacheItem `yaml:"items,flow"`
16+
}
17+
18+
// Encode encode the cache to be printed
19+
func (cache *Cache) Encode(URL string, fileCache string) error {
20+
cacheURL := shared.IncludeSlash(URL, "/") + fileCache
21+
return encode(cacheURL, cache)
22+
}

0 commit comments

Comments
 (0)