Skip to content

Commit

Permalink
Merge pull request #7 from marcellodesales/develop
Browse files Browse the repository at this point in the history
Release - Replace Docker Handler
  • Loading branch information
marcellodesales authored Sep 8, 2020
2 parents afe11f5 + 256203f commit 970980e
Show file tree
Hide file tree
Showing 11 changed files with 438 additions and 396 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/develop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
- os: windows-latest
bin_name: cloner-windows-amd64.exe
steps:
- name: Download math result for job 1
- name: Download artifact ${{ matrix.bin_name }} from build
uses: actions/download-artifact@v2
with:
name: ${{ matrix.bin_name }}
Expand All @@ -72,12 +72,14 @@ jobs:
run: |
ls -la ./${BIN_NAME}
chmod +x ./${BIN_NAME}
file ./${BIN_NAME}
./${BIN_NAME}
- name: Install Windows dependencies
- name: Windows Binary Validation
if: matrix.os == 'windows-latest'
env:
BIN_NAME: ${{ matrix.bin_name }}
# https://stackoverflow.com/questions/53961802/how-to-use-an-environment-variable-in-powershell-command/53963070#53963070
run: |
dir
echo $pwd\$env:BIN_NAME
Expand Down
49 changes: 47 additions & 2 deletions api/git/git_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package git

import (
"errors"
"fmt"
"github.com/go-git/go-git/v5"
"github.com/marcellodesales/cloner/config"
"github.com/marcellodesales/cloner/util"
"os"
Expand Down Expand Up @@ -78,6 +80,26 @@ func (service GitServiceType) GetOrgLocalPath(gitRepoClone *GitRepoClone, config
return path.Join(config.Git.CloneBaseDir, gitRepoClone.Type.GetUserDir())
}

func (service GitServiceType) VerifyCloneDir(gitRepoClone *GitRepoClone, forceClone bool, config *config.Configuration) (bool, error) {
// The location is provided by the api
gitRepoClone.CloneLocation = service.GetRepoLocalPath(gitRepoClone, config)
//log.Debugf("Verifying if the clone path '%s' exists or exists and is empty", gitRepoClone.CloneLocation)

if util.DirExists(gitRepoClone.CloneLocation) {
if forceClone {
util.DeleteDir(gitRepoClone.CloneLocation)
return true, nil
}

// Verify if the user repo is not empty
dirIsEmpty, _ := util.IsDirEmpty(gitRepoClone.CloneLocation)
if !dirIsEmpty {
return false, errors.New(fmt.Sprintf("clone location '%s' exists and it's not empty", gitRepoClone.CloneLocation))
}
}
return false, nil
}

/**
* Makes the base clone dir is the org and parts of the repo dir since the clone service creates the repo dir
*/
Expand All @@ -92,11 +114,34 @@ func (service GitServiceType) MakeCloneDir(gitRepoClone *GitRepoClone, config *c
}
}

gitRepoClone.CloneLocation = baseCloneDir

err := os.MkdirAll(gitRepoClone.CloneLocation, 0755)
if err != nil {
return err
}

gitRepoClone.CloneLocation = service.GetRepoLocalPath(gitRepoClone, config)
return nil
}

/**
* Clone the git repo to the clone location using go-git
*/
func (service GitServiceType) GoCloneRepo(gitRepoClone *GitRepoClone, config *config.Configuration) error {
gitRepoClone.CloneLocation = service.GetRepoLocalPath(gitRepoClone, config)

// https://git-scm.com/book/en/v2/Appendix-B%3A-Embedding-Git-in-your-Applications-go-git
_, err := git.PlainClone(gitRepoClone.CloneLocation, false, &git.CloneOptions{
URL: gitRepoClone.Url,
Progress: os.Stdout,
})
return err
}

/**
* Print the list of the files in the dir just like the "tree" unix command
*/
func (service GitServiceType) GoPrintTree(gitRepoClone *GitRepoClone) (string, error) {
// Exclude the .git dir from the list
excludedList := []string{".git"}
return util.GetDirTree(gitRepoClone.CloneLocation, excludedList)
}
117 changes: 0 additions & 117 deletions api/git/git_service_docker.go

This file was deleted.

