primitive 0.1.0__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. primitive-0.1.2/.git-hooks/pre-commit +19 -0
  2. primitive-0.1.2/.gitattributes +1 -0
  3. primitive-0.1.2/.github/workflows/lint.yml +19 -0
  4. primitive-0.1.2/.github/workflows/publish.yml +33 -0
  5. primitive-0.1.2/.gitignore +134 -0
  6. primitive-0.1.2/.vscode/settings.json +3 -0
  7. primitive-0.1.2/LICENSE.txt +9 -0
  8. primitive-0.1.2/Makefile +54 -0
  9. primitive-0.1.2/PKG-INFO +49 -0
  10. primitive-0.1.2/README.md +21 -0
  11. primitive-0.1.2/pyproject.toml +93 -0
  12. primitive-0.1.2/requirements.txt +123 -0
  13. primitive-0.1.2/src/primitive/__about__.py +4 -0
  14. primitive-0.1.2/src/primitive/__init__.py +3 -0
  15. primitive-0.1.2/src/primitive/auth/actions.py +43 -0
  16. primitive-0.1.2/src/primitive/auth/commands.py +77 -0
  17. primitive-0.1.2/src/primitive/cli.py +60 -0
  18. primitive-0.1.2/src/primitive/client.py +66 -0
  19. primitive-0.1.2/src/primitive/files/actions.py +76 -0
  20. primitive-0.1.2/src/primitive/files/commands.py +28 -0
  21. primitive-0.1.2/src/primitive/graphql/__init__.py +0 -0
  22. primitive-0.1.2/src/primitive/graphql/sdk.py +53 -0
  23. primitive-0.1.2/src/primitive/hardware/actions.py +447 -0
  24. primitive-0.1.2/src/primitive/hardware/commands.py +53 -0
  25. primitive-0.1.2/src/primitive/lint/actions.py +6 -0
  26. primitive-0.1.2/src/primitive/lint/commands.py +15 -0
  27. primitive-0.1.2/src/primitive/projects/__init__.py +0 -0
  28. primitive-0.1.2/src/primitive/projects/actions.py +55 -0
  29. primitive-0.1.2/src/primitive/simulations/__init__.py +0 -0
  30. primitive-0.1.2/src/primitive/simulations/actions.py +48 -0
  31. primitive-0.1.2/src/primitive/utils/actions.py +9 -0
  32. primitive-0.1.2/src/primitive/utils/config.py +34 -0
  33. primitive-0.1.2/src/primitive/utils/git.py +15 -0
  34. primitive-0.1.2/src/primitive/utils/memory_size.py +87 -0
  35. primitive-0.1.2/src/primitive/utils/printer.py +14 -0
  36. primitive-0.1.2/tests/__init__.py +3 -0
  37. primitive-0.1.0/PKG-INFO +0 -15
  38. primitive-0.1.0/README.md +0 -1
  39. primitive-0.1.0/primitive/main.py +0 -18
  40. primitive-0.1.0/primitive/scope/__init__.py +0 -2
  41. primitive-0.1.0/primitive/scope/simulator.py +0 -12
  42. primitive-0.1.0/primitive/scope/watch.py +0 -42
  43. primitive-0.1.0/pyproject.toml +0 -18
  44. {primitive-0.1.0/primitive → primitive-0.1.2/src/primitive/auth}/__init__.py +0 -0
