44 "fmt"
55 "github.com/OctopusDeploy/cli/pkg/apiclient"
66 "io"
7+ "strings"
78
89 "github.com/MakeNowJust/heredoc/v2"
910 "github.com/OctopusDeploy/cli/pkg/constants"
@@ -21,6 +22,13 @@ const (
2122 FlagWeb = "web"
2223)
2324
25+ // joinURL joins host and path, ensuring there's exactly one slash between them
26+ func joinURL (host , path string ) string {
27+ host = strings .TrimSuffix (host , "/" )
28+ path = strings .TrimPrefix (path , "/" )
29+ return host + "/" + path
30+ }
31+
2432type ViewFlags struct {
2533 Web * flag.Flag [bool ]
2634}
@@ -37,6 +45,7 @@ type ViewOptions struct {
3745 out io.Writer
3846 idOrName string
3947 flags * ViewFlags
48+ Command * cobra.Command
4049}
4150
4251func NewCmdView (f factory.Factory ) * cobra.Command {
@@ -63,6 +72,7 @@ func NewCmdView(f factory.Factory) *cobra.Command {
6372 cmd .OutOrStdout (),
6473 args [0 ],
6574 viewFlags ,
75+ cmd ,
6676 }
6777
6878 return viewRun (opts )
@@ -81,27 +91,88 @@ func viewRun(opts *ViewOptions) error {
8191 return err
8292 }
8393
84- fmt .Fprintf (opts .out , "%s %s\n " , output .Bold (project .Name ), output .Dimf ("(%s)" , project .Slug ))
94+ return output .PrintResource (project , opts .Command , output.Mappers [* projects.Project ]{
95+ Json : func (p * projects.Project ) any {
96+ cacBranch := "Not version controlled"
97+ if p .IsVersionControlled {
98+ cacBranch = p .PersistenceSettings .(projects.GitPersistenceSettings ).DefaultBranch ()
99+ }
100+
101+ return ProjectAsJson {
102+ Id : p .GetID (),
103+ Name : p .Name ,
104+ Slug : p .Slug ,
105+ Description : p .Description ,
106+ IsVersionControlled : p .IsVersionControlled ,
107+ VersionControlBranch : cacBranch ,
108+ WebUrl : joinURL (opts .Host , p .Links ["Web" ]),
109+ }
110+ },
111+ Table : output.TableDefinition [* projects.Project ]{
112+ Header : []string {"NAME" , "SLUG" , "DESCRIPTION" , "VERSION CONTROL" , "WEB URL" },
113+ Row : func (p * projects.Project ) []string {
114+ description := p .Description
115+ if description == "" {
116+ description = constants .NoDescription
117+ }
118+
119+ cacBranch := "Not version controlled"
120+ if p .IsVersionControlled {
121+ cacBranch = p .PersistenceSettings .(projects.GitPersistenceSettings ).DefaultBranch ()
122+ }
123+
124+ return []string {
125+ output .Bold (p .Name ),
126+ p .Slug ,
127+ description ,
128+ cacBranch ,
129+ output .Blue (joinURL (opts .Host , p .Links ["Web" ])),
130+ }
131+ },
132+ },
133+ Basic : func (p * projects.Project ) string {
134+ return formatProjectForBasic (opts , p )
135+ },
136+ })
137+ }
138+
139+ type ProjectAsJson struct {
140+ Id string `json:"Id"`
141+ Name string `json:"Name"`
142+ Slug string `json:"Slug"`
143+ Description string `json:"Description"`
144+ IsVersionControlled bool `json:"IsVersionControlled"`
145+ VersionControlBranch string `json:"VersionControlBranch"`
146+ WebUrl string `json:"WebUrl"`
147+ }
85148
149+ func formatProjectForBasic (opts * ViewOptions , project * projects.Project ) string {
150+ var result strings.Builder
151+
152+ // header
153+ result .WriteString (fmt .Sprintf ("%s %s\n " , output .Bold (project .Name ), output .Dimf ("(%s)" , project .Slug )))
154+
155+ // version control branch
86156 cacBranch := "Not version controlled"
87157 if project .IsVersionControlled {
88158 cacBranch = project .PersistenceSettings .(projects.GitPersistenceSettings ).DefaultBranch ()
89159 }
90- fmt .Fprintf (opts .out , "Version control branch: %s\n " , output .Cyan (cacBranch ))
160+ result .WriteString (fmt .Sprintf ("Version control branch: %s\n " , output .Cyan (cacBranch )))
161+
162+ // description
91163 if project .Description == "" {
92- fmt . Fprintln ( opts . out , output .Dim (constants .NoDescription ))
164+ result . WriteString ( fmt . Sprintln ( output .Dim (constants .NoDescription ) ))
93165 } else {
94- fmt . Fprintln ( opts . out , output .Dim (project .Description ))
166+ result . WriteString ( fmt . Sprintln ( output .Dim (project .Description ) ))
95167 }
96-
97- url := opts .Host + project .Links ["Web" ]
98-
99- // footer
100- fmt .Fprintf (opts .out , "View this project in Octopus Deploy: %s\n " , output .Blue (url ))
101-
168+
169+ // footer with web URL
170+ url := joinURL (opts .Host , project .Links ["Web" ])
171+ result .WriteString (fmt .Sprintf ("View this project in Octopus Deploy: %s\n " , output .Blue (url )))
172+
102173 if opts .flags .Web .Value {
103174 browser .OpenURL (url )
104175 }
105-
106- return nil
176+
177+ return result . String ()
107178}
0 commit comments