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
This commit is contained in:
Ruslan Aliev 2020-07-27 19:37:04 -05:00
parent e20c8ae1fa
commit 71f7a8bc85
6 changed files with 22 additions and 14 deletions

View File

@ -33,7 +33,7 @@ func NewDocumentCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Com
}, },
} }
documentRootCmd.AddCommand(NewPullCommand(rootSettings, true)) documentRootCmd.AddCommand(NewPullCommand(rootSettings))
documentRootCmd.AddCommand(NewPluginCommand(rootSettings)) documentRootCmd.AddCommand(NewPluginCommand(rootSettings))
return documentRootCmd return documentRootCmd

View File

@ -22,18 +22,15 @@ import (
) )
// NewPullCommand creates a new command for pulling airship document repositories // NewPullCommand creates a new command for pulling airship document repositories
// initConfig determines whether it's appropriate to load configuration from func NewPullCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
// disk; e.g. this is skipped when unit testing the command.
func NewPullCommand(rootSettings *environment.AirshipCTLSettings, initConfig bool) *cobra.Command {
settings := pull.Settings{AirshipCTLSettings: rootSettings} settings := pull.Settings{AirshipCTLSettings: rootSettings}
documentPullCmd := &cobra.Command{ documentPullCmd := &cobra.Command{
Use: "pull", Use: "pull",
Short: "Pulls documents from remote git repository", Short: "Pulls documents from remote git repository",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if initConfig {
// Load or Initialize airship Config // Load or Initialize airship Config
rootSettings.InitConfig() rootSettings.InitConfig()
}
return settings.Pull() return settings.Pull()
}, },
} }

View File

@ -25,7 +25,7 @@ import (
) )
func getDummyAirshipSettings() *environment.AirshipCTLSettings { func getDummyAirshipSettings() *environment.AirshipCTLSettings {
settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig()} settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig(), Create: true}
fx := fixtures.Basic().One() fx := fixtures.Basic().One()
@ -50,7 +50,7 @@ func TestPull(t *testing.T) {
{ {
Name: "document-pull-cmd", Name: "document-pull-cmd",
CmdLine: "", CmdLine: "",
Cmd: NewPullCommand(getDummyAirshipSettings(), false), Cmd: NewPullCommand(getDummyAirshipSettings()),
}, },
} }

View File

@ -17,6 +17,7 @@ package pull
import ( import (
"opendev.org/airship/airshipctl/pkg/document/repo" "opendev.org/airship/airshipctl/pkg/document/repo"
"opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/log"
) )
// Settings is a reference to environment.AirshipCTLSettings // Settings is a reference to environment.AirshipCTLSettings
@ -41,12 +42,13 @@ func (s *Settings) Pull() error {
func (s *Settings) cloneRepositories() error { func (s *Settings) cloneRepositories() error {
// Clone main repository // Clone main repository
currentManifest, err := s.Config.CurrentContextManifest() currentManifest, err := s.Config.CurrentContextManifest()
log.Debugf("Reading current context manifest information from %s", s.AirshipConfigPath)
if err != nil { if err != nil {
return err return err
} }
// Clone repositories // Clone repositories
for _, extraRepoConfig := range currentManifest.Repositories { for repoName, extraRepoConfig := range currentManifest.Repositories {
err := extraRepoConfig.Validate() err := extraRepoConfig.Validate()
if err != nil { if err != nil {
return err return err
@ -55,6 +57,8 @@ func (s *Settings) cloneRepositories() error {
if err != nil { if err != nil {
return err 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) err = repository.Download(extraRepoConfig.ToCheckoutOptions(true).Force)
if err != nil { if err != nil {
return err return err

View File

@ -22,6 +22,7 @@ import (
"github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5" "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/cache"
"github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/storage" "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 // Checkout git repository, ToCheckoutOptions method will be used go get CheckoutOptions
func (repo *Repository) Checkout(enforce bool) error { func (repo *Repository) Checkout(enforce bool) error {
log.Debugf("Attempting to checkout the repository %s", repo.Name)
if !repo.Driver.IsOpen() { if !repo.Driver.IsOpen() {
return ErrNoOpenRepo return ErrNoOpenRepo
} }
co := repo.ToCheckoutOptions(enforce) 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() tree, err := repo.Driver.Worktree()
if err != nil { if err != nil {
return fmt.Errorf("could not get worktree from the repo, %w", err) return fmt.Errorf("could not get worktree from the repo, %w", err)
@ -125,7 +132,7 @@ func (repo *Repository) Open() error {
// Clone given repository // Clone given repository
func (repo *Repository) Clone() error { 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() auth, err := repo.ToAuth()
if err != nil { if err != nil {
return fmt.Errorf("failed to build auth options for repository %v: %w", repo.Name, err) return fmt.Errorf("failed to build auth options for repository %v: %w", repo.Name, err)

View File

@ -69,7 +69,7 @@ func (a *AirshipCTLSettings) InitFlags(cmd *cobra.Command) {
a.Create = false a.Create = false
} }
// InitConfig - Initializes and loads Config it exists. // InitConfig - Initializes and loads Config if exists.
func (a *AirshipCTLSettings) InitConfig() { func (a *AirshipCTLSettings) InitConfig() {
a.Config = config.NewConfig() a.Config = config.NewConfig()