22package initcmd
33
44import (
5+ "encoding/json"
56 "fmt"
67 "os"
78
@@ -10,6 +11,7 @@ import (
1011
1112 "github.com/NodeOps-app/createos-cli/internal/api"
1213 "github.com/NodeOps-app/createos-cli/internal/config"
14+ "github.com/NodeOps-app/createos-cli/internal/git"
1315 "github.com/NodeOps-app/createos-cli/internal/terminal"
1416)
1517
@@ -75,29 +77,72 @@ func NewInitCommand() *cli.Command {
7577 return nil
7678 }
7779
78- options := make ([] string , len ( projects ))
79- for i , p := range projects {
80- desc := ""
81- if p .Description != nil && * p . Description ! = "" {
82- desc = " — " + * p . Description
80+ // Filter out non-active projects
81+ activeProjects := make ([]api. Project , 0 , len ( projects ))
82+ for _ , p := range projects {
83+ if p .Status = = "active " {
84+ activeProjects = append ( activeProjects , p )
8385 }
84- options [i ] = fmt .Sprintf ("%s (%s)%s" , p .DisplayName , p .ID , desc )
8586 }
87+ projects = activeProjects
8688
87- selected , err := pterm .DefaultInteractiveSelect .
88- WithDefaultText ("Select a project to link" ).
89- WithOptions (options ).
90- Show ()
91- if err != nil {
92- return fmt .Errorf ("selection cancelled" )
89+ if len (projects ) == 0 {
90+ fmt .Println ("You don't have any active projects yet." )
91+ return nil
9392 }
9493
95- // Find the selected project
96- for i , opt := range options {
97- if opt == selected {
98- projectID = projects [i ].ID
99- projectName = projects [i ].DisplayName
100- break
94+ // Try to auto-detect the matching VCS project from git remote
95+ repoFullName := git .GetRemoteFullName (dir )
96+ if repoFullName != "" {
97+ for _ , p := range projects {
98+ if p .Type != "vcs" && p .Type != "githubImport" {
99+ continue
100+ }
101+ var src api.VCSSource
102+ if err := json .Unmarshal (p .Source , & src ); err != nil {
103+ continue
104+ }
105+ if src .VCSFullName == repoFullName {
106+ pterm .Info .Printf ("Detected project %s from git remote (%s)\n " , p .DisplayName , repoFullName )
107+ useDetected , _ := pterm .DefaultInteractiveConfirm .
108+ WithDefaultText (fmt .Sprintf ("Link to %s?" , p .DisplayName )).
109+ WithDefaultValue (true ).
110+ Show ()
111+ if useDetected {
112+ projectID = p .ID
113+ projectName = p .DisplayName
114+ }
115+ break
116+ }
117+ }
118+ }
119+
120+ // Fall back to interactive selection if no match found
121+ if projectID == "" {
122+ options := make ([]string , len (projects ))
123+ for i , p := range projects {
124+ desc := ""
125+ if p .Description != nil && * p .Description != "" {
126+ desc = " — " + * p .Description
127+ }
128+ options [i ] = fmt .Sprintf ("%s (%s)%s" , p .DisplayName , p .ID , desc )
129+ }
130+
131+ selected , err := pterm .DefaultInteractiveSelect .
132+ WithDefaultText ("Select a project to link" ).
133+ WithOptions (options ).
134+ Show ()
135+ if err != nil {
136+ return fmt .Errorf ("selection cancelled" )
137+ }
138+
139+ // Find the selected project
140+ for i , opt := range options {
141+ if opt == selected {
142+ projectID = projects [i ].ID
143+ projectName = projects [i ].DisplayName
144+ break
145+ }
101146 }
102147 }
103148 }
0 commit comments