Skip to content

Commit

Permalink
Merge pull request #30 from ShotaKitazawa/issue/7
Browse files Browse the repository at this point in the history
Issue/7
  • Loading branch information
ShotaKitazawa authored Jul 22, 2022
2 parents dd5a479 + acf8e91 commit 210c03a
Show file tree
Hide file tree
Showing 17 changed files with 437 additions and 8 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: test
on: push

jobs:
unit-test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
with:
fetch-depth: 1
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.17
- name: Go Test
run: go test -v ./...

4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ go 1.17

require (
github.com/cheggaaa/pb v1.0.29
github.com/golang/mock v1.6.0
github.com/pkg/sftp v1.13.4
github.com/slack-go/slack v0.10.3
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
go.uber.org/zap v1.21.0
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
Expand All @@ -15,12 +17,12 @@ require (
)

require (
github.com/benbjohnson/clock v1.1.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
Expand Down Expand Up @@ -91,6 +93,7 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func runDeploy(
return err
}
// Deploy
if err = deployer.Deploy(ctx, host.Deploy.Targets); err != nil {
if err = deployer.Deploy(ctx, host.Host, host.Deploy.Targets); err != nil {
return err
}
// Execute postCommand
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func runImport(
if err != nil {
return err
}
files = importer.ExcludeSymlinkFiles(ctx, files)
for _, file := range files {
fileAbsPath := filepath.Join(target.Target, file)
content, mode, err := importer.GetFileContent(ctx, fileAbsPath)
Expand Down
101 changes: 101 additions & 0 deletions pkg/shell/mock/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 33 additions & 6 deletions pkg/usecases/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -51,28 +52,54 @@ func new(logger *zap.Logger, s shell.Iface, templator *template.Templator, local
return &Deployer{logger, s, templator, localRepoPath}
}

func (d Deployer) Deploy(ctx context.Context, targets []config.DeployTarget) error {
func (d Deployer) Deploy(ctx context.Context, host string, targets []config.DeployTarget) error {
realHost := d.shell.Host()
for _, target := range targets {
src := filepath.Join(d.localRepoPath, d.shell.Host(), target.Src)
src := filepath.Join(d.localRepoPath, host, target.Src)
if err := filepath.WalkDir(src, func(path string, info fs.DirEntry, err error) error {
if info != nil && !reflect.ValueOf(info).IsNil() && !info.IsDir() {
dst := filepath.Join(target.Target, strings.TrimPrefix(path, src))
dirname := filepath.Dir(dst)
if _, _, err := d.shell.Execf(ctx, "", `test -d "%s"`, dirname); err != nil {
d.log.Debug(fmt.Sprintf("%s does not exist, mkdir", dirname), zap.String("host", d.shell.Host()))
d.log.Debug(fmt.Sprintf("%s does not exist, mkdir", dirname), zap.String("host", realHost))
if _, _, err := d.shell.Execf(ctx, "", `mkdir -p "%s"`, dirname); err != nil {
return err
}
}
d.log.Debug(fmt.Sprintf("deploy %s to %s", path, dst), zap.String("host", d.shell.Host()))
return d.shell.Deploy(ctx, path, dst)
finfo, err := info.Info()
if err != nil {
return err
}
if finfo.Mode()&os.ModeSymlink == os.ModeSymlink { // copy source is symlink
origin, err := filepath.EvalSymlinks(path)
if err != nil {
return err
}
newHostAndSrc := strings.TrimPrefix(origin, d.localRepoPath+"/")
if newHostAndSrc == origin {
return fmt.Errorf("%s: cannot seek symlink bacause source of symlic isn't in localRepoPath", origin)
}
newHostAndSrcSlice := strings.Split(newHostAndSrc, "/")
newHost := newHostAndSrcSlice[0]
newSrc := strings.Join(newHostAndSrcSlice[1:], "/")
if len(newHostAndSrcSlice) < 2 {
return fmt.Errorf("%s: cannot seek symlink bacause this directory is hostdir", origin)
}
d.log.Debug(fmt.Sprintf("%s is symlink, seek to %s", path, origin), zap.String("host", realHost))
if err := d.Deploy(ctx, newHost, []config.DeployTarget{{Src: newSrc, Target: dst}}); err != nil {
return err
}
} else { // copy source is file
d.log.Debug(fmt.Sprintf("deploy %s to %s", path, dst), zap.String("host", realHost))
return d.shell.Deploy(ctx, path, dst)
}
}
return nil
}); err != nil {
return err
}
if target.Compile != "" {
d.log.Debug(fmt.Sprintf(`exec compile: "%s"`, target.Compile), zap.String("host", d.shell.Host()))
d.log.Debug(fmt.Sprintf(`exec compile: "%s"`, target.Compile), zap.String("host", host))
if _, stderr, err := d.shell.Exec(ctx, target.Target, target.Compile); err != nil {
return myerrros.NewErrorCommandExecutionFailed(stderr)
}
Expand Down
Loading

0 comments on commit 210c03a

Please sign in to comment.