@@ -822,71 +822,41 @@ func (pm *PipelineManager) savePipelineProviderToEnv(
822
822
return nil
823
823
}
824
824
825
+ // checkAndPromptForProviderFiles checks if the provider files are present and prompts the user to create them if not.
825
826
func (pm * PipelineManager ) checkAndPromptForProviderFiles (
826
827
ctx context.Context , props projectProperties ) error {
827
828
log .Printf ("Checking for provider files for: %s" , props .CiProvider )
828
829
829
- providerFileChecks := map [ciProviderType ]struct {
830
- ymlPath string
831
- dirPath string
832
- dirDisplayName string
833
- providerDisplayName string
834
- }{
835
- ciProviderGitHubActions : {
836
- ymlPath : filepath .Join (props .RepoRoot , gitHubYml ),
837
- dirPath : filepath .Join (props .RepoRoot , gitHubWorkflowsDirectory ),
838
- dirDisplayName : gitHubWorkflowsDirectory ,
839
- providerDisplayName : gitHubDisplayName ,
840
- },
841
- ciProviderAzureDevOps : {
842
- ymlPath : filepath .Join (props .RepoRoot , azdoYml ),
843
- dirPath : filepath .Join (props .RepoRoot , azdoPipelinesDirectory ),
844
- dirDisplayName : azdoPipelinesDirectory ,
845
- providerDisplayName : azdoDisplayName ,
846
- },
847
- }
848
-
849
- providerCheck , exists := providerFileChecks [props .CiProvider ]
850
- if ! exists {
851
- errMsg := fmt .Sprintf ("%s is not a known pipeline provider" , props .CiProvider )
852
- log .Println ("Error:" , errMsg )
853
- return fmt .Errorf (errMsg )
854
- }
855
-
856
- log .Printf ("YAML path: %s" , providerCheck .ymlPath )
857
- log .Printf ("Directory path: %s" , providerCheck .dirPath )
858
-
859
- if ! osutil .FileExists (providerCheck .ymlPath ) {
860
- log .Printf ("%s YAML not found, prompting for creation" , providerCheck .providerDisplayName )
830
+ if ! hasPipelineFile (props .CiProvider , props .RepoRoot ) {
831
+ log .Printf ("%s YAML not found, prompting for creation" , props .CiProvider )
861
832
if err := pm .promptForCiFiles (ctx , props ); err != nil {
862
833
log .Println ("Error prompting for CI files:" , err )
863
834
return err
864
835
}
865
836
log .Println ("Prompt for CI files completed successfully." )
866
837
}
867
838
868
- log .Printf ("Checking if directory %s is empty" , providerCheck .dirPath )
869
- isEmpty , err := osutil .IsDirEmpty (providerCheck .dirPath , true )
839
+ dirPath := pipelineProviderFiles [props .CiProvider ].PipelineDirectory
840
+ log .Printf ("Checking if directory %s is empty" , dirPath )
841
+ isEmpty , err := osutil .IsDirEmpty (filepath .Join (props .RepoRoot , dirPath ), true )
870
842
if err != nil {
871
843
log .Println ("Error checking if directory is empty:" , err )
872
844
return fmt .Errorf ("error checking if directory is empty: %w" , err )
873
845
}
874
846
875
847
if isEmpty {
848
+ message := fmt .Sprintf (
849
+ "%s provider selected, but %s is empty. Please add pipeline files." ,
850
+ pipelineProviderFiles [props .CiProvider ].DisplayName , dirPath )
876
851
if props .CiProvider == ciProviderAzureDevOps {
877
- message : = fmt .Sprintf (
852
+ message = fmt .Sprintf (
878
853
"%s provider selected, but %s is empty. Please add pipeline files and try again." ,
879
- providerCheck . providerDisplayName , providerCheck . dirDisplayName )
854
+ pipelineProviderFiles [ props . CiProvider ]. DisplayName , dirPath )
880
855
log .Println ("Error:" , message )
881
856
return fmt .Errorf (message )
882
857
}
883
- if props .CiProvider == ciProviderGitHubActions {
884
- message := fmt .Sprintf (
885
- "%s provider selected, but %s is empty. Please add pipeline files." ,
886
- providerCheck .providerDisplayName , providerCheck .dirDisplayName )
887
- log .Println ("Info:" , message )
888
- pm .console .Message (ctx , message )
889
- }
858
+ log .Println ("Info:" , message )
859
+ pm .console .Message (ctx , message )
890
860
pm .console .Message (ctx , "" )
891
861
}
892
862
@@ -896,29 +866,13 @@ func (pm *PipelineManager) checkAndPromptForProviderFiles(
896
866
897
867
// promptForCiFiles creates CI/CD files for the specified provider, confirming with the user before creation.
898
868
func (pm * PipelineManager ) promptForCiFiles (ctx context.Context , props projectProperties ) error {
899
- paths := map [ciProviderType ]struct {
900
- directory string
901
- yml string
902
- }{
903
- ciProviderGitHubActions : {
904
- filepath .Join (props .RepoRoot , gitHubWorkflowsDirectory ), filepath .Join (props .RepoRoot , gitHubYml ),
905
- },
906
- ciProviderAzureDevOps : {
907
- filepath .Join (props .RepoRoot , azdoPipelinesDirectory ), filepath .Join (props .RepoRoot , azdoYml ),
908
- },
909
- }
910
-
911
- providerPaths , exists := paths [props .CiProvider ]
912
- if ! exists {
913
- errMsg := fmt .Sprintf ("Unknown provider: %s" , props .CiProvider )
914
- log .Println ("Error:" , errMsg )
915
- return fmt .Errorf (errMsg )
916
- }
869
+ dirPath := filepath .Join (props .RepoRoot , pipelineProviderFiles [props .CiProvider ].PipelineDirectory )
870
+ defaultFile := filepath .Join (dirPath , pipelineProviderFiles [props .CiProvider ].DefaultFile )
917
871
918
- log .Printf ("Directory path: %s" , providerPaths . directory )
919
- log .Printf ("YAML path: %s" , providerPaths . yml )
872
+ log .Printf ("Directory path: %s" , dirPath )
873
+ log .Printf ("Default YAML path: %s" , defaultFile )
920
874
921
- // Confirm with the user before adding the file
875
+ // Confirm with the user before adding the default file
922
876
pm .console .Message (ctx , "" )
923
877
pm .console .Message (ctx ,
924
878
fmt .Sprintf (
@@ -937,33 +891,32 @@ func (pm *PipelineManager) promptForCiFiles(ctx context.Context, props projectPr
937
891
pm .console .Message (ctx , "" )
938
892
939
893
if confirm {
940
- log .Printf ("Confirmed creation of %s file at %s" , filepath .Base (providerPaths . yml ), providerPaths . directory )
894
+ log .Printf ("Confirmed creation of %s file at %s" , filepath .Base (defaultFile ), dirPath )
941
895
942
- if ! osutil .DirExists (providerPaths . directory ) {
943
- log .Printf ("Creating directory %s" , providerPaths . directory )
944
- if err := os .MkdirAll (providerPaths . directory , os .ModePerm ); err != nil {
945
- return fmt .Errorf ("creating directory %s: %w" , providerPaths . directory , err )
896
+ if ! osutil .DirExists (dirPath ) {
897
+ log .Printf ("Creating directory %s" , dirPath )
898
+ if err := os .MkdirAll (dirPath , os .ModePerm ); err != nil {
899
+ return fmt .Errorf ("creating directory %s: %w" , dirPath , err )
946
900
}
947
901
}
948
902
949
- if ! osutil .FileExists (providerPaths . yml ) {
950
- if err := generatePipelineDefinition (providerPaths . yml , props ); err != nil {
903
+ if ! osutil .FileExists (defaultFile ) {
904
+ if err := generatePipelineDefinition (defaultFile , props ); err != nil {
951
905
return err
952
906
}
953
907
pm .console .Message (ctx ,
954
908
fmt .Sprintf (
955
909
"The %s file has been created at %s. You can use it as-is or modify it to suit your needs." ,
956
- output .WithHighLightFormat (filepath .Base (providerPaths . yml )),
957
- output .WithHighLightFormat (providerPaths . yml )),
910
+ output .WithHighLightFormat (filepath .Base (defaultFile )),
911
+ output .WithHighLightFormat (defaultFile )),
958
912
)
959
913
pm .console .Message (ctx , "" )
960
914
}
961
915
962
916
return nil
963
917
}
964
918
965
- log .Printf ("User declined creation of %s file at %s" , filepath .Base (providerPaths .yml ), providerPaths .directory )
966
-
919
+ log .Printf ("User declined creation of %s file at %s" , filepath .Base (defaultFile ), dirPath )
967
920
return nil
968
921
}
969
922
@@ -998,12 +951,23 @@ func generatePipelineDefinition(path string, props projectProperties) error {
998
951
return nil
999
952
}
1000
953
954
+ // hasPipelineFile checks if any pipeline files exist for the given provider in the specified repository root.
955
+ func hasPipelineFile (provider ciProviderType , repoRoot string ) bool {
956
+ for _ , path := range pipelineProviderFiles [provider ].Files {
957
+ fullPath := filepath .Join (repoRoot , path )
958
+ if osutil .FileExists (fullPath ) {
959
+ return true
960
+ }
961
+ }
962
+ return false
963
+ }
964
+
1001
965
func (pm * PipelineManager ) determineProvider (ctx context.Context , repoRoot string ) (ciProviderType , error ) {
1002
966
log .Printf ("Checking for CI/CD YAML files in the repository root: %s" , repoRoot )
1003
967
1004
968
// Check for existence of official YAML files in the repo root
1005
- hasGitHubYml := osutil . FileExists ( filepath . Join ( repoRoot , gitHubYml ) )
1006
- hasAzDevOpsYml := osutil . FileExists ( filepath . Join ( repoRoot , azdoYml ) )
969
+ hasGitHubYml := hasPipelineFile ( ciProviderGitHubActions , repoRoot )
970
+ hasAzDevOpsYml := hasPipelineFile ( ciProviderAzureDevOps , repoRoot )
1007
971
1008
972
log .Printf ("GitHub Actions YAML exists: %v" , hasGitHubYml )
1009
973
log .Printf ("Azure DevOps YAML exists: %v" , hasAzDevOpsYml )
0 commit comments