Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src migrate early WIP #876

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions cmd/src/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"context"
"flag"
"fmt"
"os/exec"
"strings"

"github.com/sourcegraph/sourcegraph/lib/errors"
)

type genericData struct {
data []byte
err error
}

func runDockerCommand(ctx context.Context, cmd string, args ...string) *genericData {
f := &genericData{}

f.data, f.err = exec.Command(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
}

func init() {
usage := `'src migrate' helps you migrate your sourcegraph data to another instance.

Usage:

TBD

Examples:

TBD
`

var container string
var outputPath string

flagSet := flag.NewFlagSet("migrate", flag.ExitOnError)
flagSet.StringVar(&container, "c", "", "The container to target")
flagSet.StringVar(&outputPath, "o", "", "The path for the output file")

handler := func(args []string) error {
fmt.Fprintf(flag.CommandLine.Output(), "From container: %s'", container)

// init context
ctx := context.Background()

data := runDockerCommand(
ctx,
"docker", "exec", container, "sh", "-c", "'pg_dump -C --username=postgres sourcegraph' > "+outputPath,
)
if data.err != nil {
fmt.Println("Error:", data.err)
}

return nil
}

// Register the command.
commands = append(commands, &command{
flagSet: flagSet,
aliases: []string{"migrate"},
handler: handler,
usageFunc: func() {
fmt.Println(usage)
},
})
}