Skip to content

Commit

Permalink
Merge branch '32-fix-copy' into 'master'
Browse files Browse the repository at this point in the history
Resolve "Fix copy destination"

Closes #32

See merge request asannikov/mgt!32
  • Loading branch information
asannikov committed Jan 18, 2021
2 parents b24c6d3 + 6adccca commit 8772d69
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 28 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGELOG

## 1.7.3 (19.01.2021)
- bugfix
- fix sync destination option

- features
- add force flag for copyfrom

## 1.7.2 (17.01.2021)
- bugfix
- refactoring for commands, added general type that contains all func parameters
Expand Down
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ I was inspired by [Mark's Shust](https://github.com/markshust/docker-magento) so

[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/asannikov/jumper/blob/master/LICENSE)
[![Build Status](https://travis-ci.com/asannikov/jumper.svg?branch=master)](https://travis-ci.com/asannikov/jumper)
[![Release](https://img.shields.io/badge/release-1.7.2-brightgreen.svg)](https://github.com/asannikov/jumper/releases/tag/v1.7.2)
[![Release](https://img.shields.io/badge/release-1.7.3-brightgreen.svg)](https://github.com/asannikov/jumper/releases/tag/v1.7.3)

It was not tested on Windows.

Expand Down Expand Up @@ -102,7 +102,7 @@ Every stable release has attached sources for "linux/amd64", "linux/386", "darwi

For example:
```
https://github.com/asannikov/jumper/releases/tag/v1.7.2
https://github.com/asannikov/jumper/releases/tag/v1.7.3
```
Find related source there and download it. Now you can place use source at any place you want on your machine or make it global in a standard way.

Expand Down
48 changes: 37 additions & 11 deletions app/command/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/urfave/cli/v2"
)

const syncCopyFrom = "copyfrom"
const syncCopyTo = "copyto"

type sync struct {
usage map[string]string
aliases map[string]string
Expand All @@ -33,10 +37,12 @@ type syncProjectConfig interface {
func getSyncArgs(cfg syncProjectConfig, direction string, syncPath string, projectRoot string) []string {
projectRoot = strings.TrimRight(projectRoot, string(os.PathSeparator))

args := []string{"cp", projectRoot + syncPath, cfg.GetProjectMainContainer() + ":" + strings.TrimRight(cfg.GetProjectDockerPath(), string(os.PathSeparator)) + syncPath}
targetPath := filepath.Dir(syncPath)

args := []string{"cp", projectRoot + syncPath, cfg.GetProjectMainContainer() + ":" + strings.TrimRight(cfg.GetProjectDockerPath(), string(os.PathSeparator)) + targetPath}

if direction == "copyfrom" {
args = []string{"cp", cfg.GetProjectMainContainer() + ":" + strings.TrimRight(cfg.GetProjectDockerPath(), string(os.PathSeparator)) + syncPath, projectRoot + syncPath}
if direction == syncCopyFrom {
args = []string{"cp", cfg.GetProjectMainContainer() + ":" + strings.TrimRight(cfg.GetProjectDockerPath(), string(os.PathSeparator)) + syncPath, projectRoot + targetPath}
}

return args
Expand All @@ -60,25 +66,36 @@ func SyncCommand(direction string, cfg syncProjectConfig, d syncCommandDialog, o

s := &sync{
usage: map[string]string{
"copyto": "Sync local -> docker container, set related path, ie `vendor/folder/` for syncing as a parameter, or use --all to sync all project",
"copyfrom": "Sync docker container -> local, set related path, ie `vendor/folder/` for syncing as a parameter, or use --all to sync all project",
syncCopyTo: "Sync local -> docker container, set related path, ie `vendor/folder/` for syncing as a parameter, or use --all to sync all project",
syncCopyFrom: "Sync docker container -> local, set related path, ie `vendor/folder/` for syncing as a parameter, or use --all to sync all project",
},
aliases: map[string]string{
"copyto": "cpt",
"copyfrom": "cpf",
syncCopyTo: "cpt",
syncCopyFrom: "cpf",
},
description: map[string]string{
"copyto": "phpContainer is taken from project config file",
"copyfrom": "phpContainer is taken from project config file, php and composer commands will be found automatically",
syncCopyTo: "phpContainer is taken from project config file",
syncCopyFrom: "phpContainer is taken from project config file, php and composer commands will be found automatically",
},
}

flags := []cli.Flag{}

if direction == syncCopyFrom {
flags = append(flags, &cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Force create directory for file if it does not exist",
})
}

return &cli.Command{
Name: direction,
Aliases: []string{s.aliases[direction]},
Usage: s.usage[direction],
Description: s.description[direction],
SkipFlagParsing: true,
SkipFlagParsing: false,
Flags: flags,
Action: func(c *cli.Context) (err error) {
syncPath := c.Args().First()

Expand All @@ -105,11 +122,20 @@ func SyncCommand(direction string, cfg syncProjectConfig, d syncCommandDialog, o

args := getSyncArgs(cfg, direction, syncPath, currentPath)

if direction == syncCopyFrom && c.Bool("f") == true {
fmt.Printf("Path %s was created", args[2]+filepath.Base(syncPath))
err = os.MkdirAll(args[2]+filepath.Base(syncPath), os.ModePerm)
}

if err != nil {
return err
}

if err = execCommand("docker", args, c.App); err != nil {
return err
}

if direction == "copyto" {
if direction == syncCopyTo {
fmt.Printf("Completed copying %s files from host to container %s \n", syncPath, cfg.GetProjectMainContainer())
} else {
fmt.Printf("Completed copying %s from container %s to host\n", syncPath, cfg.GetProjectMainContainer())
Expand Down
28 changes: 14 additions & 14 deletions app/command/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,44 @@ func TestGetSyncArgs(t *testing.T) {
projectDockerPath: "/var/www/html/",
}
args := getSyncArgs(cfg, "copyfrom", getSyncPath("/vendor/path"), "/path/to/local/project/")
assert.Equal(t, "cp phpfmp:/var/www/html/vendor/path /path/to/local/project/vendor/path", strings.Join(args, " "))
assert.Equal(t, "cp phpfmp:/var/www/html/vendor/path /path/to/local/project/vendor", strings.Join(args, " "))

args = getSyncArgs(cfg, "copyfrom", getSyncPath("/vendor/path/file.php"), "/path/to/local/project/")
assert.Equal(t, "cp phpfmp:/var/www/html/vendor/path/file.php /path/to/local/project/vendor/path/file.php", strings.Join(args, " "))
assert.Equal(t, "cp phpfmp:/var/www/html/vendor/path/file.php /path/to/local/project/vendor/path", strings.Join(args, " "))

args = getSyncArgs(cfg, "copyto", getSyncPath("/vendor/path/file.php"), "/path/to/local/project/")
assert.Equal(t, "cp /path/to/local/project/vendor/path/file.php phpfmp:/var/www/html/vendor/path/file.php", strings.Join(args, " "))
assert.Equal(t, "cp /path/to/local/project/vendor/path/file.php phpfmp:/var/www/html/vendor/path", strings.Join(args, " "))

args = getSyncArgs(cfg, "copyto", getSyncPath("/vendor/path/"), "/path/to/local/project/")
assert.Equal(t, "cp /path/to/local/project/vendor/path phpfmp:/var/www/html/vendor/path", strings.Join(args, " "))
assert.Equal(t, "cp /path/to/local/project/vendor/path phpfmp:/var/www/html/vendor", strings.Join(args, " "))

args = getSyncArgs(cfg, "copyto", getSyncPath("/vendor/path"), "/path/to/local/project/")
assert.Equal(t, "cp /path/to/local/project/vendor/path phpfmp:/var/www/html/vendor/path", strings.Join(args, " "))
assert.Equal(t, "cp /path/to/local/project/vendor/path phpfmp:/var/www/html/vendor", strings.Join(args, " "))

args = getSyncArgs(cfg, "copyto", getSyncPath("/vendor/path"), "/path/to/local/project")
assert.Equal(t, "cp /path/to/local/project/vendor/path phpfmp:/var/www/html/vendor/path", strings.Join(args, " "))
assert.Equal(t, "cp /path/to/local/project/vendor/path phpfmp:/var/www/html/vendor", strings.Join(args, " "))

args = getSyncArgs(cfg, "copyto", getSyncPath("--all"), "/path/to/local/project")
assert.Equal(t, "cp /path/to/local/project/./ phpfmp:/var/www/html/./", strings.Join(args, " "))
assert.Equal(t, "cp /path/to/local/project/./ phpfmp:/var/www/html/", strings.Join(args, " "))

args = getSyncArgs(cfg, "copyfrom", getSyncPath("--all"), "/path/to/local/project")
assert.Equal(t, "cp phpfmp:/var/www/html/./ /path/to/local/project/./", strings.Join(args, " "))
assert.Equal(t, "cp phpfmp:/var/www/html/./ /path/to/local/project/", strings.Join(args, " "))

args = getSyncArgs(cfg, "copyfrom", getSyncPath("/vendor/path"), "/path/to/local/project/")
assert.Equal(t, "cp phpfmp:/var/www/html/vendor/path /path/to/local/project/vendor/path", strings.Join(args, " "))
assert.Equal(t, "cp phpfmp:/var/www/html/vendor/path /path/to/local/project/vendor", strings.Join(args, " "))

args = getSyncArgs(cfg, "copyfrom", getSyncPath("vendor/path/file.php"), "/path/to/local/project/")
assert.Equal(t, "cp phpfmp:/var/www/html/vendor/path/file.php /path/to/local/project/vendor/path/file.php", strings.Join(args, " "))
assert.Equal(t, "cp phpfmp:/var/www/html/vendor/path/file.php /path/to/local/project/vendor/path", strings.Join(args, " "))

args = getSyncArgs(cfg, "copyto", getSyncPath("vendor/path/file.php"), "/path/to/local/project/")
assert.Equal(t, "cp /path/to/local/project/vendor/path/file.php phpfmp:/var/www/html/vendor/path/file.php", strings.Join(args, " "))
assert.Equal(t, "cp /path/to/local/project/vendor/path/file.php phpfmp:/var/www/html/vendor/path", strings.Join(args, " "))

args = getSyncArgs(cfg, "copyto", getSyncPath("vendor/path/"), "/path/to/local/project/")
assert.Equal(t, "cp /path/to/local/project/vendor/path phpfmp:/var/www/html/vendor/path", strings.Join(args, " "))
assert.Equal(t, "cp /path/to/local/project/vendor/path phpfmp:/var/www/html/vendor", strings.Join(args, " "))

args = getSyncArgs(cfg, "copyto", getSyncPath("/vendor/path"), "/path/to/local/project/")
assert.Equal(t, "cp /path/to/local/project/vendor/path phpfmp:/var/www/html/vendor/path", strings.Join(args, " "))
assert.Equal(t, "cp /path/to/local/project/vendor/path phpfmp:/var/www/html/vendor", strings.Join(args, " "))

args = getSyncArgs(cfg, "copyto", getSyncPath("/vendor/path"), "/path/to/local/project")
assert.Equal(t, "cp /path/to/local/project/vendor/path phpfmp:/var/www/html/vendor/path", strings.Join(args, " "))
assert.Equal(t, "cp /path/to/local/project/vendor/path phpfmp:/var/www/html/vendor", strings.Join(args, " "))
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/urfave/cli/v2"
)

const version = "1.7.2"
const version = "1.7.3"

func main() {

Expand Down

0 comments on commit 8772d69

Please sign in to comment.