-
Notifications
You must be signed in to change notification settings - Fork 56
/
extensions_copy.go
174 lines (154 loc) · 4.08 KB
/
extensions_copy.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"context"
"flag"
"fmt"
"io"
"net/http"
"strings"
"github.com/sourcegraph/src-cli/internal/api"
)
func withCfg(new *config, f func()) {
old := cfg
cfg = new
f()
cfg = old
}
func init() {
usage := `
Copy an extension from Sourcegraph.com to your private registry.
`
flagSet := flag.NewFlagSet("copy", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src extensions %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
extensionIDFlag = flagSet.String("extension-id", "", `The <extID> in https://sourcegraph.com/extensions/<extID> (e.g. sourcegraph/java)`)
currentUserFlag = flagSet.String("current-user", "", `The current user`)
apiFlags = api.NewFlags(flagSet)
)
handler := func(args []string) error {
err := flagSet.Parse(args)
if err != nil {
return err
}
extensionID := *extensionIDFlag
if extensionID == "" {
return fmt.Errorf("must provide -extension-id")
}
currentUser := *currentUserFlag
if currentUser == "" {
return fmt.Errorf("must provide -current-user")
}
extensionIDParts := strings.Split(extensionID, "/")
if len(extensionIDParts) != 2 {
return fmt.Errorf("-extension-id must have the form <publisher>/<name>")
}
extensionName := extensionIDParts[1]
ctx := context.Background()
client := cfg.apiClient(apiFlags, flagSet.Output())
ok := false
var extensionResult struct {
ExtensionRegistry struct {
Extension struct {
Manifest struct {
Raw string
BundleURL string
}
}
}
}
withCfg(&config{Endpoint: "https://sourcegraph.com"}, func() {
dotComClient := cfg.apiClient(apiFlags, flagSet.Output())
query := `query GetExtension(
$extensionID: String!
){
extensionRegistry{
extension(extensionID: $extensionID) {
manifest{
raw
bundleURL
}
}
}
}`
ok, err = dotComClient.NewRequest(query, map[string]interface{}{
"extensionID": extensionID,
}).Do(ctx, &extensionResult)
})
if err != nil || !ok {
return err
}
rawManifest := []byte(extensionResult.ExtensionRegistry.Extension.Manifest.Raw)
manifest, err := updatePropertyInManifest(rawManifest, "extensionID", extensionID)
if err != nil {
return err
}
// Remove sourcegraph.com bundle URL.
manifest, err = updatePropertyInManifest(manifest, "url", "")
if err != nil {
return err
}
response, err := http.Get(extensionResult.ExtensionRegistry.Extension.Manifest.BundleURL)
if err != nil {
return err
}
defer response.Body.Close()
bundle, err := io.ReadAll(response.Body)
if err != nil {
return err
}
fmt.Printf("bundle: %s\n", string(bundle[0:100]))
fmt.Printf("manifest: %s\n", string(manifest[0:]))
query := `mutation PublishExtension(
$extensionID: String!,
$manifest: String!,
$bundle: String,
) {
extensionRegistry {
publishExtension(
extensionID: $extensionID,
manifest: $manifest,
bundle: $bundle
) {
extension {
extensionID
url
}
}
}
}`
var publishResult struct {
ExtensionRegistry struct {
PublishExtension struct {
Extension struct {
ExtensionID string
URL string
}
}
}
}
if ok, err := client.NewRequest(query, map[string]interface{}{
"extensionID": currentUser + "/" + extensionName,
"manifest": string(manifest),
"bundle": bundle,
}).Do(ctx, &publishResult); err != nil || !ok {
return err
}
fmt.Println("Extension published!")
fmt.Println()
fmt.Printf("\tExtension ID: %s\n\n", publishResult.ExtensionRegistry.PublishExtension.Extension.ExtensionID)
fmt.Printf("View, enable, and configure it at: %s\n", cfg.Endpoint+publishResult.ExtensionRegistry.PublishExtension.Extension.URL)
return nil
}
// Register the command.
extensionsCommands = append(extensionsCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
// Catch the mistake of omitting the "extensions" subcommand.
commands = append(commands, didYouMeanOtherCommand("publish", []string{"extensions publish", "ext publish (alias)"}))
}