@@ -3,6 +3,7 @@ package cmd
33import (
44 "errors"
55 "fmt"
6+ "log"
67 "os"
78 "path/filepath"
89 "time"
@@ -21,6 +22,13 @@ const (
2122 FlagGitConfig = "git-config"
2223)
2324
25+ type LogWriter struct {}
26+
27+ func (w LogWriter ) Write (p []byte ) (n int , err error ) {
28+ log .Print (string (p ))
29+ return len (p ), nil
30+ }
31+
2432// manageDeploymentRepoCmd represents the manageDeploymentRepo command
2533var manageDeploymentRepoCmd = & cobra.Command {
2634 Use : "manageDeploymentRepo" ,
@@ -31,16 +39,20 @@ and usage of using your command. For example:
3139Cobra is a CLI library for Go that empowers applications.
3240This application is a tool to generate the needed files
3341to quickly create a Cobra application.` ,
34- Args : cobra .ExactArgs (3 ),
42+ Args : cobra .ExactArgs (4 ),
3543 ArgAliases : []string {
44+ "componentLocation" ,
45+ "templateDirectory" ,
3646 "deploymentRepository" ,
3747 "deploymentRepositoryBranch" ,
38- "templateDirectory" ,
3948 },
4049 RunE : func (cmd * cobra.Command , args []string ) error {
41- deploymentRepository := args [0 ]
42- deploymentRepositoryBranch := args [1 ]
43- templateDirectory := args [2 ]
50+ //componentLocation := args[0]
51+ templateDirectory := args [1 ]
52+ deploymentRepository := args [2 ]
53+ deploymentRepositoryBranch := args [3 ]
54+
55+ log .Println ("parsing git config file from flag:" , cmd .Flag (FlagGitConfig ).Value .String ())
4456
4557 gitConfig , err := gitconfig .ParseConfig (cmd .Flag (FlagGitConfig ).Value .String ())
4658 if err != nil {
@@ -51,37 +63,49 @@ to quickly create a Cobra application.`,
5163 }
5264
5365 cloneOptions := & git.CloneOptions {
54- URL : deploymentRepository ,
66+ URL : deploymentRepository ,
67+ Progress : LogWriter {},
5568 }
5669 if err := gitConfig .ConfigureCloneOptions (cloneOptions ); err != nil {
5770 return err
5871 }
5972
60- repo , tmpDir , err := CloneRepo (deploymentRepository , gitConfig )
61- fmt .Printf ("Cloned repository to temporary directory: %s\n " , tmpDir )
73+ tmpDir , err := os .MkdirTemp ("" , "deployment-repo-" )
74+ if err != nil {
75+ return fmt .Errorf ("failed to create temp dir: %w" , err )
76+ }
77+
78+ repo , err := CloneRepo (deploymentRepository , tmpDir , gitConfig )
79+ log .Println ("Cloned repository to temporary directory: " , tmpDir )
6280 if err != nil {
6381 return fmt .Errorf ("failed to clone repository: %w" , err )
6482 }
6583 defer func (path string ) {
6684 err := os .RemoveAll (path )
6785 if err != nil {
68- fmt .Fprintf (os .Stderr , "failed to remove temporary directory %s: %v\n " , path , err )
86+ _ , _ = fmt .Fprintf (os .Stderr , "failed to remove temporary directory %s: %v\n " , path , err )
6987 }
7088 }(tmpDir )
7189
72- // Check if branch exists
90+ // Check if branch exists (local or remote)
7391 branchExists := false
74- branches , err := repo .Branches ()
92+ references , err := repo .References ()
7593 if err != nil {
76- return fmt .Errorf ("failed to list branches : %w" , err )
94+ return fmt .Errorf ("failed to list references : %w" , err )
7795 }
78- branchRefName := plumbing .NewBranchReferenceName (deploymentRepositoryBranch )
79- branches .ForEach (func (ref * plumbing.Reference ) error {
80- if ref .Name () == branchRefName {
96+ localRef := plumbing .NewBranchReferenceName (deploymentRepositoryBranch )
97+ remoteRef := plumbing .NewRemoteReferenceName ("origin" , deploymentRepositoryBranch )
98+ err = references .ForEach (func (ref * plumbing.Reference ) error {
99+ log .Printf ("Found reference: %s" , ref .Name ())
100+ if ref .Name () == localRef || ref .Name () == remoteRef {
81101 branchExists = true
102+ log .Printf ("Branch %s exists as %s" , deploymentRepositoryBranch , ref .Name ())
82103 }
83104 return nil
84105 })
106+ if err != nil {
107+ return err
108+ }
85109
86110 workTree , err := repo .Worktree ()
87111 if err != nil {
@@ -90,9 +114,9 @@ to quickly create a Cobra application.`,
90114
91115 if ! branchExists {
92116 // Create and checkout new branch
93- fmt .Printf ("Branch %s does not exist. Creating...\n " , deploymentRepositoryBranch )
117+ log .Printf ("Branch %s does not exist. Creating...\n " , deploymentRepositoryBranch )
94118 err = workTree .Checkout (& git.CheckoutOptions {
95- Branch : branchRefName ,
119+ Branch : localRef ,
96120 Create : true ,
97121 })
98122 if err != nil {
@@ -101,8 +125,9 @@ to quickly create a Cobra application.`,
101125
102126 pushOptions := & git.PushOptions {
103127 RefSpecs : []config.RefSpec {
104- config .RefSpec (branchRefName + ":" + branchRefName ),
128+ config .RefSpec (localRef + ":" + localRef ),
105129 },
130+ Progress : LogWriter {},
106131 }
107132
108133 if err := gitConfig .ConfigurePushOptions (pushOptions ); err != nil {
@@ -117,7 +142,7 @@ to quickly create a Cobra application.`,
117142 } else {
118143 // Checkout existing branch
119144 err = workTree .Checkout (& git.CheckoutOptions {
120- Branch : branchRefName ,
145+ Branch : remoteRef ,
121146 })
122147 if err != nil {
123148 return fmt .Errorf ("failed to checkout branch: %w" , err )
@@ -145,7 +170,7 @@ to quickly create a Cobra application.`,
145170 }
146171 if ! d .IsDir () {
147172 // Process the file (path)
148- fmt .Printf ("Found file: %s\n " , path )
173+ log .Printf ("Found file: %s\n " , path )
149174 template , err := os .ReadFile (path )
150175 if err != nil {
151176 return fmt .Errorf ("failed to read template file %s: %w" , path , err )
@@ -197,6 +222,7 @@ to quickly create a Cobra application.`,
197222 if err := gitConfig .ConfigurePushOptions (pushOptions ); err != nil {
198223 return fmt .Errorf ("failed to configure push options: %w" , err )
199224 }
225+ pushOptions .Progress = LogWriter {}
200226 err = repo .Push (pushOptions )
201227 if err != nil {
202228 if ! errors .Is (err , git .NoErrAlreadyUpToDate ) {
@@ -211,31 +237,32 @@ to quickly create a Cobra application.`,
211237func init () {
212238 RootCmd .AddCommand (manageDeploymentRepoCmd )
213239
214- manageDeploymentRepoCmd .PersistentFlags ().StringP ("ocm-config" , "" , "" , "ocm configuration file" )
215- manageDeploymentRepoCmd .Flags ().StringP (FlagGitConfig , "" , "" , "Git configuration file" )
240+ manageDeploymentRepoCmd .Flags ().StringArray ("cluster-providers" , nil , "List of cluster providers to manage" )
241+ manageDeploymentRepoCmd .Flags ().StringArray ("serviceProviders" , nil , "List of service providers to manage" )
242+ manageDeploymentRepoCmd .Flags ().StringArray ("platformServices" , nil , "List of platform services to manage" )
243+
244+ manageDeploymentRepoCmd .Flags ().String ("ocm-config" , "" , "ocm configuration file" )
245+ manageDeploymentRepoCmd .Flags ().String (FlagGitConfig , "" , "Git configuration file" )
246+
216247 if err := manageDeploymentRepoCmd .MarkFlagRequired (FlagGitConfig ); err != nil {
217248 panic (err )
218249 }
219250}
220251
221- func CloneRepo (repoURL string , config * gitconfig.Config ) (* git.Repository , string , error ) {
252+ func CloneRepo (repoURL , path string , config * gitconfig.Config ) (* git.Repository , error ) {
222253 cloneOptions := & git.CloneOptions {
223- URL : repoURL ,
254+ URL : repoURL ,
255+ SingleBranch : false ,
224256 }
225257
226258 if err := config .ConfigureCloneOptions (cloneOptions ); err != nil {
227- return nil , "" , err
228- }
229-
230- tmpDir , err := os .MkdirTemp ("" , "deployment-repo-" )
231- if err != nil {
232- return nil , "" , fmt .Errorf ("failed to create temp dir: %w" , err )
259+ return nil , err
233260 }
234261
235- repo , err := git .PlainClone (tmpDir , false , cloneOptions )
262+ repo , err := git .PlainClone (path , false , cloneOptions )
236263 if err != nil {
237- return nil , tmpDir , fmt .Errorf ("failed to clone repository: %w" , err )
264+ return nil , fmt .Errorf ("failed to clone repository: %w" , err )
238265 }
239266
240- return repo , tmpDir , nil
267+ return repo , nil
241268}
0 commit comments