@@ -17,12 +17,14 @@ package commands
1717import (
1818 "context"
1919 "fmt"
20+ "os"
2021 "time"
2122
2223 "github.com/codefresh-io/cli-v2/pkg/log"
2324 "github.com/codefresh-io/cli-v2/pkg/runtime"
2425 "github.com/codefresh-io/cli-v2/pkg/store"
2526 "github.com/codefresh-io/cli-v2/pkg/util"
27+ "github.com/juju/ansiterm"
2628
2729 apcmd "github.com/argoproj-labs/argocd-autopilot/cmd/commands"
2830 "github.com/argoproj-labs/argocd-autopilot/pkg/application"
@@ -66,6 +68,7 @@ func NewGitSourceCommand() *cobra.Command {
6668 }
6769
6870 cmd .AddCommand (NewGitSourceCreateCommand ())
71+ cmd .AddCommand (NewGitSourceListCommand ())
6972 cmd .AddCommand (NewGitSourceDeleteCommand ())
7073
7174 return cmd
@@ -132,6 +135,58 @@ func NewGitSourceCreateCommand() *cobra.Command {
132135 return cmd
133136}
134137
138+ func NewGitSourceListCommand () * cobra.Command {
139+ cmd := & cobra.Command {
140+ Use : "list runtime_name" ,
141+ Short : "List all Codefresh git-sources of a given runtime" ,
142+ Example : util .Doc (`<BIN> git-source list my-runtime` ),
143+ RunE : func (_ * cobra.Command , args []string ) error {
144+ return RunGitSourceList (args [0 ])
145+ },
146+ }
147+ return cmd
148+ }
149+
150+ func RunGitSourceList (runtimeName string ) error {
151+ gitSources , err := cfConfig .NewClient ().GitSource ().List (runtimeName )
152+
153+ if err != nil {
154+ return fmt .Errorf ("failed to get git-sources list. Err: %w" , err )
155+ }
156+
157+ tb := ansiterm .NewTabWriter (os .Stdout , 0 , 0 , 4 , ' ' , 0 )
158+ _ , err = fmt .Fprintln (tb , "NAME\t REPOURL\t PATH\t HEALTH-STATUS\t SYNC-STATUS" )
159+ if err != nil {
160+ return fmt .Errorf ("failed to print git-source list table headers. Err: %w" , err )
161+ }
162+
163+ for _ , gs := range gitSources {
164+ name := gs .Metadata .Name
165+ repoURL := gs .Self .RepoURL
166+ path := gs .Self .Path
167+ healthStatus := "N/A"
168+ syncStatus := gs .Self .Status .SyncStatus .String ()
169+
170+ if gs .Self .Status .HealthStatus != nil {
171+ healthStatus = gs .Self .Status .HealthStatus .String ()
172+ }
173+
174+ _ , err = fmt .Fprintf (tb , "%s\t %s\t %s\t %s\t %s\n " ,
175+ name ,
176+ repoURL ,
177+ path ,
178+ healthStatus ,
179+ syncStatus ,
180+ )
181+
182+ if err != nil {
183+ return err
184+ }
185+ }
186+
187+ return tb .Flush ()
188+ }
189+
135190func NewGitSourceDeleteCommand () * cobra.Command {
136191 var (
137192 cloneOpts * git.CloneOptions
@@ -182,7 +237,6 @@ func RunCreateGitSource(ctx context.Context, opts *GitSourceCreateOptions) error
182237 }
183238
184239 fi , err := gsFs .ReadDir ("." )
185-
186240 if err != nil {
187241 return fmt .Errorf ("failed to read files in git-source repo. Err: %w" , err )
188242 }
0 commit comments