Skip to content

Commit 2cafca9

Browse files
authored
Merge pull request #4 from link-foundation/issue-3-e87aeca07ea0
feat: Add CLI tool with full glab auth login options and use command-stream
2 parents 5202587 + 8536d8f commit 2cafca9

File tree

8 files changed

+997
-195
lines changed

8 files changed

+997
-195
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'glab-setup-git-identity': minor
3+
---
4+
5+
Add CLI tool and update library to use command-stream
6+
7+
- Add `glab-setup-git-identity` CLI command with all `glab auth login` options
8+
- Update README.md to match gh-setup-git-identity structure with "Instead of manually running" section
9+
- Refactor library to use command-stream for shell command execution
10+
- Add support for all glab auth login options: --hostname, --token, --stdin, --git-protocol, --api-protocol, --api-host, --use-keyring, --job-token
11+
- Add lino-arguments dependency for CLI argument parsing
12+
- Update TypeScript definitions with new options (apiProtocol, apiHost, jobToken, stdin)

README.md

Lines changed: 204 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,219 @@
11
# glab-setup-git-identity
22

3-
Setup git identity using GitLab CLI (glab) - Configure `git user.name` and `user.email` from your GitLab account.
3+
A tool to setup git identity based on current GitLab user.
44

5-
Similar to [gh-setup-git-identity](https://github.com/link-foundation/gh-setup-git-identity) but for GitLab.
5+
[![npm version](https://img.shields.io/npm/v/glab-setup-git-identity)](https://www.npmjs.com/package/glab-setup-git-identity)
6+
[![License: Unlicense](https://img.shields.io/badge/license-Unlicense-blue.svg)](http://unlicense.org/)
7+
[![Node.js Version](https://img.shields.io/badge/node-%3E%3D20.0.0-brightgreen.svg)](https://nodejs.org/)
8+
9+
## Overview
10+
11+
`glab-setup-git-identity` is a CLI tool that simplifies setting up your git identity using your GitLab account. It automatically fetches your GitLab username and primary email address, then configures git with these values.
12+
13+
Instead of manually running:
14+
15+
```bash
16+
glab auth login --hostname gitlab.com --git-protocol https
17+
glab auth git-credential # For HTTPS authentication helper
18+
19+
USERNAME=$(glab api user --jq '.username')
20+
EMAIL=$(glab api user --jq '.email')
21+
22+
git config --global user.name "$USERNAME"
23+
git config --global user.email "$EMAIL"
24+
```
25+
26+
You can simply run:
27+
28+
```bash
29+
glab-setup-git-identity
30+
```
31+
32+
## Features
33+
34+
- **Automatic identity setup**: Fetches username and email from GitLab
35+
- **Global and local configuration**: Configure git globally or per-repository
36+
- **Authentication check**: Prompts you to login if not authenticated
37+
- **Git credential helper setup**: Automatically configures git to use GitLab CLI for HTTPS authentication
38+
- **Dry-run mode**: Preview changes without making them
39+
- **Cross-platform**: Works on macOS, Linux, and Windows
40+
- **Verbose mode**: Built-in verbose mode for debugging
41+
- **Self-hosted GitLab support**: Works with GitLab.com and self-hosted instances
642

743
## Prerequisites
844

9-
- [GitLab CLI (glab)](https://gitlab.com/gitlab-org/cli) installed
10-
- Authenticated with GitLab via `glab auth login`
45+
- Node.js >= 20.0.0 (or Bun >= 1.0.0)
46+
- Git (installed and configured)
47+
- GitLab CLI (`glab`) installed
48+
49+
To install GitLab CLI, see: https://gitlab.com/gitlab-org/cli#installation
1150

1251
## Installation
1352

53+
### Global Installation (CLI)
54+
55+
```bash
56+
# Using npm
57+
npm install -g glab-setup-git-identity
58+
59+
# Using bun
60+
bun install -g glab-setup-git-identity
61+
```
62+
63+
### Local Installation (Library)
64+
1465
```bash
1566
# Using npm
1667
npm install glab-setup-git-identity
1768

1869
# Using bun
1970
bun install glab-setup-git-identity
71+
```
72+
73+
## CLI Usage
74+
75+
### Basic Usage
76+
77+
```bash
78+
# Setup git identity globally (default)
79+
glab-setup-git-identity
80+
81+
# Setup git identity for current repository only
82+
glab-setup-git-identity --local
83+
84+
# Preview what would be configured (dry run)
85+
glab-setup-git-identity --dry-run
86+
87+
# Verify current git identity configuration
88+
glab-setup-git-identity --verify
89+
90+
# Enable verbose output
91+
glab-setup-git-identity --verbose
92+
```
93+
94+
### CLI Options
95+
96+
```
97+
Usage: glab-setup-git-identity [options]
98+
99+
Git Identity Options:
100+
--global, -g Set git config globally (default: true)
101+
--local, -l Set git config locally (in current repository)
102+
--dry-run, --dry Dry run - show what would be done without making changes
103+
--verify Verify current git identity configuration
104+
--verbose, -v Enable verbose output
105+
106+
GitLab Authentication Options:
107+
--hostname GitLab hostname to authenticate with (default: gitlab.com)
108+
--token, -t GitLab access token (reads from stdin if --stdin is used)
109+
--stdin Read token from standard input
110+
--git-protocol, -p Protocol for git operations: ssh, https, or http (default: https)
111+
--api-protocol Protocol for API calls: https or http (default: https)
112+
--api-host Custom API host URL
113+
--use-keyring Store token in system keyring
114+
--job-token, -j CI job token for authentication
115+
116+
General Options:
117+
--help, -h Show help
118+
--version Show version number
119+
```
120+
121+
### Advanced Authentication Examples
122+
123+
```bash
124+
# Authenticate with self-hosted GitLab
125+
glab-setup-git-identity --hostname gitlab.company.com
126+
127+
# Use SSH protocol instead of HTTPS
128+
glab-setup-git-identity --git-protocol ssh
129+
130+
# Authenticate with token from environment variable
131+
echo "$GITLAB_TOKEN" | glab-setup-git-identity --stdin
132+
133+
# Use token-based authentication directly
134+
glab-setup-git-identity --token glpat-xxxxx
135+
136+
# Store credentials in system keyring
137+
glab-setup-git-identity --use-keyring
138+
139+
# Use in CI/CD with job token
140+
glab-setup-git-identity --job-token "$CI_JOB_TOKEN" --hostname gitlab.company.com
141+
```
142+
143+
### First Run (Not Authenticated)
144+
145+
If you haven't authenticated with GitLab CLI yet, the tool will automatically start the authentication process:
146+
147+
```
148+
GitLab CLI is not authenticated. Starting authentication...
149+
150+
Starting GitLab CLI authentication...
20151
21-
# Using yarn
22-
yarn add glab-setup-git-identity
152+
? What GitLab instance do you want to log into? gitlab.com
153+
? What is your preferred protocol for Git operations? HTTPS
154+
? How would you like to login? Token
23155
24-
# Using pnpm
25-
pnpm add glab-setup-git-identity
156+
Tip: you can generate a Personal Access Token here https://gitlab.com/-/profile/personal_access_tokens
157+
The minimum required scopes are 'api' and 'write_repository'.
158+
? Paste your authentication token:
26159
```
27160

28-
## Quick Start
161+
The tool runs `glab auth login` automatically, followed by configuring git to use GitLab CLI as the credential helper. Follow the prompts to complete login.
29162

30-
### Manual Setup (using glab directly)
163+
If automatic authentication fails, you can run the commands manually:
31164

32165
```bash
33-
# Install glab CLI
34-
brew install glab # macOS
35-
# or see https://gitlab.com/gitlab-org/cli#installation for other platforms
166+
glab auth login --hostname gitlab.com --git-protocol https
167+
```
168+
169+
### Successful Run
170+
171+
```
172+
Fetching GitLab user information...
173+
GitLab user: your-username
174+
GitLab email: your-email@example.com
175+
176+
Configuring git (global)...
177+
Git identity configured successfully!
178+
179+
Git configured:
180+
user.name: your-username
181+
user.email: your-email@example.com
182+
Scope: global (--global)
36183
37-
# Authenticate with GitLab
38-
glab auth login
184+
Git identity setup complete!
39185
40-
# Clone a repository
41-
git clone https://gitlab.com/your-username/your-repo
186+
You can verify your configuration with:
187+
glab auth status
188+
git config --global user.name
189+
git config --global user.email
42190
```
43191

44-
### Using this library
192+
### Verifying Configuration
193+
194+
You can verify your git identity configuration at any time using:
195+
196+
```bash
197+
glab-setup-git-identity --verify
198+
```
199+
200+
Or by running the verification commands directly:
201+
202+
```bash
203+
glab auth status
204+
git config --global user.name
205+
git config --global user.email
206+
```
207+
208+
For local repository configuration, use `--local`:
209+
210+
```bash
211+
glab-setup-git-identity --verify --local
212+
git config --local user.name
213+
git config --local user.email
214+
```
215+
216+
## Library Usage
45217

46218
```javascript
47219
import {
@@ -103,11 +275,17 @@ await runGlabAuthLogin({
103275
gitProtocol: 'https', // 'ssh', 'https', or 'http'
104276
useKeyring: true, // store token in OS keyring
105277
});
278+
279+
// Login with CI job token
280+
await runGlabAuthLogin({
281+
hostname: 'gitlab.company.com',
282+
jobToken: 'CI_JOB_TOKEN_VALUE',
283+
});
106284
```
107285

108286
#### `runGlabAuthSetupGit(options?)`
109287

110-
Configure git to use GitLab CLI as a credential helper for HTTPS operations. This is the equivalent of `gh auth setup-git` for GitHub CLI.
288+
Configure git to use GitLab CLI as a credential helper for HTTPS operations.
111289

112290
Without this configuration, `git push/pull` may fail with "could not read Username" error when using HTTPS protocol.
113291

@@ -219,6 +397,7 @@ console.log(defaultAuthOptions);
219397
// {
220398
// hostname: 'gitlab.com',
221399
// gitProtocol: 'https',
400+
// apiProtocol: 'https',
222401
// useKeyring: false
223402
// }
224403
```
@@ -240,6 +419,12 @@ await setupGitIdentity({
240419
});
241420
```
242421

422+
Or via CLI:
423+
424+
```bash
425+
glab-setup-git-identity --hostname gitlab.company.com
426+
```
427+
243428
## TypeScript Support
244429

245430
This package includes TypeScript type definitions. All interfaces are exported:

0 commit comments

Comments
 (0)