From 71f7a8bc85e3a15e5f0d2b6905794f3e2e022809 Mon Sep 17 00:00:00 2001
From: Ruslan Aliev <raliev@mirantis.com>
Date: Mon, 27 Jul 2020 19:37:04 -0500
Subject: [PATCH] Improve document pull command

This patch refactors some code and adds useful debug/info log output.

Change-Id: I590f6e5c300e5c91443af0a8fdb1073229602a67
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
Relates-To: #278
---
 cmd/document/document.go    |  2 +-
 cmd/document/pull.go        | 11 ++++-------
 cmd/document/pull_test.go   |  4 ++--
 pkg/document/pull/pull.go   |  6 +++++-
 pkg/document/repo/repo.go   | 11 +++++++++--
 pkg/environment/settings.go |  2 +-
 6 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/cmd/document/document.go b/cmd/document/document.go
index 1cdaf7029..fb56e841c 100644
--- a/cmd/document/document.go
+++ b/cmd/document/document.go
@@ -33,7 +33,7 @@ func NewDocumentCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Com
 		},
 	}
 
-	documentRootCmd.AddCommand(NewPullCommand(rootSettings, true))
+	documentRootCmd.AddCommand(NewPullCommand(rootSettings))
 	documentRootCmd.AddCommand(NewPluginCommand(rootSettings))
 
 	return documentRootCmd
diff --git a/cmd/document/pull.go b/cmd/document/pull.go
index 9dd9c1a2e..22f155885 100644
--- a/cmd/document/pull.go
+++ b/cmd/document/pull.go
@@ -22,18 +22,15 @@ import (
 )
 
 // NewPullCommand creates a new command for pulling airship document repositories
-// initConfig determines whether it's appropriate to load configuration from
-// disk; e.g. this is skipped when unit testing the command.
-func NewPullCommand(rootSettings *environment.AirshipCTLSettings, initConfig bool) *cobra.Command {
+func NewPullCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
 	settings := pull.Settings{AirshipCTLSettings: rootSettings}
 	documentPullCmd := &cobra.Command{
 		Use:   "pull",
 		Short: "Pulls documents from remote git repository",
 		RunE: func(cmd *cobra.Command, args []string) error {
-			if initConfig {
-				// Load or Initialize airship Config
-				rootSettings.InitConfig()
-			}
+			// Load or Initialize airship Config
+			rootSettings.InitConfig()
+
 			return settings.Pull()
 		},
 	}
diff --git a/cmd/document/pull_test.go b/cmd/document/pull_test.go
index e55cfa07e..e590f8cd9 100644
--- a/cmd/document/pull_test.go
+++ b/cmd/document/pull_test.go
@@ -25,7 +25,7 @@ import (
 )
 
 func getDummyAirshipSettings() *environment.AirshipCTLSettings {
-	settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig()}
+	settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig(), Create: true}
 
 	fx := fixtures.Basic().One()
 
@@ -50,7 +50,7 @@ func TestPull(t *testing.T) {
 		{
 			Name:    "document-pull-cmd",
 			CmdLine: "",
-			Cmd:     NewPullCommand(getDummyAirshipSettings(), false),
+			Cmd:     NewPullCommand(getDummyAirshipSettings()),
 		},
 	}
 
diff --git a/pkg/document/pull/pull.go b/pkg/document/pull/pull.go
index 25a665580..a8c708de4 100644
--- a/pkg/document/pull/pull.go
+++ b/pkg/document/pull/pull.go
@@ -17,6 +17,7 @@ package pull
 import (
 	"opendev.org/airship/airshipctl/pkg/document/repo"
 	"opendev.org/airship/airshipctl/pkg/environment"
+	"opendev.org/airship/airshipctl/pkg/log"
 )
 
 // Settings is a reference to environment.AirshipCTLSettings
@@ -41,12 +42,13 @@ func (s *Settings) Pull() error {
 func (s *Settings) cloneRepositories() error {
 	// Clone main repository
 	currentManifest, err := s.Config.CurrentContextManifest()
+	log.Debugf("Reading current context manifest information from %s", s.AirshipConfigPath)
 	if err != nil {
 		return err
 	}
 
 	// Clone repositories
-	for _, extraRepoConfig := range currentManifest.Repositories {
+	for repoName, extraRepoConfig := range currentManifest.Repositories {
 		err := extraRepoConfig.Validate()
 		if err != nil {
 			return err
@@ -55,6 +57,8 @@ func (s *Settings) cloneRepositories() error {
 		if err != nil {
 			return err
 		}
+		log.Printf("Downloading %s repository %s from %s into %s",
+			repoName, repository.Name, extraRepoConfig.URL(), currentManifest.TargetPath)
 		err = repository.Download(extraRepoConfig.ToCheckoutOptions(true).Force)
 		if err != nil {
 			return err
diff --git a/pkg/document/repo/repo.go b/pkg/document/repo/repo.go
index 70ed3f573..f660d77c4 100644
--- a/pkg/document/repo/repo.go
+++ b/pkg/document/repo/repo.go
@@ -22,6 +22,7 @@ import (
 	"github.com/go-git/go-billy/v5"
 	"github.com/go-git/go-billy/v5/osfs"
 	"github.com/go-git/go-git/v5"
+	"github.com/go-git/go-git/v5/plumbing"
 	"github.com/go-git/go-git/v5/plumbing/cache"
 	"github.com/go-git/go-git/v5/plumbing/transport"
 	"github.com/go-git/go-git/v5/storage"
@@ -105,11 +106,17 @@ func (repo *Repository) Update(force bool) error {
 
 // Checkout git repository, ToCheckoutOptions method will be used go get CheckoutOptions
 func (repo *Repository) Checkout(enforce bool) error {
-	log.Debugf("Attempting to checkout the repository %s", repo.Name)
 	if !repo.Driver.IsOpen() {
 		return ErrNoOpenRepo
 	}
 	co := repo.ToCheckoutOptions(enforce)
+	var branchHash string
+	if co.Hash == plumbing.ZeroHash {
+		branchHash = fmt.Sprintf("branch %s", co.Branch.String())
+	} else {
+		branchHash = fmt.Sprintf("commit hash %s", co.Hash.String())
+	}
+	log.Debugf("Attempting to checkout the repository %s from %s", repo.Name, branchHash)
 	tree, err := repo.Driver.Worktree()
 	if err != nil {
 		return fmt.Errorf("could not get worktree from the repo, %w", err)
@@ -125,7 +132,7 @@ func (repo *Repository) Open() error {
 
 // Clone given repository
 func (repo *Repository) Clone() error {
-	log.Debugf("Attempting to clone the repository %s", repo.Name)
+	log.Debugf("Attempting to clone the repository %s from %s", repo.Name, repo.URL())
 	auth, err := repo.ToAuth()
 	if err != nil {
 		return fmt.Errorf("failed to build auth options for repository %v: %w", repo.Name, err)
diff --git a/pkg/environment/settings.go b/pkg/environment/settings.go
index 6e3528b5b..ff42538d2 100644
--- a/pkg/environment/settings.go
+++ b/pkg/environment/settings.go
@@ -69,7 +69,7 @@ func (a *AirshipCTLSettings) InitFlags(cmd *cobra.Command) {
 	a.Create = false
 }
 
-// InitConfig - Initializes and loads Config it exists.
+// InitConfig - Initializes and loads Config if exists.
 func (a *AirshipCTLSettings) InitConfig() {
 	a.Config = config.NewConfig()