-
Notifications
You must be signed in to change notification settings - Fork 137
/
assets.go
144 lines (125 loc) · 3.59 KB
/
assets.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"github.com/cozy/cozy-stack/client/request"
modelAsset "github.com/cozy/cozy-stack/pkg/assets/model"
"github.com/spf13/cobra"
)
var flagURL string
var flagName string
var flagShasum string
var flagContext string
var assetsCmdGroup = &cobra.Command{
Use: "assets <command>",
Short: "Show and manage dynamic assets",
Long: `cozy-stack assets can be used to list, insert or remove dynamic assets`,
}
var addAssetCmd = &cobra.Command{
Use: "add --url <url> --name <name> --shasum <shasum> --context <context>",
Aliases: []string{"insert"},
Short: "Insert a dynamic asset",
Long: `Insert an asset that will be available on https://<instance>/assets/<name>
For example, if a dynamic asset with the name '/foo/bar/baz' is added for a
context foocontext, and an instance example.mycozy.cloud is in the foocontext
context, then this asset can be requested on
https://example.mycozy.cloud/assets/foo/bar/baz.js (and not on
'example-app.mycozy.cloud').`,
Example: "$ cozy-stack assets add --url file:///foo/bar/baz.js --name /foo/bar/baz.js --shasum 0763d6c2cebee0880eb3a9cc25d38cd23db39b5c3802f2dc379e408c877a2788 --context foocontext",
RunE: addAsset,
}
func addAsset(cmd *cobra.Command, args []string) error {
if flagContext == "" {
return fmt.Errorf("You must provide a context")
}
assetOption := modelAsset.AssetOption{
URL: flagURL,
Name: flagName,
Shasum: flagShasum,
Context: flagContext,
}
customAssets := []modelAsset.AssetOption{assetOption}
marshaledAssets, err := json.Marshal(customAssets)
if err != nil {
return err
}
ac := newAdminClient()
req := &request.Options{
Method: "POST",
Path: "instances/assets",
Body: bytes.NewReader(marshaledAssets),
}
res, err := ac.Req(req)
if err != nil {
return err
}
defer res.Body.Close()
return nil
}
var rmAssetCmd = &cobra.Command{
Use: "rm [context] [name]",
Aliases: []string{"remove"},
Short: "Removes an asset",
Long: "Removes a custom asset in a specific context",
Example: "$ cozy-stack assets rm foobar /foo/bar/baz.js",
RunE: rmAsset,
}
func rmAsset(cmd *cobra.Command, args []string) error {
// Check params
if len(args) != 2 {
return cmd.Usage()
}
ac := newAdminClient()
req := &request.Options{
Method: "DELETE",
Path: fmt.Sprintf("instances/assets/%s/%s", args[0], args[1]),
}
res, err := ac.Req(req)
if err != nil {
return err
}
defer res.Body.Close()
return nil
}
var lsAssetsCmd = &cobra.Command{
Use: "ls",
Aliases: []string{"list"},
Short: "List assets",
Long: "List assets currently served by the stack",
Example: "$ cozy-stack assets ls",
RunE: lsAssets,
}
func lsAssets(cmd *cobra.Command, args []string) error {
ac := newAdminClient()
req := &request.Options{
Method: "GET",
Path: "instances/assets",
}
res, err := ac.Req(req)
if err != nil {
return err
}
defer res.Body.Close()
var v interface{}
err = json.NewDecoder(res.Body).Decode(&v)
if err != nil {
return err
}
json, err := json.MarshalIndent(v, "", " ")
if err != nil {
return err
}
fmt.Println(string(json))
return nil
}
func init() {
assetsCmdGroup.AddCommand(addAssetCmd)
assetsCmdGroup.AddCommand(lsAssetsCmd)
assetsCmdGroup.AddCommand(rmAssetCmd)
RootCmd.AddCommand(assetsCmdGroup)
addAssetCmd.Flags().StringVar(&flagURL, "url", "", "The URL of the asset")
addAssetCmd.Flags().StringVar(&flagName, "name", "", "The name of the asset")
addAssetCmd.Flags().StringVar(&flagShasum, "shasum", "", "The shasum of the asset")
addAssetCmd.Flags().StringVar(&flagContext, "context", "", "The context of the asset")
}