Skip to content

Commit

Permalink
Fix: ensure surrounding directories exist (#10)
Browse files Browse the repository at this point in the history
Resolves: #9
  • Loading branch information
till committed Jul 1, 2020
1 parent 0e41d94 commit fd9bc54
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Sync workflow
uses: hostwithquantum/github-org-sync-action@0.5.3
uses: hostwithquantum/github-org-sync-action@0.5.4
with:
github-user: ${{ secrets.GH_USER }}
github-email: ${{ secrets.GH_EMAIL }}
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ inputs:

runs:
using: 'docker'
image: 'docker://quay.io/hostwithquantum/github-org-sync:v0.5.3'
image: 'docker://quay.io/hostwithquantum/github-org-sync:v0.5.4'
env:
GITHUB_USER: ${{ inputs.github-user }}
GITHUB_EMAIL: ${{ inputs.github-email }}
Expand Down
33 changes: 31 additions & 2 deletions repo/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ func NewHandler(repository string, baseDirectory string) Handler {

// Sync ...
func (h Handler) Sync(target string) {
dotGithub := filepath.Join(target, ".github")
if ensureDirectory(dotGithub) == false {
return
}

workflows := filepath.Join(dotGithub, "workflows")
if ensureDirectory(workflows) == false {
return
}

for _, file := range h.Workflows {
repoFile := fmt.Sprintf(
"%s/.github/workflows/%s", target, filepath.Base(file))
repoFile := filepath.Join(workflows, filepath.Base(file))

err := copy(file, repoFile)
CheckIfError(err)
Expand All @@ -37,6 +46,26 @@ func (h Handler) Sync(target string) {
log.Debugf("Synced files to '%s'", target)
}

func ensureDirectory(directory string) bool {
stat, err := os.Stat(directory)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(directory, os.ModePerm)
}

// handle all errors
CheckIfError(err)
return true
}

if stat.IsDir() {
return true
}

log.Errorf("Path '%s' exists, but is not a directory!", directory)
return false
}

func copy(src string, dst string) error {
in, err := os.Open(src)
if err != nil {
Expand Down

0 comments on commit fd9bc54

Please sign in to comment.