Skip to content

Commit

Permalink
Refactor workdir module by splitting into multiple modules and structs
Browse files Browse the repository at this point in the history
Signed-off-by: Lou Marvin Caraig <[email protected]>
  • Loading branch information
se7entyse7en committed Aug 5, 2019
1 parent 1dc2fea commit 5601dca
Show file tree
Hide file tree
Showing 7 changed files with 443 additions and 361 deletions.
33 changes: 19 additions & 14 deletions cmd/sourced/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,25 @@ type initLocalCmd struct {
Args struct {
Reposdir string `positional-arg-name:"workdir"`
} `positional-args:"yes"`

workdirFactory *workdir.Factory
}

func (c *initLocalCmd) Execute(args []string) error {
wdHandler, err := workdir.NewHandler()
if err != nil {
return err
}

reposdir, err := c.reposdirArg()
if err != nil {
return err
}

workdir, err := c.workdirFactory.InitLocal(reposdir)
wd, err := workdir.InitLocal(reposdir)
if err != nil {
return err
}

if err := activate(workdir); err != nil {
if err := activate(wdHandler, wd); err != nil {
return err
}

Expand Down Expand Up @@ -78,22 +81,25 @@ type initOrgsCmd struct {
Args struct {
Orgs []string `required:"yes"`
} `positional-args:"yes" required:"1"`

workdirFactory *workdir.Factory
}

func (c *initOrgsCmd) Execute(args []string) error {
wdHandler, err := workdir.NewHandler()
if err != nil {
return err
}

orgs := c.orgsList()
if err := c.validate(orgs); err != nil {
return err
}

workdir, err := c.workdirFactory.InitOrgs(orgs, c.Token)
wd, err := workdir.InitOrgs(orgs, c.Token)
if err != nil {
return err
}

if err := activate(workdir); err != nil {
if err := activate(wdHandler, wd); err != nil {
return err
}

Expand Down Expand Up @@ -138,16 +144,16 @@ func (c *initOrgsCmd) validate(orgs []string) error {
return nil
}

func activate(dir string) error {
func activate(wdHandler *workdir.Handler, workdir *workdir.Workdir) error {
// Before setting a new workdir, stop the current containers
compose.Run(context.Background(), "stop")

err := workdir.SetActive(dir)
err := wdHandler.SetActive(workdir)
if err != nil {
return err
}

fmt.Printf("docker-compose working directory set to %s\n", dir)
fmt.Printf("docker-compose working directory set to %s\n", workdir.Path)
return compose.Run(context.Background(), "up", "--detach")
}

Expand All @@ -163,7 +169,6 @@ func (t *authTransport) RoundTrip(r *http.Request) (*http.Response, error) {
func init() {
c := rootCmd.AddCommand(&initCmd{})

workdirFactory := &workdir.Factory{}
c.AddCommand(&initOrgsCmd{workdirFactory: workdirFactory})
c.AddCommand(&initLocalCmd{workdirFactory: workdirFactory})
c.AddCommand(&initOrgsCmd{})
c.AddCommand(&initLocalCmd{})
}
23 changes: 14 additions & 9 deletions cmd/sourced/cmd/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,34 @@ type pruneCmd struct {
}

func (c *pruneCmd) Execute(args []string) error {
workdirHandler, err := workdir.NewHandler()
if err != nil {
return err
}

if !c.All {
return c.pruneActive()
return c.pruneActive(workdirHandler)
}

dirs, err := workdir.ListPaths()
wds, err := workdirHandler.List()
if err != nil {
return err
}

for _, dir := range dirs {
if err := workdir.SetActivePath(dir); err != nil {
for _, wd := range wds {
if err := workdirHandler.SetActive(wd); err != nil {
return err
}

if err = c.pruneActive(); err != nil {
if err = c.pruneActive(workdirHandler); err != nil {
return err
}
}

return nil
}

func (c *pruneCmd) pruneActive() error {
func (c *pruneCmd) pruneActive(workdirHandler *workdir.Handler) error {
a := []string{"down", "--volumes"}
if c.Images {
a = append(a, "--rmi", "all")
Expand All @@ -47,16 +52,16 @@ func (c *pruneCmd) pruneActive() error {
return err
}

dir, err := workdir.ActivePath()
wd, err := workdirHandler.Active()
if err != nil {
return err
}

if err := workdir.RemovePath(dir); err != nil {
if err := workdirHandler.Remove(wd); err != nil {
return err
}

return workdir.UnsetActive()
return workdirHandler.UnsetActive()
}

func init() {
Expand Down
17 changes: 11 additions & 6 deletions cmd/sourced/cmd/workdirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ type workdirsCmd struct {
}

func (c *workdirsCmd) Execute(args []string) error {
dirs, err := workdir.List()
workdirHandler, err := workdir.NewHandler()
if err != nil {
return err
}

active, err := workdir.Active()
wds, err := workdirHandler.List()
if err != nil {
return err
}

for _, dir := range dirs {
if dir == active {
fmt.Printf("* %s\n", dir)
active, err := workdirHandler.Active()
if err != nil {
return err
}

for _, wd := range wds {
if wd.Path == active.Path {
fmt.Printf("* %s\n", wd.Name)
} else {
fmt.Printf(" %s\n", dir)
fmt.Printf(" %s\n", wd.Name)
}
}

Expand Down
25 changes: 17 additions & 8 deletions cmd/sourced/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const dockerComposeVersion = "1.24.0"
var composeContainerURL = fmt.Sprintf("https://github.com/docker/compose/releases/download/%s/run.sh", dockerComposeVersion)

type Compose struct {
bin string
bin string
workdirHandler *workdir.Handler
}

func (c *Compose) Run(ctx context.Context, arg ...string) error {
Expand All @@ -34,30 +35,38 @@ func (c *Compose) RunWithIO(ctx context.Context, stdin io.Reader,
stdout, stderr io.Writer, arg ...string) error {
cmd := exec.CommandContext(ctx, c.bin, arg...)

dir, err := workdir.ActivePath()
wd, err := c.workdirHandler.Active()
if err != nil {
return err
}

if err := workdir.ValidatePath(dir); err != nil {
if err := c.workdirHandler.Validate(wd); err != nil {
return err
}

cmd.Dir = dir
cmd.Dir = wd.Path
cmd.Stdin = stdin
cmd.Stdout = stdout
cmd.Stderr = stderr

return cmd.Run()
}

func NewCompose() (*Compose, error) {
func newCompose() (*Compose, error) {
workdirHandler, err := workdir.NewHandler()
if err != nil {
return nil, err
}

bin, err := getOrInstallComposeBinary()
if err != nil {
return nil, err
}

return &Compose{bin: bin}, nil
return &Compose{
bin: bin,
workdirHandler: workdirHandler,
}, nil
}

func getOrInstallComposeBinary() (string, error) {
Expand Down Expand Up @@ -118,7 +127,7 @@ func downloadCompose(path string) error {
}

func Run(ctx context.Context, arg ...string) error {
comp, err := NewCompose()
comp, err := newCompose()
if err != nil {
return err
}
Expand All @@ -127,7 +136,7 @@ func Run(ctx context.Context, arg ...string) error {
}

func RunWithIO(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, arg ...string) error {
comp, err := NewCompose()
comp, err := newCompose()
if err != nil {
return err
}
Expand Down
129 changes: 129 additions & 0 deletions cmd/sourced/compose/workdir/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package workdir

import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"

"github.com/pkg/errors"
composefile "github.com/src-d/sourced-ce/cmd/sourced/compose/file"
)

// InitLocal initializes the workdir for local path and returns the Workdir instance
func InitLocal(reposdir string) (*Workdir, error) {
dirName := encodeDirName(reposdir)

envf := envFile{
Workdir: dirName,
ReposDir: reposdir,
}

return initialize(dirName, "local", envf)
}

// InitOrgs initializes the workdir for organizations and returns the Workdir instance
func InitOrgs(orgs []string, token string) (*Workdir, error) {
// be indifferent to the order of passed organizations
sort.Strings(orgs)
dirName := encodeDirName(strings.Join(orgs, ","))

envf := envFile{
Workdir: dirName,
GithubOrganizations: orgs,
GithubToken: token,
}

return initialize(dirName, "orgs", envf)
}

func encodeDirName(dirName string) string {
return base64.URLEncoding.EncodeToString([]byte(dirName))
}

func buildAbsPath(dirName, subPath string) (string, error) {
path, err := workdirsPath()
if err != nil {
return "", err
}

return filepath.Join(path, subPath, dirName), nil
}

func initialize(dirName string, subPath string, envf envFile) (*Workdir, error) {
path, err := workdirsPath()
if err != nil {
return nil, err
}

workdir := filepath.Join(path, subPath, dirName)
if err != nil {
return nil, err
}

err = os.MkdirAll(workdir, 0755)
if err != nil {
return nil, errors.Wrap(err, "could not create working directory")
}

defaultFilePath, err := composefile.InitDefault()
if err != nil {
return nil, err
}

composePath := filepath.Join(workdir, "docker-compose.yml")
if err := link(defaultFilePath, composePath); err != nil {
return nil, err
}

defaultOverridePath, err := composefile.InitDefaultOverride()
if err != nil {
return nil, err
}

workdirOverridePath := filepath.Join(workdir, "docker-compose.override.yml")
if err := link(defaultOverridePath, workdirOverridePath); err != nil {
return nil, err
}

envPath := filepath.Join(workdir, ".env")
contents := envf.String()
err = ioutil.WriteFile(envPath, []byte(contents), 0644)

if err != nil {
return nil, errors.Wrap(err, "could not write .env file")
}

b := &builder{workdirsPath: path}
return b.build(workdir)
}

type envFile struct {
Workdir string
ReposDir string
GithubOrganizations []string
GithubToken string
}

func (f *envFile) String() string {
volumeType := "bind"
volumeSource := f.ReposDir
gitbaseSiva := ""
if f.ReposDir == "" {
volumeType = "volume"
volumeSource = "gitbase_repositories"
gitbaseSiva = "true"
}

return fmt.Sprintf(`COMPOSE_PROJECT_NAME=srcd-%s
GITBASE_VOLUME_TYPE=%s
GITBASE_VOLUME_SOURCE=%s
GITBASE_SIVA=%s
GITHUB_ORGANIZATIONS=%s
GITHUB_TOKEN=%s
`, f.Workdir, volumeType, volumeSource, gitbaseSiva,
strings.Join(f.GithubOrganizations, ","), f.GithubToken)
}
Loading

0 comments on commit 5601dca

Please sign in to comment.