Skip to content

Commit

Permalink
Merge pull request #183 from se7entyse7en/separated-local-orgs-workdirs
Browse files Browse the repository at this point in the history
Separates local and orgs workdirs
  • Loading branch information
carlosms authored Aug 5, 2019
2 parents eff2ca7 + 5601dca commit 08fff1c
Show file tree
Hide file tree
Showing 11 changed files with 560 additions and 507 deletions.
44 changes: 24 additions & 20 deletions cmd/sourced/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,22 @@ type initLocalCmd struct {
}

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

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

// Before setting a new workdir, stop the current containers
compose.Run(context.Background(), "stop")

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

fmt.Printf("docker-compose working directory set to %s\n", dir)

if err := compose.Run(context.Background(), "up", "--detach"); err != nil {
if err := activate(wdHandler, wd); err != nil {
return err
}

Expand Down Expand Up @@ -89,27 +84,22 @@ type initOrgsCmd struct {
}

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

dir, err := workdir.InitWithOrgs(orgs, c.Token)
if err != nil {
orgs := c.orgsList()
if err := c.validate(orgs); err != nil {
return err
}

// Before setting a new workdir, stop the current containers
compose.Run(context.Background(), "stop")

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

fmt.Printf("docker-compose working directory set to %s\n", strings.Join(orgs, ","))

if err := compose.Run(context.Background(), "up", "--detach"); err != nil {
if err := activate(wdHandler, wd); err != nil {
return err
}

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

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

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

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

type authTransport struct {
token string
}
Expand All @@ -165,6 +168,7 @@ func (t *authTransport) RoundTrip(r *http.Request) (*http.Response, error) {

func init() {
c := rootCmd.AddCommand(&initCmd{})

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
4 changes: 2 additions & 2 deletions cmd/sourced/compose/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func List() ([]RevOrURL, error) {
}

func composeName(rev string) string {
if decoded, err := base64.StdEncoding.DecodeString(rev); err == nil {
if decoded, err := base64.URLEncoding.DecodeString(rev); err == nil {
return string(decoded)
}

Expand Down Expand Up @@ -248,7 +248,7 @@ func path(revOrURL RevOrURL) (string, error) {

subPath := revOrURL
if isURL(revOrURL) {
subPath = base64.StdEncoding.EncodeToString([]byte(revOrURL))
subPath = base64.URLEncoding.EncodeToString([]byte(revOrURL))
}

dirPath := filepath.Join(composeDirPath, subPath)
Expand Down
Loading

0 comments on commit 08fff1c

Please sign in to comment.