diff --git a/forjfile/deployment.go b/forjfile/deployment.go index 815d536c..730ac2ee 100644 --- a/forjfile/deployment.go +++ b/forjfile/deployment.go @@ -6,7 +6,6 @@ type DeploymentStruct struct { Details *DeployForgeYaml `yaml:"define,omitempty"` } - // MarshalYAML provides the encoding part for DeploymentStruct // // In short we do not want to encode forjj deployment details) info except the core. @@ -15,11 +14,11 @@ func (d DeploymentStruct) MarshalYAML() (interface{}, error) { } // UpdateDeploymentCoreData set all DeploymentCore data -func (d *DeploymentStruct)UpdateDeploymentCoreData(data DeploymentCoreStruct) { +func (d *DeploymentStruct) UpdateDeploymentCoreData(data DeploymentCoreStruct) { d.DeploymentCoreStruct = data } // RunInContext run GIT commands in the GIT repo context. func (d *DeploymentStruct) RunInContext(doRun func() error) (err error) { - return d.DeploymentCoreStruct.runInContext(doRun) -} \ No newline at end of file + return d.DeploymentCoreStruct.RunInContext(doRun) +} diff --git a/forjfile/deployment_core-git.go b/forjfile/deployment_core-git.go index ac3b8feb..f8ea33c1 100644 --- a/forjfile/deployment_core-git.go +++ b/forjfile/deployment_core-git.go @@ -34,7 +34,7 @@ func (d *DeploymentCoreStruct) GitSetRepo(aPath, origin string) (err error) { // GitDefineRemote helps to configure a deployment repository with a remote func (d *DeploymentCoreStruct) GitDefineRemote(name, uri string) (err error) { - return d.runInContext(func() (err error) { + return d.RunInContext(func() (err error) { if err = git.EnsureRemoteIs(name, uri); err != nil { return } @@ -44,7 +44,7 @@ func (d *DeploymentCoreStruct) GitDefineRemote(name, uri string) (err error) { // GitSyncFrom refresh the remote, and synchronize. func (d *DeploymentCoreStruct) GitSyncFrom(remote, branch string) error { - return d.runInContext(func() (_ error) { + return d.RunInContext(func() (_ error) { if !git.RemoteExist(remote) { return } @@ -57,7 +57,7 @@ func (d *DeploymentCoreStruct) GitSyncFrom(remote, branch string) error { // GitSyncUp set and report sync status func (d *DeploymentCoreStruct) GitSyncUp() error { - return d.runInContext(func() (_ error) { + return d.RunInContext(func() (_ error) { if d.syncStatus == 0 { return fmt.Errorf("Internal error! Unable to sync up. The synchronization was not initiliazed. You must call GitSyncFrom, Once") } @@ -81,7 +81,7 @@ func (d *DeploymentCoreStruct) GitSyncUp() error { // !!! Conflict can happen !!! // func (d *DeploymentCoreStruct) SwitchTo(branch string) error { - return d.runInContext(func() (err error) { + return d.RunInContext(func() (err error) { if git.GetCurrentBranch() != branch { trackedFiles := git.GetStatus().CountTracked() @@ -106,7 +106,7 @@ func (d *DeploymentCoreStruct) SwitchTo(branch string) error { // GitCommit do the commit in the Deployment repository. func (d *DeploymentCoreStruct) GitCommit(message string) (_ error) { - return d.runInContext(func() (err error) { + return d.RunInContext(func() (err error) { status := git.GetStatus() if status.Ready.CountFiles() > 0 { git.Commit(message, true) @@ -118,7 +118,7 @@ func (d *DeploymentCoreStruct) GitCommit(message string) (_ error) { // GitPush do a git push // depending on the previous Git SyncFrom, a push can take place func (d *DeploymentCoreStruct) GitPush(force bool) (_ error) { - return d.runInContext(func() (err error) { + return d.RunInContext(func() (err error) { if d.syncStatus == -2 { return fmt.Errorf("Unable to push to an inexistent remote") } @@ -146,7 +146,7 @@ func (d *DeploymentCoreStruct) GitPush(force bool) (_ error) { // GitResetBranchFromRemote clean current branch, check out to the requested branch and reset against remote branch. // The reset is not made if the fetch return an error. func (d *DeploymentCoreStruct) GitResetBranchFromRemote(branch, remote string) { - d.runInContext(func() (_ error) { + d.RunInContext(func() (_ error) { git.Do("reset", "--hard", "HEAD") git.Do("checkout", branch) if git.Do("fetch", remote) == 0 { @@ -159,7 +159,7 @@ func (d *DeploymentCoreStruct) GitResetBranchFromRemote(branch, remote string) { // ------------------ Internal functions // runInContext ensure GIT commands are executed in the right GIT repo context. -func (d *DeploymentCoreStruct) runInContext(doRun func() error) (err error) { +func (d *DeploymentCoreStruct) RunInContext(doRun func() error) (err error) { if d.savedPath != "" { return doRun() } diff --git a/maintain.go b/maintain.go index 38153d46..be4e2ee5 100644 --- a/maintain.go +++ b/maintain.go @@ -4,6 +4,7 @@ import ( "fmt" "forjj/creds" "forjj/git" + "os" "github.com/forj-oss/forjj-modules/trace" ) @@ -130,17 +131,48 @@ func (a *Forj) doInstanceMaintain(instance string) error { // after the first upstream maintain call remote repo should exist // So, we can sync it up if the sync was not done successfully before. - if !a.d.InSync() { - if err := a.d.GitSyncUp(); err != nil { - return err + + if err := a.d.RunInContext(func() error { + if !a.d.InSync() { + if err := a.d.GitSyncUp(); err != nil { + return err + } + + // Create basic README.md file on deploy repos if no commit found + if _, err := git.Get("log", "-1", "--pretty=%H"); err != nil { + a.createInitialCommit() + git.Add([]string{"README.md"}) + if err := git.Commit("Initial Source Deploy commit.", true); err != nil { + return err + } + } + + return a.d.GitPush(false) } - return a.d.GitPush(false) + return nil + }); err != nil { + return err } } return nil } +// Must be in the current repo dir +func (a *Forj) createInitialCommit() error { + fd, err := os.Create("README.md") + if err != nil { + return err + } + defer fd.Close() + + _, err = fd.WriteString("# Information\n\nThis repository (source Deployment repository) has been created by forjj. \n\nUse forjj update to update it from your infra source repository.\n\nForj team.\n") + if err != nil { + return err + } + return nil +} + // get_infra_repo detect in the path given contains the infra repository. func (a *Forj) get_infra_repo() error { return a.i.Use(a.f.InfraPath())