@@ -0,0 +1,19 @@
1
+ #!/bin/sh
2
+ # lint, if linting errors, then format, then lint again
3
+ make lint
4
+ if [ $? -ne 0 ]; then
5
+ echo "lint errors. attempting to format."
6
+ make format
7
+ if [ $? -ne 0 ]; then
8
+ echo "format found errors. Aborting commit."
9
+ exit 1
10
+ fi
11
+ make lint
12
+ if [ $? -ne 0 ]; then
13
+ echo "lint errors. Aborting commit."
14
+ exit 1
15
+ fi
16
+ fi
17
+
18
+ # If there are no errors, continue with the commit
19
+ exit 0
@@ -0,0 +1 @@
1
+ * text=auto
@@ -0,0 +1,19 @@
1
+ name: Lint
2
+
3
+ on:
4
+ push:
5
+ branches: ["*"]
6
+ pull_request:
7
+ branches: ["*"]
8
+
9
+ jobs:
10
+ lint:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ max-parallel: 4
14
+ matrix:
15
+ python-version: [3.12]
16
+
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+ - uses: chartboost/ruff-action@v1
@@ -0,0 +1,33 @@
1
+ name: Publish
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ push:
6
+ # Publish semver tags as releases.
7
+ tags: ["v*.*.*"]
8
+
9
+ jobs:
10
+ publish:
11
+ runs-on: ubuntu-latest
12
+ environment:
13
+ name: pypi
14
+ url: https://pypi.org/p/primitive
15
+ permissions:
16
+ id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
17
+ contents: read
18
+ strategy:
19
+ matrix:
20
+ python-version: [3.12]
21
+
22
+ steps:
23
+ - uses: actions/checkout@v4
24
+
25
+ - name: Install Hatch
26
+ uses: pypa/hatch@install
27
+
28
+ - name: Build Package
29
+ run: |
30
+ hatch build
31
+
32
+ - name: Publish package distributions to PyPI
33
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,134 @@
1
+ *certbot-files/
2
+ *staticfiles/
3
+
4
+ # Byte-compiled / optimized / DLL files
5
+ __pycache__/
6
+ *.py[cod]
7
+ *$py.class
8
+
9
+ # C extensions
10
+ *.so
11
+
12
+ # Distribution / packaging
13
+ .Python
14
+ build/
15
+ develop-eggs/
16
+ dist/
17
+ downloads/
18
+ eggs/
19
+ .eggs/
20
+ lib/
21
+ lib64/
22
+ parts/
23
+ sdist/
24
+ var/
25
+ wheels/
26
+ pip-wheel-metadata/
27
+ share/python-wheels/
28
+ *.egg-info/
29
+ .installed.cfg
30
+ *.egg
31
+ MANIFEST
32
+
33
+ # PyInstaller
34
+ # Usually these files are written by a python script from a template
35
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
36
+ *.manifest
37
+ *.spec
38
+
39
+ # Installer logs
40
+ pip-log.txt
41
+ pip-delete-this-directory.txt
42
+
43
+ # Unit test / coverage reports
44
+ htmlcov/
45
+ .tox/
46
+ .nox/
47
+ .coverage
48
+ .coverage.*
49
+ .cache
50
+ nosetests.xml
51
+ coverage.xml
52
+ *.cover
53
+ *.py,cover
54
+ .hypothesis/
55
+ .pytest_cache/
56
+
57
+ # Translations
58
+ *.mo
59
+ *.pot
60
+
61
+ # Django stuff:
62
+ *.log
63
+ local_settings.py
64
+ db.sqlite3
65
+ db.sqlite3-journal
66
+
67
+ # Flask stuff:
68
+ instance/
69
+ .webassets-cache
70
+
71
+ # Scrapy stuff:
72
+ .scrapy
73
+
74
+ # Sphinx documentation
75
+ docs/_build/
76
+
77
+ # PyBuilder
78
+ target/
79
+
80
+ # Jupyter Notebook
81
+ .ipynb_checkpoints
82
+
83
+ # IPython
84
+ profile_default/
85
+ ipython_config.py
86
+
87
+ # pyenv
88
+ .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
98
+ __pypackages__/
99
+
100
+ # Celery stuff
101
+ celerybeat-schedule
102
+ celerybeat.pid
103
+
104
+ # SageMath parsed files
105
+ *.sage.py
106
+
107
+ # Environments
108
+ .env
109
+ .venv
110
+ env/
111
+ venv/
112
+ ENV/
113
+ env.bak/
114
+ venv.bak/
115
+
116
+ # Spyder project settings
117
+ .spyderproject
118
+ .spyproject
119
+
120
+ # Rope project settings
121
+ .ropeproject
122
+
123
+ # mkdocs documentation
124
+ /site
125
+
126
+ # mypy
127
+ .mypy_cache/
128
+ .dmypy.json
129
+ dmypy.json
130
+
131
+ # Pyre type checker
132
+ .pyre/
133
+
134
+ .DS_Store
@@ -0,0 +1,3 @@
1
+ {
2
+ "cSpell.words": ["Checkin", "loguru", "machdep"]
3
+ }
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present Dylan Stein <dylan@steins.studio>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ # Makefile with phony targets
2
+
3
+ .PHONY: setup lint format setup-hooks services
4
+ # OS := $(shell uname)
5
+
6
+ .ONESHELL:
7
+ SHELL := /opt/homebrew/bin/fish
8
+ # .SHELLFLAGS += -e
9
+ setup : ## Setup Environment
10
+ @make setup-git-hooks
11
+ cp .env.example .env
12
+ uv venv
13
+ source .venv/bin/activate.fish
14
+ uv pip install -r requirements.txt
15
+ uv pip install -e .
16
+
17
+ setup-ci: ## Setup the environment for CI
18
+ cp .env.example .env
19
+ uv venv
20
+ source .venv/bin/activate.fish
21
+ uv pip install -r requirements.txt
22
+ uv pip install -e .
23
+
24
+ test: ## test
25
+ @ENVIRONMENT=test python run pytest
26
+
27
+ lint: ## Lint Python Files
28
+ @source .venv/bin/activate.fish
29
+ ruff check
30
+ # pyright
31
+
32
+ format: ## Format Python Files
33
+ @source .venv/bin/activate.fish
34
+ ruff format
35
+ ruff check --fix
36
+
37
+ install: ## Install dependencies
38
+ @source .venv/bin/activate.fish
39
+ uv pip install -r requirements.txt
40
+
41
+ lock: ## Lock dependencies
42
+ @source .venv/bin/activate.fish
43
+ uv pip compile pyproject.toml -o requirements.txt
44
+
45
+ setup-git-hooks: ## Setup Git Hook
46
+ @echo "Setting up Git hooks..."
47
+ cp ./.git-hooks/pre-commit ./.git/hooks/pre-commit
48
+ chmod +x ./.git/hooks/pre-commit
49
+
50
+ build: ## Build the project
51
+ @hatch build
52
+
53
+ help: ## show help message
54
+ @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[$$()% a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
@@ -0,0 +1,49 @@
1
+ Metadata-Version: 2.3
2
+ Name: primitive
3
+ Version: 0.1.2
4
+ Project-URL: Documentation, https://github.com//primitivecorp/primitive-cli#readme
5
+ Project-URL: Issues, https://github.com//primitivecorp/primitive-cli/issues
6
+ Project-URL: Source, https://github.com//primitivecorp/primitive-cli
7
+ Author-email: Dylan Stein <dylan@primitive.tech>, Chase Zimmerman <chase@primitive.tech>
8
+ License-Expression: MIT
9
+ License-File: LICENSE.txt
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Programming Language :: Python
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.8
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: Implementation :: CPython
19
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
20
+ Requires-Python: >=3.12
21
+ Requires-Dist: click
22
+ Requires-Dist: gql[all]
23
+ Requires-Dist: ipdb
24
+ Requires-Dist: loguru
25
+ Requires-Dist: pyright
26
+ Requires-Dist: ruff
27
+ Description-Content-Type: text/markdown
28
+
29
+ # primitive
30
+
31
+ [![PyPI - Version](https://img.shields.io/pypi/v/primitive-cli.svg)](https://pypi.org/project/primitive-cli)
32
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/primitive-cli.svg)](https://pypi.org/project/primitive-cli)
33
+
34
+ ---
35
+
36
+ **Table of Contents**
37
+
38
+ - [Installation](#installation)
39
+ - [License](#license)
40
+
41
+ ## Installation
42
+
43
+ ```console
44
+ pip install primitive
45
+ ```
46
+
47
+ ## License
48
+
49
+ `primitive` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
@@ -0,0 +1,21 @@
1
+ # primitive
2
+
3
+ [![PyPI - Version](https://img.shields.io/pypi/v/primitive-cli.svg)](https://pypi.org/project/primitive-cli)
4
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/primitive-cli.svg)](https://pypi.org/project/primitive-cli)
5
+
6
+ ---
7
+
8
+ **Table of Contents**
9
+
10
+ - [Installation](#installation)
11
+ - [License](#license)
12
+
13
+ ## Installation
14
+
15
+ ```console
16
+ pip install primitive
17
+ ```
18
+
19
+ ## License
20
+
21
+ `primitive` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
@@ -0,0 +1,93 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "primitive"
7
+ dynamic = ["version"]
8
+ description = ''
9
+ readme = "README.md"
10
+ requires-python = ">=3.12"
11
+ license = "MIT"
12
+ keywords = []
13
+ authors = [
14
+ { name = "Dylan Stein", email = "dylan@primitive.tech" },
15
+ { name = "Chase Zimmerman", email = "chase@primitive.tech" },
16
+ ]
17
+ classifiers = [
18
+ "Development Status :: 4 - Beta",
19
+ "Programming Language :: Python",
20
+ "Programming Language :: Python :: 3",
21
+ "Programming Language :: Python :: 3.8",
22
+ "Programming Language :: Python :: 3.9",
23
+ "Programming Language :: Python :: 3.10",
24
+ "Programming Language :: Python :: 3.11",
25
+ "Programming Language :: Python :: 3.12",
26
+ "Programming Language :: Python :: Implementation :: CPython",
27
+ "Programming Language :: Python :: Implementation :: PyPy",
28
+ ]
29
+ dependencies = [
30
+ "ipdb",
31
+ "ruff",
32
+ "pyright",
33
+ "click",
34
+ "gql[all]",
35
+ "loguru"
36
+ ]
37
+
38
+ [project.urls]
39
+ Documentation = "https://github.com//primitivecorp/primitive-cli#readme"
40
+ Issues = "https://github.com//primitivecorp/primitive-cli/issues"
41
+ Source = "https://github.com//primitivecorp/primitive-cli"
42
+
43
+ [project.scripts]
44
+ primitive = "primitive.cli:cli"
45
+
46
+ [tool.hatch.version]
47
+ path = "src/primitive/__about__.py"
48
+
49
+ [tool.hatch.envs.default]
50
+ dependencies = [
51
+ "coverage[toml]>=6.5",
52
+ "pytest",
53
+ ]
54
+ [tool.hatch.envs.default.scripts]
55
+ test = "pytest {args:tests}"
56
+ test-cov = "coverage run -m pytest {args:tests}"
57
+ cov-report = [
58
+ "- coverage combine",
59
+ "coverage report",
60
+ ]
61
+ cov = [
62
+ "test-cov",
63
+ "cov-report",
64
+ ]
65
+
66
+ [[tool.hatch.envs.all.matrix]]
67
+ python = ["3.8", "3.9", "3.10", "3.11", "3.12"]
68
+
69
+ [tool.hatch.envs.types]
70
+ dependencies = [
71
+ "mypy>=1.0.0",
72
+ ]
73
+ [tool.hatch.envs.types.scripts]
74
+ check = "mypy --install-types --non-interactive {args:src/primitive tests}"
75
+
76
+ [tool.coverage.run]
77
+ source_pkgs = ["primitive", "tests"]
78
+ branch = true
79
+ parallel = true
80
+ omit = [
81
+ "src/primitive/__about__.py",
82
+ ]
83
+
84
+ [tool.coverage.paths]
85
+ primitive = ["src/primitive", "*/primitive/src/primitive"]
86
+ tests = ["tests", "*/primitive/tests"]
87
+
88
+ [tool.coverage.report]
89
+ exclude_lines = [
90
+ "no cov",
91
+ "if __name__ == .__main__.:",
92
+ "if TYPE_CHECKING:",
93
+ ]
@@ -0,0 +1,123 @@
1
+ # This file was autogenerated by uv via the following command:
2
+ # uv pip compile pyproject.toml -o requirements.txt
3
+ aiohappyeyeballs==2.3.7
4
+ # via aiohttp
5
+ aiohttp==3.10.4
6
+ # via gql
7
+ aiosignal==1.3.1
8
+ # via aiohttp
9
+ anyio==4.4.0
10
+ # via
11
+ # gql
12
+ # httpx
13
+ asttokens==2.4.1
14
+ # via stack-data
15
+ attrs==24.2.0
16
+ # via aiohttp
17
+ backoff==2.2.1
18
+ # via gql
19
+ botocore==1.35.0
20
+ # via gql
21
+ certifi==2024.7.4
22
+ # via
23
+ # httpcore
24
+ # httpx
25
+ # requests
26
+ charset-normalizer==3.3.2
27
+ # via requests
28
+ click==8.1.7
29
+ # via primitive (pyproject.toml)
30
+ decorator==5.1.1
31
+ # via
32
+ # ipdb
33
+ # ipython
34
+ executing==2.0.1
35
+ # via stack-data
36
+ frozenlist==1.4.1
37
+ # via
38
+ # aiohttp
39
+ # aiosignal
40
+ gql==3.5.0
41
+ # via primitive (pyproject.toml)
42
+ graphql-core==3.2.3
43
+ # via gql
44
+ h11==0.14.0
45
+ # via httpcore
46
+ httpcore==1.0.5
47
+ # via httpx
48
+ httpx==0.27.0
49
+ # via gql
50
+ idna==3.7
51
+ # via
52
+ # anyio
53
+ # httpx
54
+ # requests
55
+ # yarl
56
+ ipdb==0.13.13
57
+ # via primitive (pyproject.toml)
58
+ ipython==8.26.0
59
+ # via ipdb
60
+ jedi==0.19.1
61
+ # via ipython
62
+ jmespath==1.0.1
63
+ # via botocore
64
+ loguru==0.7.2
65
+ # via primitive (pyproject.toml)
66
+ matplotlib-inline==0.1.7
67
+ # via ipython
68
+ multidict==6.0.5
69
+ # via
70
+ # aiohttp
71
+ # yarl
72
+ nodeenv==1.9.1
73
+ # via pyright
74
+ parso==0.8.4
75
+ # via jedi
76
+ pexpect==4.9.0
77
+ # via ipython
78
+ prompt-toolkit==3.0.47
79
+ # via ipython
80
+ ptyprocess==0.7.0
81
+ # via pexpect
82
+ pure-eval==0.2.3
83
+ # via stack-data
84
+ pygments==2.18.0
85
+ # via ipython
86
+ pyright==1.1.376
87
+ # via primitive (pyproject.toml)
88
+ python-dateutil==2.9.0.post0
89
+ # via botocore
90
+ requests==2.32.3
91
+ # via
92
+ # gql
93
+ # requests-toolbelt
94
+ requests-toolbelt==1.0.0
95
+ # via gql
96
+ ruff==0.6.1
97
+ # via primitive (pyproject.toml)
98
+ six==1.16.0
99
+ # via
100
+ # asttokens
101
+ # python-dateutil
102
+ sniffio==1.3.1
103
+ # via
104
+ # anyio
105
+ # httpx
106
+ stack-data==0.6.3
107
+ # via ipython
108
+ traitlets==5.14.3
109
+ # via
110
+ # ipython
111
+ # matplotlib-inline
112
+ urllib3==2.2.2
113
+ # via
114
+ # botocore
115
+ # requests
116
+ wcwidth==0.2.13
117
+ # via prompt-toolkit
118
+ websockets==11.0.3
119
+ # via gql
120
+ yarl==1.9.4
121
+ # via
122
+ # aiohttp
123
+ # gql
@@ -0,0 +1,4 @@
1
+ # SPDX-FileCopyrightText: 2024-present Dylan Stein <dylan@steins.studio>
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ __version__ = "0.1.2"
@@ -0,0 +1,3 @@
1
+ # SPDX-FileCopyrightText: 2024-present Dylan Stein <dylan@steins.studio>
2
+ #
3
+ # SPDX-License-Identifier: MIT
@@ -0,0 +1,43 @@
1
+ from gql import gql
2
+
3
+ from ..utils.config import read_config_file, update_config_file
4
+
5
+
6
+ from primitive.utils.actions import BaseAction
7
+
8
+
9
+ class Auth(BaseAction):
10
+ def whoami(self):
11
+ query = gql(
12
+ """
13
+ query whoami {
14
+ whoami {
15
+ username
16
+ }
17
+ }
18
+ """
19
+ )
20
+
21
+ result = self.primitive.session.execute(query, get_execution_result=True)
22
+
23
+ return result
24
+
25
+ def setup_config(
26
+ self,
27
+ username: str,
28
+ token: str,
29
+ host: str = "api.primitive.tech",
30
+ transport: str = "https",
31
+ ):
32
+ full_config = read_config_file()
33
+ new_host_config = {
34
+ "username": username,
35
+ "token": token,
36
+ "transport": transport,
37
+ }
38
+
39
+ if existing_host_config := full_config.get(host, None):
40
+ full_config[host] = {**existing_host_config, **new_host_config}
41
+ else:
42
+ full_config[host] = new_host_config
43
+ update_config_file(new_config=full_config)
@@ -0,0 +1,77 @@
1
+ import os
2
+ import webbrowser
3
+ import click
4
+ from .actions import Auth
5
+ from ..utils.config import PRIMITIVE_CREDENTIALS_FILEPATH
6
+ from ..utils.printer import print_result
7
+
8
+ import typing
9
+
10
+ if typing.TYPE_CHECKING:
11
+ from ..client import Primitive
12
+
13
+
14
+ @click.group()
15
+ @click.pass_context
16
+ def cli(context):
17
+ """Authentication"""
18
+ pass
19
+
20
+
21
+ @cli.command("whoami")
22
+ @click.pass_context
23
+ def whoami_command(context):
24
+ """Whoami"""
25
+ primitive: Primitive = context.obj.get("PRIMITIVE")
26
+ result = primitive.auth.whoami()
27
+ if context.obj["DEBUG"] or context.obj["JSON"]:
28
+ message = result.data
29
+ else:
30
+ message = f"Logged in as {result.data['whoami']['username']}"
31
+ print_result(message=message, context=context)
32
+
33
+
34
+ @cli.command("config")
35
+ @click.pass_context
36
+ @click.option(
37
+ "--username",
38
+ prompt=True,
39
+ default=lambda: os.environ.get("PRIMITIVE_USER", ""),
40
+ show_default="current user",
41
+ help="Username for Primitive API",
42
+ )
43
+ @click.option(
44
+ "--transport",
45
+ required=False,
46
+ default="https",
47
+ show_default="https",
48
+ help="Transport protocol for Primitive API",
49
+ )
50
+ def config_command(context, username: str, transport: str):
51
+ """Configure the CLI"""
52
+ token = os.environ.get("PRIMITIVE_TOKEN", "")
53
+ if not token and context.obj.get("YES"):
54
+ raise click.ClickException(
55
+ "PRIMITIVE_TOKEN environment variable is required for non-interactive mode"
56
+ )
57
+ host = context.obj.get("HOST", "api.primitive.tech")
58
+ while not token:
59
+ account_settings_url = (
60
+ f"{transport}://{host.replace('api', 'app')}/account/tokens"
61
+ )
62
+ if "localhost" in host:
63
+ account_settings_url = "http://localhost:3000/account/tokens"
64
+ click.secho(
65
+ f"You can find or create a Primitive API token at {account_settings_url}",
66
+ fg="yellow",
67
+ )
68
+
69
+ webbrowser.open_new_tab(account_settings_url)
70
+ token = click.prompt(
71
+ "Please enter your Primitive API token", hide_input=True, type=str
72
+ )
73
+
74
+ auth = Auth(primitive=None)
75
+ auth.setup_config(username=username, token=token, host=host, transport=transport)
76
+ message = f"Config created at '{PRIMITIVE_CREDENTIALS_FILEPATH}' for user '{username}' on host '{host}'" # noqa
77
+ print_result(message=message, context=context, fg="green")