23 changes: 15 additions & 8 deletions cmd/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,29 @@ var initCmd = &cobra.Command{
}

forceClone, _ := cmd.Flags().GetBool("force")
err = git.GitService.VerifyCloneDir(gitRepo, forceClone, config.INSTANCE)
if forceClone {
log.Info("Forcing clone...")
}

deletedExistingDir, err := git.GitService.VerifyCloneDir(gitRepo, forceClone, config.INSTANCE)
if deletedExistingDir {
log.Infof("Deleted dir '%s'", gitRepo.CloneLocation)
}
if err != nil {
log.Errorf("Can't clone repo: %v", err)
log.Errorf("You can specify --force or -f to delete the existing dir and clone again. Make sure there are no panding changes!")
log.Errorf("You can specify --force or -f to delete the existing dir and clone again. " +
"Make sure there are no panding changes!")
os.Exit(3)
}

log.Infof("Cloning the provided repo at '%s'", gitRepo.CloneLocation)

err = git.GitService.MakeCloneDir(gitRepo, config.INSTANCE)
if err != nil {
log.Errorf("Can't create the base clone repo '%s': %v", gitRepo.Type.GetUserDir(), err)
os.Exit(4)
}

_, err = git.GitService.DockerGitClone(gitRepo, config.INSTANCE)
log.Infof("Cloning into '%s'", gitRepo.CloneLocation)
err = git.GitService.GoCloneRepo(gitRepo, config.INSTANCE)
if err != nil {
log.Errorf("Can't clone the repo at '%s': %v", gitRepo.Type.GetRepoDir(), err)
os.Exit(5)
Expand All @@ -70,14 +77,14 @@ var initCmd = &cobra.Command{
}

// Show the files cloned
stdout, err := git.GitService.DockerFilesTree(gitRepo, config.INSTANCE)
filesListTree, err := git.GitService.GoPrintTree(gitRepo)
if err != nil {
log.Errorf("Can't show the cloned repo tree '%s': %v", gitRepo.Type.GetRepoDir(), err)
os.Exit(4)
}

if !util.IsLogInDebug() {
log.Infof("\n%s", stdout)
if util.IsLogInDebug() {
log.Infof("\n%s", filesListTree)
}
},
}
Expand Down
10 changes: 0 additions & 10 deletions config/config_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ import (
* https://godoc.org/github.com/mitchellh/mapstructure
*/
type Git struct {
// The name of the protos used by the generated Stubs
DockerImage string `mapstructure:"dockerImage"`

CloneBaseDir string `mapstructure:"cloneBaseDir"`

// The list of plugins to initialize
Expand All @@ -55,9 +52,6 @@ func parseGitConfig() (*Git, error) {
}

func setDefaultCliValues(git *Git) {
if git.DockerImage == "" {
git.DockerImage = "alpine/git"
}
homeDir, _ := os.UserHomeDir()
if git.CloneBaseDir == "" {
git.CloneBaseDir = path.Join(homeDir, "cloner")
Expand All @@ -72,10 +66,6 @@ func setDefaultCliValues(git *Git) {
* Validate the initialization
*/
func validateInitConfig(git *Git) error {
if git.DockerImage == "" {
git.DockerImage = "alpine/git"
return nil
}
if git.CloneBaseDir == "" {
return errors.New("you must provide the clone base dir")
}
Expand Down
18 changes: 7 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ module github.com/marcellodesales/cloner
go 1.13

require (
github.com/jinzhu/copier v0.0.0-20190625015134-976e0346caa8
github.com/disiqueira/gotree v1.0.0
github.com/go-git/go-git/v5 v5.1.0
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.1.2
github.com/prometheus/common v0.4.0
github.com/sirupsen/logrus v1.2.0
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/thoas/go-funk v0.4.0
github.com/uber/prototool v1.8.1
gopkg.in/yaml.v2 v2.2.2
github.com/mitchellh/mapstructure v1.3.3
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.1
github.com/thoas/go-funk v0.7.0
)

replace google.golang.org/genproto v0.0.0-20170818100345-ee236bd376b0 => google.golang.org/genproto v0.0.0-20170818010345-ee236bd376b0
Loading

0 comments on commit 970980e

Please sign in to comment.