Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add excluding file and placeholder file support #879

Open
smiletrl opened this issue Jan 28, 2021 · 5 comments
Open

Add excluding file and placeholder file support #879

smiletrl opened this issue Jan 28, 2021 · 5 comments

Comments

@smiletrl
Copy link

smiletrl commented Jan 28, 2021

Is your feature request related to a problem? Please describe.
The project might include api endpoints for different ports. For example, one port for mobile, and one port for desktop. In this case, pkg/user might include pkg/user/api_mobile.go and pkg/user/api_desktop.go, and each file includes the port's own endpoints.

The feature is to allow mobile version api doc to exclude all api_desktop.go file or *_desktop.go.

Describe the solution you'd like
Use command like swag init --exclude api_desktop.go or swag init --exclude *_desktop.go to exclude files api_desktop.go or endpoints_desktop.go inside this project dir for doc generating.

Describe alternatives you've considered
I was thinking the current option --exclude should achieve this already, but it is only excluding path now, but not specific file.

Additional context
Maybe the final goal could be ignoring files like what .gitignore does for git repository ^?

@smiletrl
Copy link
Author

smiletrl commented Jan 28, 2021

It looks like I can't make a pull request against this repo, so here's a patch working for me :) Maybe helpful for someone else ^

diff --git a/cmd/swag/main.go b/cmd/swag/main.go
index 75c1756..5052540 100644
--- a/cmd/swag/main.go
+++ b/cmd/swag/main.go
@@ -40,7 +40,7 @@ var initFlags = []cli.Flag{
 	},
 	&cli.StringFlag{
 		Name:  excludeFlag,
-		Usage: "Exclude directories and files when searching, comma separated",
+		Usage: "Exclude directories and files (placeholder leading `*` is supported, e.g., `*_app.go`) when searching, comma separated",
 	},
 	&cli.StringFlag{
 		Name:    propertyStrategyFlag,
diff --git a/parser.go b/parser.go
index 4ca9bbf..43ce6cd 100644
--- a/parser.go
+++ b/parser.go
@@ -1440,6 +1440,17 @@ func (parser *Parser) Skip(path string, f os.FileInfo) error {
 				return filepath.SkipDir
 			}
 		}
+	} else {
+		if parser.excludes != nil {
+			if _, ok := parser.excludes[f.Name()]; ok { // exclude direct file name match
+				return filepath.SkipDir
+			}
+			for exclude := range parser.excludes {
+				if exclude[:1] == "*" && strings.Contains(f.Name(), exclude[1:]) { // exclude placeholder file `*some_file_name`
+					return filepath.SkipDir
+				}
+			}
+		}
 	}
 
 	return nil

@vishwanath-palagummi
Copy link

vishwanath-palagummi commented Feb 4, 2021

Another issue is that this doesn't work only directories, since the exclude list is checked only if f is directory.
You can perform the check upfront to see if the path is in the exclude list, irrespective of the type of file or directory
E.g -- exclude mydir/a.go

diff --git a/parser.go b/parser.go
index b4871f4..8d9c8e4 100644
--- a/parser.go
+++ b/parser.go
@@ -1557,6 +1557,13 @@ func (parser *Parser) parseFile(path string) error {

 // Skip returns filepath.SkipDir error if match vendor and hidden folder
 func (parser *Parser) Skip(path string, f os.FileInfo) error {
+       if parser.excludes != nil {
+               if _, ok := parser.excludes[path]; ok {
+                       fmt.Printf("Skipping path %s %t\n", path, f.IsDir())
+                       return filepath.SkipDir
+               }
+       }
+
        if f.IsDir() {
                if !parser.ParseVendor && f.Name() == "vendor" || //ignore "vendor"
                        f.Name() == "docs" || //exclude docs

@sdghchj
Copy link
Member

sdghchj commented Feb 23, 2021

Why not classify your endpoints into different directoies?

@lunemec
Copy link

lunemec commented May 2, 2022

I have ran into this issue too. It would be awesome to have either explicit "include" files or "exclude" work with files, not just directories.

@sdghchj you can have the same code for an endpoint live in 1 directory, but want to expose different documentation for different consumers.

Example would be internal API that should be complete, but external API where you have chosen to expose only a subset of necessary endpoints.

I don't think its realistic for people to change directory structure simply to work around this tool. It may also be that it would break some other tool (like some code-gen, grpc, what have you).

@Guillembonet
Copy link

Guillembonet commented May 18, 2023

@lunemec the #1585 PR addresses the issue you describe which I also faced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants