@@ -4,68 +4,83 @@ import (
4
4
"fmt"
5
5
"os"
6
6
"strings"
7
+
8
+ "github.com/github/gh-combine/internal/github"
7
9
)
8
10
9
- // ParseRepositories parses repository names from arguments or file with support for default owner
10
- func ParseRepositories (args []string , reposFile string , defaultOwner string ) ([]string , error ) {
11
- // Explicitly initialize repos as an empty slice
12
- repos := []string {}
11
+ var (
12
+ ErrEmptyRepositoriesFilePath = fmt .Errorf ("empty repositories file path" )
13
+ )
13
14
14
- // If both args and reposFile are empty, return an empty slice
15
+ func ParseRepositories ( args [] string , path string ) ([]github. Repo , error ) {
15
16
if len (args ) == 0 && reposFile == "" {
16
- return repos , nil
17
+ return nil , nil
17
18
}
18
19
19
- // Parse from command line arguments
20
- if len (args ) > 0 {
21
- // Check if repos are comma-separated
22
- for _ , arg := range args {
23
- if strings .Contains (arg , "," ) {
24
- splitRepos := strings .Split (arg , "," )
25
- for _ , repo := range splitRepos {
26
- if trimmedRepo := strings .TrimSpace (repo ); trimmedRepo != "" {
27
- repos = append (repos , applyDefaultOwner (trimmedRepo , defaultOwner ))
28
- }
29
- }
30
- } else {
31
- repos = append (repos , applyDefaultOwner (arg , defaultOwner ))
32
- }
33
- }
20
+ argsRepos , err := parseRepositoriesArgs (args )
21
+ if err != nil {
22
+ return nil , err
34
23
}
35
24
36
- // Parse from file if specified
37
- if reposFile != "" {
38
- fileContent , err := os .ReadFile (reposFile )
39
- if err != nil {
40
- return nil , fmt .Errorf ("failed to read repositories file: %w" , err )
41
- }
25
+ fileRepos , err := parseRepositoriesFile (path )
26
+ if err != nil {
27
+ return nil , err
28
+ }
42
29
43
- lines := strings .Split (string (fileContent ), "\n " )
44
- for _ , line := range lines {
45
- // Trim whitespace and ignore comments
46
- trimmedLine := strings .TrimSpace (line )
47
- if trimmedLine == "" || strings .HasPrefix (trimmedLine , "#" ) {
48
- continue
49
- }
30
+ return append (argsRepos , fileRepos ... ), nil
31
+ }
50
32
51
- // Remove inline comments
52
- if idx := strings .Index (trimmedLine , "#" ); idx != - 1 {
53
- trimmedLine = strings .TrimSpace (trimmedLine [:idx ])
54
- }
33
+ func parseRepositoriesArgs (args []string ) ([]github.Repo , error ) {
34
+ repos := []github.Repo {}
35
+
36
+ for _ , arg := range args {
37
+ for _ , rawRepo := range strings .Split (arg , "," ) {
55
38
56
- if trimmedLine != "" {
57
- repos = append (repos , applyDefaultOwner (trimmedLine , defaultOwner ))
39
+ repo , err := github .ParseRepo (rawRepo )
40
+ if err != nil {
41
+ return nil , err
58
42
}
43
+
44
+ repos = append (repos , repo )
59
45
}
60
46
}
61
47
62
48
return repos , nil
63
49
}
64
50
65
- // applyDefaultOwner adds the default owner to a repo name if it doesn't already have an owner
66
- func applyDefaultOwner ( repo string , defaultOwner string ) string {
67
- if defaultOwner == "" || strings . Contains ( repo , "/" ) {
68
- return repo
51
+ // TODO: this should be removed to accept `gh-combine < repos` instead.
52
+ func parseRepositoriesFile ( path string ) ([]github. Repo , error ) {
53
+ if path == "" {
54
+ return nil , nil
69
55
}
70
- return defaultOwner + "/" + repo
56
+
57
+ repos := []github.Repo {}
58
+
59
+ fileContent , err := os .ReadFile (path )
60
+ if err != nil {
61
+ return nil , fmt .Errorf ("failed to read repositories file %s: %w" , path , err )
62
+ }
63
+
64
+ lines := strings .Split (string (fileContent ), "\n " )
65
+ for _ , line := range lines {
66
+ line = strings .TrimSpace (line )
67
+
68
+ if line == "" || strings .HasPrefix (line , "#" ) {
69
+ continue
70
+ }
71
+
72
+ // Remove inline comments
73
+ if idx := strings .Index (line , "#" ); idx != - 1 {
74
+ line = strings .TrimSpace (line [:idx ])
75
+ }
76
+
77
+ repo , err := github .ParseRepo (line )
78
+ if err != nil {
79
+ return nil , err
80
+ }
81
+
82
+ repos = append (repos , repo )
83
+ }
84
+
85
+ return repos , nil
71
86
}
0 commit comments