From fd9bc54cddd5698d818fb0fa292392b4858dece8 Mon Sep 17 00:00:00 2001 From: Till! Date: Wed, 1 Jul 2020 09:42:37 +0200 Subject: [PATCH] Fix: ensure surrounding directories exist (#10) Resolves: #9 --- .github/workflows/sync.yml | 2 +- action.yml | 2 +- repo/handler.go | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/.github/workflows/sync.yml b/.github/workflows/sync.yml index c8737d8..b5d4dec 100644 --- a/.github/workflows/sync.yml +++ b/.github/workflows/sync.yml @@ -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 }} diff --git a/action.yml b/action.yml index 758984c..0f4a6e5 100644 --- a/action.yml +++ b/action.yml @@ -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 }} diff --git a/repo/handler.go b/repo/handler.go index ea9e0dd..9abd07c 100644 --- a/repo/handler.go +++ b/repo/handler.go @@ -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) @@ -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 {