Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Changed

- Enhanced error reporting for errors that occur when cloning a project's
associated git repository ([PR #1351](https://github.com/ActiveState/cli/pull/1351))
- The State Tool now comes with the ActiveState Desktop application, which is
currently a system tray application.
- We've replaced the auto updating system with a manual updating system, updates
Expand Down
37 changes: 19 additions & 18 deletions pkg/cmdlets/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"gopkg.in/src-d/go-git.v4"

"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/output"
Expand Down Expand Up @@ -38,12 +37,12 @@ type Repo struct {
func (r *Repo) CloneProject(owner, name, path string, out output.Outputer) error {
project, err := model.FetchProjectByName(owner, name)
if err != nil {
return err
return locale.WrapError(err, "err_git_fetch_project", "Could not fetch project details")
}

tempDir, err := ioutil.TempDir("", fmt.Sprintf("state-activate-repo-%s-%s", owner, name))
if err != nil {
return errs.Wrap(err, "OS failure")
return locale.WrapError(err, "err_git_tempdir", "Could not create temporary directory for git clone operation")
}
defer os.RemoveAll(tempDir)

Expand All @@ -58,42 +57,39 @@ func (r *Repo) CloneProject(owner, name, path string, out output.Outputer) error
Progress: os.Stdout,
})
if err != nil {
return errs.Wrap(err, "Cmd failure")
return locale.WrapError(err, "err_clone_repo", "Could not clone repository with URL: {{.V0}}, error received: {{.V1}}.", *project.RepoURL, err.Error())
}

err = ensureCorrectRepo(owner, name, filepath.Join(tempDir, constants.ConfigFileName))
if err != nil {
return err
return locale.WrapError(err, "err_git_ensure_repo", "The activestate.yaml in the cloned repository does not match the project you are activating.")
}

err = moveFiles(tempDir, path)
if err != nil {
return err
return locale.WrapError(err, "err_git_move_files", "Could not move cloned files")
}

return nil
}

func ensureCorrectRepo(owner, name, projectFilePath string) error {
_, err := os.Stat(projectFilePath)
if os.IsNotExist(err) {
if !fileutils.FileExists(projectFilePath) {
return nil
}
if err != nil {
return errs.Wrap(err, "OS failure")
}

projectFile, err := projectfile.Parse(projectFilePath)
if err != nil {
return err
return locale.WrapError(err, "err_git_parse_projectfile", "Could not parse projectfile")
}

proj, err := project.NewLegacy(projectFile)
if err != nil {
return err
return locale.WrapError(err, "err_git_project", "Could not create new project from project file at: {{.V0}}", projectFile.Path())
}

if !(strings.ToLower(proj.Owner()) == strings.ToLower(owner)) || !(strings.ToLower(proj.Name()) == strings.ToLower(name)) {
return locale.NewError("ProjectURLMismatch")
return locale.NewError("err_git_project_url_mismatch", "Cloned project file does not match expected")
}

return nil
Expand All @@ -102,10 +98,15 @@ func ensureCorrectRepo(owner, name, projectFilePath string) error {
func moveFiles(src, dest string) error {
err := verifyDestinationDirectory(dest)
if err != nil {
return err
return locale.WrapError(err, "err_git_verify_dir", "Could not verify destination directory")
}

return fileutils.MoveAllFilesCrossDisk(src, dest)
err = fileutils.MoveAllFilesCrossDisk(src, dest)
if err != nil {
return locale.WrapError(err, "err_git_move_file", "Could not move files from {{.V0}} to {{.V1}}", src, dest)
}

return nil
}

func verifyDestinationDirectory(dest string) error {
Expand All @@ -115,10 +116,10 @@ func verifyDestinationDirectory(dest string) error {

empty, err := fileutils.IsEmptyDir(dest)
if err != nil {
return err
return locale.WrapError(err, "err_git_empty_dir", "Could not verify if destination directory is empty")
}
if !empty {
return locale.NewError("TargetDirInUse")
return locale.NewError("err_git_in_use", "Destination directory is not empty")
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmdlets/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (suite *GitTestSuite) TestEnsureCorrectRepo() {

func (suite *GitTestSuite) TestEnsureCorrectRepo_Mistmatch() {
err := ensureCorrectRepo("not-owner", "bad-project", filepath.Join(suite.dir, constants.ConfigFileName))
expected := locale.NewError("ProjectURLMismatch")
expected := locale.NewError("err_git_project_url_mismatch", "Cloned project file does not match expected")
suite.EqualError(err, expected.Error(), "expected errors to match")
}

Expand All @@ -150,7 +150,7 @@ func (suite *GitTestSuite) TestMoveFilesDirNoEmpty() {
suite.Require().NoError(err)

err = moveFiles(suite.dir, anotherDir)
expected := locale.NewError("TargetDirInUse")
expected := locale.WrapError(err, "err_git_verify_dir", "Could not verify destination directory")
suite.EqualError(err, expected.Error())
}

Expand Down