@@ -2,16 +2,18 @@ package list
22
33import (
44 "errors"
5- "github.com/OctopusDeploy/cli/pkg/apiclient"
65 "math"
76
7+ "github.com/OctopusDeploy/cli/pkg/apiclient"
8+
89 "github.com/MakeNowJust/heredoc/v2"
910 "github.com/OctopusDeploy/cli/pkg/constants"
1011 "github.com/OctopusDeploy/cli/pkg/factory"
1112 "github.com/OctopusDeploy/cli/pkg/output"
1213 "github.com/OctopusDeploy/cli/pkg/question/selectors"
1314 "github.com/OctopusDeploy/cli/pkg/util/flag"
1415 "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects"
16+ "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
1517 "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/runbooks"
1618 "github.com/spf13/cobra"
1719)
@@ -20,19 +22,22 @@ const (
2022 FlagProject = "project"
2123 FlagLimit = "limit"
2224 FlagFilter = "filter"
25+ FlagGitRef = "git-ref"
2326)
2427
2528type ListFlags struct {
2629 Project * flag.Flag [string ]
2730 Limit * flag.Flag [int32 ]
2831 Filter * flag.Flag [string ]
32+ GitRef * flag.Flag [string ]
2933}
3034
3135func NewListFlags () * ListFlags {
3236 return & ListFlags {
3337 Project : flag .New [string ](FlagProject , false ),
3438 Limit : flag .New [int32 ](FlagLimit , false ),
3539 Filter : flag .New [string ](FlagFilter , false ),
40+ GitRef : flag .New [string ](FlagGitRef , false ),
3641 }
3742}
3843
@@ -62,6 +67,7 @@ func NewCmdList(f factory.Factory) *cobra.Command {
6267 flags .StringVarP (& listFlags .Project .Value , listFlags .Project .Name , "p" , "" , "Name or ID of the project to list runbooks for" )
6368 flags .Int32Var (& listFlags .Limit .Value , listFlags .Limit .Name , 0 , "limit the maximum number of results that will be returned" )
6469 flags .StringVarP (& listFlags .Filter .Value , listFlags .Filter .Name , "q" , "" , "filter runbooks to match only ones with a name containing the given string" )
70+ flags .StringVarP (& listFlags .GitRef .Value , listFlags .GitRef .Name , "" , "" , "Git reference to list runbooks for e.g. refs/heads/main. Only relevant for config-as-code projects where runbooks are stored in Git." )
6571 return cmd
6672}
6773
@@ -80,6 +86,7 @@ func listRun(cmd *cobra.Command, f factory.Factory, flags *ListFlags) error {
8086 limit := flags .Limit .Value
8187 filter := flags .Filter .Value
8288 projectNameOrID := flags .Project .Value
89+ gitReference := flags .GitRef .Value
8390
8491 octopus , err := f .GetSpacedClient (apiclient .NewRequester (cmd ))
8592 if err != nil {
@@ -112,10 +119,48 @@ func listRun(cmd *cobra.Command, f factory.Factory, flags *ListFlags) error {
112119 }
113120 }
114121
122+ var foundRunbooks * resources.Resources [* runbooks.Runbook ]
123+ runbooksAreInGit := false
124+
125+ if selectedProject .PersistenceSettings .Type () == projects .PersistenceSettingsTypeVersionControlled {
126+ runbooksAreInGit = selectedProject .PersistenceSettings .(projects.GitPersistenceSettings ).RunbooksAreInGit ()
127+ }
128+
115129 if limit <= 0 {
116130 limit = math .MaxInt32
117131 }
118- foundRunbooks , err := runbooks .List (octopus , f .GetCurrentSpace ().ID , selectedProject .ID , filter , int (limit ))
132+
133+ if runbooksAreInGit {
134+ if f .IsPromptEnabled () {
135+ if gitReference == "" { // we need a git ref; ask for one
136+ gitRef , err := selectors .GitReference ("Select the Git reference to list runbooks for" , octopus , f .Ask , selectedProject )
137+ if err != nil {
138+ return err
139+ }
140+ gitReference = gitRef .CanonicalName // e.g /refs/heads/main
141+ } else {
142+ if ! constants .IsProgrammaticOutputFormat (outputFormat ) {
143+ cmd .Printf ("Git reference %s\n " , output .Cyan (gitReference ))
144+ }
145+ }
146+ } else {
147+ if gitReference == "" {
148+ return errors .New ("git reference must be specified" )
149+ }
150+ }
151+
152+ foundRunbooks , err = runbooks .ListGitRunbooks (octopus , f .GetCurrentSpace ().ID , selectedProject .ID , gitReference , filter , int (limit ))
153+
154+ if err != nil {
155+ return err
156+ }
157+ } else {
158+ foundRunbooks , err = runbooks .List (octopus , f .GetCurrentSpace ().ID , selectedProject .ID , filter , int (limit ))
159+
160+ if err != nil {
161+ return err
162+ }
163+ }
119164
120165 return output .PrintArray (foundRunbooks .Items , cmd , output.Mappers [* runbooks.Runbook ]{
121166 Json : func (item * runbooks.Runbook ) any {
0 commit comments