-
Notifications
You must be signed in to change notification settings - Fork 57
/
debug_common.go
123 lines (102 loc) · 3.19 KB
/
debug_common.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
package main
import (
"archive/zip"
"context"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/sourcegraph/src-cli/internal/exec"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
type archiveFile struct {
name string
data []byte
err error
}
func archiveFileFromCommand(ctx context.Context, path, cmd string, args ...string) *archiveFile {
f := &archiveFile{name: path}
f.data, f.err = exec.CommandContext(ctx, cmd, args...).CombinedOutput()
if f.err != nil {
f.err = errors.Wrapf(f.err, "executing command: %s %s: received error: %s", cmd, strings.Join(args, " "), f.data)
}
return f
}
// verify prompts the user to confirm they want to run the command
func verify(confirmationText string) (bool, error) {
input := ""
for strings.ToLower(input) != "y" && strings.ToLower(input) != "n" {
fmt.Printf("%s [y/N]: ", confirmationText)
if _, err := fmt.Scanln(&input); err != nil {
return false, err
}
}
return strings.ToLower(input) == "y", nil
}
func processBaseDir(base string) (string, string) {
if !strings.HasSuffix(base, ".zip") {
return base + ".zip", base
}
return base, strings.TrimSuffix(base, ".zip")
}
// write all the outputs from an archive command passed on the channel to to the zip writer
func writeChannelContentsToZip(zw *zip.Writer, ch <-chan *archiveFile, verbose bool) error {
for f := range ch {
if verbose {
log.Printf("archiving file %q with %d bytes", f.name, len(f.data))
}
if f.err != nil {
return f.err
}
zf, err := zw.Create(f.name)
if err != nil {
return errors.Wrapf(err, "failed to create %q", f.name)
}
if _, err := zf.Write(f.data); err != nil {
return errors.Wrapf(err, "failed to write to %q", f.name)
}
}
return nil
}
// TODO: Currently external services and site configs are pulled using the SRC_ENDPOINT env var,
// if theres a way to validate that the env var is pointing at the same instance as the docker and kubectl commands,
// it should be implemented.
// TODO: file issue on the existence of OAuth signKey which needs to be redacted
// getExternalServicesConfig calls src extsvc list with the format flag -f,
// and then returns an archiveFile to be consumed
func getExternalServicesConfig(ctx context.Context, baseDir string) *archiveFile {
const fmtStr = `{{range .Nodes}}{{.id}} | {{.kind}} | {{.displayName}}{{"\n"}}{{.config}}{{"\n---\n"}}{{end}}`
return archiveFileFromCommand(
ctx,
filepath.Join(baseDir, "config", "external_services.txt"),
os.Args[0], "extsvc", "list", "-f", fmtStr,
)
}
// getSiteConfig calls src api -query=... to query the api for site config json
func getSiteConfig(ctx context.Context, baseDir string) *archiveFile {
const siteConfigStr = `query { site { configuration { effectiveContents } } }`
f := archiveFileFromCommand(ctx,
filepath.Join(baseDir, "config", "siteConfig.json"),
os.Args[0], "api", "-query", siteConfigStr,
)
if f.err != nil {
return f
}
var siteConfig struct {
Data struct {
Site struct {
Configuration struct {
EffectiveContents string
}
}
}
}
if err := json.Unmarshal(f.data, &siteConfig); err != nil {
f.err = err
return f
}
f.data = []byte(siteConfig.Data.Site.Configuration.EffectiveContents)
return f
}