Skip to content

Commit abaabc9

Browse files
committedSep 15, 2024·
config: Add support for auth.client-id
1 parent e459ca7 commit abaabc9

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed
 

‎data/config-schema.json

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@
44
"description": "User configuration for gcalcli command-line tool.\n\nSee https://pypi.org/project/gcalcli/.",
55
"type": "object",
66
"$defs": {
7+
"AuthSection": {
8+
"title": "Settings for authentication",
9+
"description": "Configuration for settings like client-id used in auth flow.\n\nNote that client-secret can't be configured here and should be passed on\ncommand-line instead for security reasons.",
10+
"type": "object",
11+
"properties": {
12+
"client-id": {
13+
"title": "Client ID for Google auth token",
14+
"description": "Google client ID for your auth client project.\n\nDetails: https://github.com/insanum/gcalcli/blob/HEAD/docs/api-auth.md",
15+
"anyOf": [
16+
{
17+
"type": "string"
18+
},
19+
{
20+
"type": "null"
21+
}
22+
],
23+
"default": null
24+
}
25+
}
26+
},
727
"CalendarsSection": {
828
"title": "Settings about the set of calendars gcalcli operates on",
929
"type": "object",
@@ -45,6 +65,9 @@
4565
}
4666
},
4767
"properties": {
68+
"auth": {
69+
"$ref": "#/$defs/AuthSection"
70+
},
4871
"calendars": {
4972
"$ref": "#/$defs/CalendarsSection"
5073
},

‎gcalcli/config.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from collections import OrderedDict
55
from enum import Enum
66
import sys
7-
from typing import Any, List, Mapping
7+
from typing import Any, List, Mapping, Optional
88

99
if sys.version_info[:2] < (3, 11):
1010
import toml as tomllib
@@ -15,6 +15,24 @@
1515
from pydantic.json_schema import GenerateJsonSchema
1616

1717

18+
class AuthSection(BaseModel):
19+
"""Configuration for settings like client-id used in auth flow.
20+
21+
Note that client-secret can't be configured here and should be passed on
22+
command-line instead for security reasons.
23+
"""
24+
25+
model_config = ConfigDict(title='Settings for authentication')
26+
27+
client_id: Optional[str] = Field(
28+
alias='client-id',
29+
title='Client ID for Google auth token',
30+
description="""Google client ID for your auth client project.\n
31+
Details: https://github.com/insanum/gcalcli/blob/HEAD/docs/api-auth.md""",
32+
default=None,
33+
)
34+
35+
1836
class CalendarsSection(BaseModel):
1937
model_config = ConfigDict(
2038
title='Settings about the set of calendars gcalcli operates on'
@@ -48,7 +66,8 @@ class OutputSection(BaseModel):
4866
week_start: WeekStart = Field(
4967
alias='week-start',
5068
title='Weekday to treat as start of week',
51-
default=WeekStart.SUNDAY)
69+
default=WeekStart.SUNDAY,
70+
)
5271

5372

5473
class Config(BaseModel):
@@ -62,6 +81,7 @@ class Config(BaseModel):
6281
json_schema_extra={'$schema': GenerateJsonSchema.schema_dialect},
6382
)
6483

84+
auth: AuthSection = Field(default_factory=AuthSection)
6585
calendars: CalendarsSection = Field(default_factory=CalendarsSection)
6686
output: OutputSection = Field(default_factory=OutputSection)
6787

@@ -72,6 +92,8 @@ def from_toml(cls, config_file):
7292

7393
def to_argparse_namespace(self) -> argparse.Namespace:
7494
kwargs = {}
95+
if self.auth:
96+
kwargs.update(vars(self.auth))
7597
if self.calendars:
7698
kwargs.update(vars(self.calendars))
7799
if self.output:

‎tests/cli/test.bats

+4-9
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,26 @@ function teardown() {
1313
temp_del "$TEST_HOME_DIR"
1414
}
1515

16-
function run_gcalcli() {
17-
# Kill any commands that hang for longer than 3s timeout.
18-
run timeout 3 gcalcli "$@"
19-
}
20-
2116
@test "can run" {
22-
run_gcalcli
17+
run gcalcli
2318
assert_failure 2
2419
assert_output --regexp 'usage: .*error:.*required: .*command'
2520
}
2621

2722
@test "prints correct help" {
28-
GCALCLI_CONFIG=/some/gcalcli/config COLUMNS=72 run_gcalcli -h
23+
GCALCLI_CONFIG=/some/gcalcli/config COLUMNS=72 run gcalcli -h
2924
assert_success
3025
assert_snapshot
3126
}
3227

3328
@test "can run init" {
34-
run_gcalcli init --client-id=SOME_CLIENT <<< "SOME_SECRET
29+
run gcalcli init --client-id=SOME_CLIENT <<< "SOME_SECRET
3530
"
3631
assert_snapshot
3732
}
3833

3934
@test "can run add" {
40-
run_gcalcli add <<< "sometitle
35+
run gcalcli add <<< "sometitle
4136
4237
tomorrow
4338

0 commit comments

Comments
 (0)