44 "fmt"
55 "os"
66 "path/filepath"
7+ "regexp"
8+ "strings"
79
810 "github.com/charmbracelet/huh"
911 "github.com/major-technology/cli/clients/api"
@@ -57,23 +59,32 @@ func runPull(cmd *cobra.Command) error {
5759
5860 cmd .Printf ("Selected application: %s\n " , selectedApp .Name )
5961
60- // Determine the target directory (use the repository name)
61- targetDir := filepath . Join ( "." , selectedApp .GithubRepositoryName )
62+ // Determine the desired directory name (based on app name)
63+ desiredDir := sanitizeDirName ( selectedApp .Name )
6264
63- // Check if the directory already exists
64- var gitErr error
65- if _ , err := os .Stat (targetDir ); err == nil {
66- // Directory exists, just pull
67- cmd .Printf ("Directory '%s' already exists. Pulling latest changes...\n " , targetDir )
68-
69- gitErr = git .Pull (targetDir )
70- } else if os .IsNotExist (err ) {
71- // Directory doesn't exist, clone it
72- cmd .Printf ("Directory '%s' does not exist. Cloning repository...\n " , targetDir )
65+ // Determine the repository directory (use the repository name for git operations)
66+ repoDir := filepath .Join ("." , selectedApp .GithubRepositoryName )
7367
74- _ , gitErr = cloneRepository (selectedApp .CloneURLSSH , selectedApp .CloneURLHTTPS , targetDir )
68+ // Check if either directory exists
69+ var gitErr error
70+ var workingDir string
71+
72+ // Check if desired directory exists
73+ if _ , err := os .Stat (desiredDir ); err == nil {
74+ // Desired directory exists, use it for pulling
75+ workingDir = desiredDir
76+ cmd .Printf ("Directory '%s' already exists. Pulling latest changes...\n " , workingDir )
77+ gitErr = git .Pull (workingDir )
78+ } else if _ , err := os .Stat (repoDir ); err == nil {
79+ // Repository directory exists (old naming), use it for pulling then rename
80+ workingDir = repoDir
81+ cmd .Printf ("Directory '%s' already exists. Pulling latest changes...\n " , workingDir )
82+ gitErr = git .Pull (workingDir )
7583 } else {
76- return fmt .Errorf ("failed to check directory: %w" , err )
84+ // Neither directory exists, clone directly to desired directory
85+ workingDir = desiredDir
86+ cmd .Printf ("Directory '%s' does not exist. Cloning repository...\n " , workingDir )
87+ _ , gitErr = cloneRepository (selectedApp .CloneURLSSH , selectedApp .CloneURLHTTPS , workingDir )
7788 }
7889
7990 // Handle git authentication errors
@@ -85,14 +96,14 @@ func runPull(cmd *cobra.Command) error {
8596 }
8697
8798 // Retry the git operation
88- if _ , err := os .Stat (targetDir ); err == nil {
99+ if _ , err := os .Stat (workingDir ); err == nil {
89100 // Directory exists, pull
90101 cmd .Printf ("Pulling latest changes...\n " )
91- gitErr = git .Pull (targetDir )
92- } else if os . IsNotExist ( err ) {
102+ gitErr = git .Pull (workingDir )
103+ } else {
93104 // Directory doesn't exist, clone
94105 cmd .Printf ("Cloning repository...\n " )
95- _ , gitErr = cloneRepository (selectedApp .CloneURLSSH , selectedApp .CloneURLHTTPS , targetDir )
106+ _ , gitErr = cloneRepository (selectedApp .CloneURLSSH , selectedApp .CloneURLHTTPS , workingDir )
96107 }
97108
98109 // Check if retry succeeded
@@ -104,17 +115,40 @@ func runPull(cmd *cobra.Command) error {
104115 }
105116 }
106117
118+ // Rename directory if needed
119+ finalDir := workingDir
120+ if workingDir != desiredDir {
121+ // Check if desired directory name is available
122+ if _ , err := os .Stat (desiredDir ); err == nil {
123+ // Desired directory already exists, keep using working directory
124+ cmd .Printf ("\n Note: Directory '%s' already exists, keeping repository in '%s'\n " , desiredDir , workingDir )
125+ } else if os .IsNotExist (err ) {
126+ // Rename to desired directory
127+ if err := os .Rename (workingDir , desiredDir ); err != nil {
128+ cmd .Printf ("\n Warning: Failed to rename directory to '%s': %v\n " , desiredDir , err )
129+ cmd .Printf ("Continuing with directory '%s'\n " , workingDir )
130+ } else {
131+ cmd .Printf ("\n Renamed directory from '%s' to '%s'\n " , workingDir , desiredDir )
132+ finalDir = desiredDir
133+ }
134+ } else {
135+ // Some other error checking the desired directory
136+ cmd .Printf ("\n Warning: Could not check directory '%s': %v\n " , desiredDir , err )
137+ cmd .Printf ("Continuing with directory '%s'\n " , workingDir )
138+ }
139+ }
140+
107141 // Generate env file
108142 cmd .Println ("\n Generating .env file..." )
109- envFilePath , _ , err := generateEnvFile (targetDir )
143+ envFilePath , _ , err := generateEnvFile (finalDir )
110144 if err != nil {
111145 return fmt .Errorf ("failed to generate .env file: %w" , err )
112146 }
113147 cmd .Printf ("Successfully generated .env file at: %s\n " , envFilePath )
114148
115149 // Generate resources file
116150 cmd .Println ("\n Generating RESOURCES.md file..." )
117- resourcesFilePath , _ , err := generateResourcesFile (targetDir )
151+ resourcesFilePath , _ , err := generateResourcesFile (finalDir )
118152 if err != nil {
119153 return fmt .Errorf ("failed to generate RESOURCES.md file: %w" , err )
120154 }
@@ -126,6 +160,26 @@ func runPull(cmd *cobra.Command) error {
126160 return nil
127161}
128162
163+ // sanitizeDirName converts an application name to a valid directory name
164+ func sanitizeDirName (name string ) string {
165+ // Convert to lowercase
166+ dirName := strings .ToLower (name )
167+
168+ // Replace spaces and special characters with hyphens
169+ reg := regexp .MustCompile (`[^a-z0-9]+` )
170+ dirName = reg .ReplaceAllString (dirName , "-" )
171+
172+ // Remove leading/trailing hyphens
173+ dirName = strings .Trim (dirName , "-" )
174+
175+ // If the result is empty, use a default
176+ if dirName == "" {
177+ dirName = "app"
178+ }
179+
180+ return dirName
181+ }
182+
129183// selectApplication prompts the user to select an application from the list
130184func selectApplication (cmd * cobra.Command , apps []api.ApplicationItem ) (* api.ApplicationItem , error ) {
131185 if len (apps ) == 0 {
0 commit comments