|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/sourcegraph/src-cli/internal/exec" |
| 14 | + |
| 15 | + "github.com/sourcegraph/sourcegraph/lib/errors" |
| 16 | +) |
| 17 | + |
| 18 | +type archiveFile struct { |
| 19 | + name string |
| 20 | + data []byte |
| 21 | + err error |
| 22 | +} |
| 23 | + |
| 24 | +func archiveFileFromCommand(ctx context.Context, path, cmd string, args ...string) *archiveFile { |
| 25 | + f := &archiveFile{name: path} |
| 26 | + f.data, f.err = exec.CommandContext(ctx, cmd, args...).CombinedOutput() |
| 27 | + if f.err != nil { |
| 28 | + f.err = errors.Wrapf(f.err, "executing command: %s %s: received error: %s", cmd, strings.Join(args, " "), f.data) |
| 29 | + } |
| 30 | + return f |
| 31 | +} |
| 32 | + |
| 33 | +// verify prompts the user to confirm they want to run the command |
| 34 | +func verify(confirmationText string) (bool, error) { |
| 35 | + input := "" |
| 36 | + for strings.ToLower(input) != "y" && strings.ToLower(input) != "n" { |
| 37 | + fmt.Printf("%s [y/N]: ", confirmationText) |
| 38 | + if _, err := fmt.Scanln(&input); err != nil { |
| 39 | + return false, err |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return strings.ToLower(input) == "y", nil |
| 44 | +} |
| 45 | + |
| 46 | +func processBaseDir(base string) (string, string) { |
| 47 | + if !strings.HasSuffix(base, ".zip") { |
| 48 | + return base + ".zip", base |
| 49 | + } |
| 50 | + |
| 51 | + return base, strings.TrimSuffix(base, ".zip") |
| 52 | +} |
| 53 | + |
| 54 | +// write all the outputs from an archive command passed on the channel to to the zip writer |
| 55 | +func writeChannelContentsToZip(zw *zip.Writer, ch <-chan *archiveFile, verbose bool) error { |
| 56 | + for f := range ch { |
| 57 | + if verbose { |
| 58 | + log.Printf("archiving file %q with %d bytes", f.name, len(f.data)) |
| 59 | + } |
| 60 | + |
| 61 | + if f.err != nil { |
| 62 | + return f.err |
| 63 | + } |
| 64 | + |
| 65 | + zf, err := zw.Create(f.name) |
| 66 | + if err != nil { |
| 67 | + return errors.Wrapf(err, "failed to create %q", f.name) |
| 68 | + } |
| 69 | + |
| 70 | + if _, err := zf.Write(f.data); err != nil { |
| 71 | + return errors.Wrapf(err, "failed to write to %q", f.name) |
| 72 | + } |
| 73 | + } |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +// TODO: Currently external services and site configs are pulled using the SRC_ENDPOINT env var, |
| 78 | +// if theres a way to validate that the env var is pointing at the same instance as the docker and kubectl commands, |
| 79 | +// it should be implemented. |
| 80 | + |
| 81 | +// TODO: file issue on the existence of OAuth signKey which needs to be redacted |
| 82 | + |
| 83 | +// getExternalServicesConfig calls src extsvc list with the format flag -f, |
| 84 | +// and then returns an archiveFile to be consumed |
| 85 | +func getExternalServicesConfig(ctx context.Context, baseDir string) *archiveFile { |
| 86 | + const fmtStr = `{{range .Nodes}}{{.id}} | {{.kind}} | {{.displayName}}{{"\n"}}{{.config}}{{"\n---\n"}}{{end}}` |
| 87 | + return archiveFileFromCommand( |
| 88 | + ctx, |
| 89 | + filepath.Join(baseDir, "config", "external_services.txt"), |
| 90 | + os.Args[0], "extsvc", "list", "-f", fmtStr, |
| 91 | + ) |
| 92 | +} |
| 93 | + |
| 94 | +// getSiteConfig calls src api -query=... to query the api for site config json |
| 95 | +func getSiteConfig(ctx context.Context, baseDir string) *archiveFile { |
| 96 | + const siteConfigStr = `query { site { configuration { effectiveContents } } }` |
| 97 | + f := archiveFileFromCommand(ctx, |
| 98 | + filepath.Join(baseDir, "config", "siteConfig.json"), |
| 99 | + os.Args[0], "api", "-query", siteConfigStr, |
| 100 | + ) |
| 101 | + |
| 102 | + if f.err != nil { |
| 103 | + return f |
| 104 | + } |
| 105 | + |
| 106 | + var siteConfig struct { |
| 107 | + Data struct { |
| 108 | + Site struct { |
| 109 | + Configuration struct { |
| 110 | + EffectiveContents string |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if err := json.Unmarshal(f.data, &siteConfig); err != nil { |
| 117 | + f.err = err |
| 118 | + return f |
| 119 | + } |
| 120 | + |
| 121 | + f.data = []byte(siteConfig.Data.Site.Configuration.EffectiveContents) |
| 122 | + return f |
| 123 | +} |
0 commit comments