forked from solo-io/wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
62 lines (52 loc) · 1.36 KB
/
cache.go
File metadata and controls
62 lines (52 loc) · 1.36 KB
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
package cache
import (
"context"
"fmt"
"net/http"
"github.com/solo-io/wasme/pkg/cmd/opts"
"github.com/solo-io/wasme/pkg/defaults"
"github.com/spf13/cobra"
)
type cacheOptions struct {
targetRefs []string
code string
config string
verbose bool
debug bool
port int
*opts.GeneralOptions
}
func CacheCmd(generalOptions *opts.GeneralOptions) *cobra.Command {
var opts cacheOptions
opts.GeneralOptions = generalOptions
cmd := &cobra.Command{
Use: "cache name[:tag|@digest] ...",
Short: "Expose images using http and their sha",
Long: `cache
`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("invalid number of arguments")
}
opts.targetRefs = args
return runCache(opts)
},
}
cmd.Flags().BoolVarP(&opts.verbose, "verbose", "v", false, "verbose output")
cmd.Flags().BoolVarP(&opts.debug, "debug", "d", false, "debug mode")
cmd.Flags().IntVarP(&opts.port, "port", "", 9979, "port")
return cmd
}
func runCache(opts cacheOptions) error {
imageCache := defaults.NewDefaultCache()
for _, image := range opts.targetRefs {
digest, err := imageCache.Add(context.TODO(), image)
if err != nil {
return fmt.Errorf("can't add image")
}
fmt.Println("added digest", digest)
}
http.ListenAndServe(fmt.Sprintf(":%d", opts.port), imageCache)
return nil
}