Skip to content

Commit 083229d

Browse files
committed
feat(cli): add subcommands to the CLI
1 parent 7229d1f commit 083229d

File tree

4 files changed

+119
-107
lines changed

4 files changed

+119
-107
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
- [Introduction](#introduction)
44
- [Installation](#installation)
5-
- [Using pre-compiled executables](#using-pre-compiled-executables)
6-
- [Installing from sources](#installing-from-sources)
5+
- [Using pre-compiled executables](#using-pre-compiled-executables)
6+
- [Installing from sources](#installing-from-sources)
77
- [Generate My Token](#generate-my-token)
8-
- [GitHub](#github)
9-
- [GitLab](#gitlab)
8+
- [GitHub](#github)
9+
- [GitLab](#gitlab)
1010
- [Compute my code fingerprints](#compute-my-code-fingerprints)
11-
- [GitHub](#github-1)
12-
- [GitLab](#gitlab-1)
13-
- [Bitbucket server (formely Atlassian Stash)](#bitbucket-server-formely-atlassian-stash)
14-
- [Repository](#repository)
11+
- [GitHub](#github-1)
12+
- [GitLab](#gitlab-1)
13+
- [Bitbucket server (formely Atlassian Stash)](#bitbucket-server-formely-atlassian-stash)
14+
- [Repository](#repository)
1515
- [License](#License)
1616

1717
## Introduction
1818

1919
The purpose of `src-fingerprint` is to provide an easy way to extract git related information (namely all file shas of a repository) from your hosted source version control system.
2020

21-
This util supports 3 main version control systems:
21+
This util's main command is the `collect` command used to collect source code fingerprints from a version control system or a local repository. It supports 3 main VCS:
2222

2323
- GitHub and GitHub Enterprise
2424
- Gitlab CE and EE
@@ -94,7 +94,7 @@ $ go get -u github.com/gitguardian/src-fingerprint/cmd/src-fingerprint
9494
3. Click the `read repository` box. This is the only scope we need. You can set an end-date for the token validity if you want more security
9595
4. Click on `Create personal token`. The token will only be available at this time so make sure you keep it in a safe place
9696

97-
## Compute my code fingerprints
97+
## Collect my code fingerprints
9898

9999
### General information
100100

@@ -171,31 +171,31 @@ Allows the processing of a single repository given a git clone URL
171171
1. ssh cloning
172172

173173
```sh
174-
src-fingerprint -p repository -u '[email protected]:GitGuardian/gg-shield.git'
174+
src-fingerprint collect -p repository -u '[email protected]:GitGuardian/gg-shield.git'
175175
```
176176

177177
2. http cloning with basic authentication
178178

179179
```sh
180-
src-fingerprint -p repository -u 'https://user:[email protected]/GitGuardian/gg-shield.git'
180+
src-fingerprint collect -p repository -u 'https://user:[email protected]/GitGuardian/gg-shield.git'
181181
```
182182

183183
2. http cloning without basic authentication
184184

185185
```sh
186-
src-fingerprint -p repository -u 'https://github.com/GitGuardian/gg-shield.git'
186+
src-fingerprint collect -p repository -u 'https://github.com/GitGuardian/gg-shield.git'
187187
```
188188

189189
3. repository in a local directory
190190

191191
```sh
192-
src-fingerprint -p repository -u /projects/gitlab/src-fingerprint
192+
src-fingerprint collect -p repository -u /projects/gitlab/src-fingerprint
193193
```
194194

195195
4. repository in current directory
196196

197197
```sh
198-
src-fingerprint -p repository -u .
198+
src-fingerprint collect -p repository -u .
199199
```
200200

201201
## License

cmd/src-fingerprint/main.go

Lines changed: 102 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -90,106 +90,117 @@ func main() {
9090
app := &cli.App{
9191
Name: "src-fingerprint",
9292
Version: version,
93-
Usage: "Collect user/organization source code fingerprints from your vcs provider of choice",
94-
Flags: []cli.Flag{
95-
&cli.BoolFlag{
96-
Name: "verbose",
97-
Aliases: []string{"v"},
98-
Value: false,
99-
Usage: "verbose logging",
100-
},
101-
&cli.BoolFlag{
102-
Name: "debug",
103-
Value: false,
104-
Usage: "debug logging, override verbose",
105-
},
106-
&cli.BoolFlag{
107-
Name: "include-forked-repos",
108-
Value: false,
109-
Usage: "include forked repositories. Available for 'github' and 'gitlab' providers.",
110-
},
111-
&cli.BoolFlag{
112-
Name: "include-public-repos",
113-
Value: false,
114-
Usage: "Include fileshas from both public and private repositories. Available for 'github' provider only.",
115-
},
116-
&cli.BoolFlag{
117-
Name: "include-archived-repos",
118-
Value: false,
119-
Usage: "Include archived repositories. Available for 'github' provider only.",
120-
},
121-
&cli.StringFlag{
122-
Name: "output",
123-
Aliases: []string{"o"},
124-
Value: "./fingerprints.jsonl.gz",
125-
Usage: "set output path to `FILE`. Use \"-\" to redirect to stdout.",
126-
},
127-
&cli.StringFlag{
128-
Name: "export-format",
129-
Aliases: []string{"f"},
130-
Value: "gzip-jsonl",
131-
Usage: "export format: 'jsonl'/'gzip-jsonl'/'json'/'gzip-json'",
132-
},
133-
&cli.StringFlag{
134-
Name: "clone-dir",
135-
Value: "-",
136-
Usage: "set cloning location for repositories",
137-
},
138-
&cli.StringFlag{
139-
Name: "after",
140-
Value: "",
141-
Usage: "set a commit date after which we want to collect fileshas",
142-
},
143-
&cli.StringFlag{
144-
Name: "provider",
145-
Aliases: []string{"p"},
146-
Required: true,
147-
Usage: "vcs provider. options: 'gitlab'/'github'/'bitbucket'/'repository'",
148-
},
149-
&cli.StringFlag{
150-
Name: "repo-name",
151-
Usage: "Name of the repository to display in outputs if the provider is 'repository'",
152-
},
153-
&cli.BoolFlag{
154-
Name: "repo-is-private",
155-
Value: false,
156-
Usage: "Private status value to display in outputs if the provider is 'repository'",
157-
},
158-
&cli.StringFlag{
159-
Name: "token",
160-
Aliases: []string{"t"},
161-
Usage: "token for vcs access.",
162-
EnvVars: []string{"VCS_TOKEN", "GITLAB_TOKEN", "GITHUB_TOKEN"},
163-
},
164-
&cli.StringFlag{
165-
Name: "object",
166-
Aliases: []string{"u"},
167-
Usage: "repository|org|group to scrape. If not specified all reachable repositories will be collected.",
168-
},
169-
&cli.IntFlag{
170-
Name: "cloners",
171-
Value: DefaultClonerN,
172-
Usage: "number of cloners, more cloners means more memory usage",
173-
},
174-
&cli.StringFlag{
175-
Name: "provider-url",
176-
Usage: "base URL of the Git provider API. If not set, defaults URL are used.",
177-
},
178-
&cli.IntFlag{
179-
Name: "limit",
180-
Value: DefaultLimit,
181-
Usage: "maximum number of repositories to analyze (0 for unlimited).",
93+
Usage: "src-fingerprint is a tool to collect and manipulate source code fingerprints.",
94+
Commands: []*cli.Command{
95+
{
96+
Name: "collect",
97+
Usage: "Collect user or organization source code fingerprints from a vcs provider or from a local repository.",
98+
Action: collectAction,
99+
Flags: []cli.Flag{
100+
&cli.StringFlag{
101+
Name: "provider",
102+
Aliases: []string{"p"},
103+
Required: true,
104+
Usage: "Source code provider. options: 'gitlab'/'github'/'bitbucket'/'repository'",
105+
},
106+
&cli.StringFlag{
107+
Name: "provider-url",
108+
Usage: "base URL of the Git provider API. If not set, defaults URL are used.",
109+
},
110+
&cli.StringFlag{
111+
Name: "object",
112+
Aliases: []string{"u"},
113+
Usage: "repository|org|group to scrape. If not specified all reachable repositories will be collected.",
114+
},
115+
&cli.BoolFlag{
116+
Name: "verbose",
117+
Aliases: []string{"v"},
118+
Value: false,
119+
Usage: "Run with verbose logging",
120+
},
121+
&cli.BoolFlag{
122+
Name: "debug",
123+
Value: false,
124+
Usage: "debug logging, override verbose",
125+
},
126+
&cli.BoolFlag{
127+
Name: "debug",
128+
Value: false,
129+
Usage: "Run with debug logging, overrides verbose",
130+
},
131+
&cli.BoolFlag{
132+
Name: "include-forked-repos",
133+
Value: false,
134+
Usage: "include forked repositories. Available for 'github' and 'gitlab' providers.",
135+
},
136+
&cli.BoolFlag{
137+
Name: "include-public-repos",
138+
Value: false,
139+
Usage: "Include fileshas from both public and private repositories. Available for 'github' provider only.",
140+
},
141+
&cli.BoolFlag{
142+
Name: "include-archived-repos",
143+
Value: false,
144+
Usage: "Include archived repositories. Available for 'github' provider only.",
145+
},
146+
&cli.StringFlag{
147+
Name: "export-format",
148+
Aliases: []string{"f"},
149+
Value: "gzip-jsonl",
150+
Usage: "export format: 'jsonl'/'gzip-jsonl'/'json'/'gzip-json'",
151+
},
152+
&cli.StringFlag{
153+
Name: "output",
154+
Aliases: []string{"o"},
155+
Value: "./fingerprints.jsonl.gz",
156+
Usage: "set output path to `FILE`. Use \"-\" to redirect to stdout.",
157+
},
158+
&cli.StringFlag{
159+
Name: "clone-dir",
160+
Value: "-",
161+
Usage: "set cloning location for repositories",
162+
},
163+
&cli.StringFlag{
164+
Name: "after",
165+
Value: "",
166+
Usage: "set a commit date after which we want to collect fileshas",
167+
},
168+
&cli.StringFlag{
169+
Name: "repo-name",
170+
Usage: "Name of the repository to display in outputs if the provider is 'repository'",
171+
},
172+
&cli.BoolFlag{
173+
Name: "repo-is-private",
174+
Value: false,
175+
Usage: "Private status value to display in outputs if the provider is 'repository'",
176+
},
177+
&cli.StringFlag{
178+
Name: "token",
179+
Aliases: []string{"t"},
180+
Usage: "token for vcs access.",
181+
EnvVars: []string{"VCS_TOKEN", "GITLAB_TOKEN", "GITHUB_TOKEN"},
182+
},
183+
&cli.IntFlag{
184+
Name: "cloners",
185+
Value: DefaultClonerN,
186+
Usage: "number of cloners, more cloners means more memory usage",
187+
},
188+
&cli.IntFlag{
189+
Name: "limit",
190+
Value: DefaultLimit,
191+
Usage: "maximum number of repositories to analyze (0 for unlimited).",
192+
},
193+
},
182194
},
183195
},
184-
Action: mainAction,
185196
}
186197

187198
if err := app.Run(os.Args); err != nil {
188199
log.Fatal(err)
189200
}
190201
}
191202

192-
func mainAction(c *cli.Context) error {
203+
func collectAction(c *cli.Context) error {
193204
if c.Bool("debug") {
194205
log.SetLevel(log.DebugLevel)
195206
} else if c.Bool("verbose") {

provider/generic_repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (p *GenericProvider) Gather(user string) ([]GitRepository, error) {
5151
if user == "" {
5252
return nil, errors.New(
5353
"this provider requires an object. " +
54-
"Example: src-fingerprint -p repository -u 'https://github.com/GitGuardian/src-fingerprint'",
54+
"Example: src-fingerprint collect -p repository -u 'https://github.com/GitGuardian/src-fingerprint'",
5555
)
5656
}
5757

tests/integration_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def run_src_fingerprint(provider: str, args: Optional[List[str]] = []):
1111
subprocess.run(
1212
[
1313
"./src-fingerprint",
14+
"collect",
1415
"-p",
1516
provider,
1617
"--token",

0 commit comments

Comments
 (0)