# SDKとしてのコマンダーCLI

## インストール <a href="#installation" id="installation"></a>

以下は、Pythonの仮想環境を使って依存関係を分離しつつ、ソースコードからコマンダーを開発・テストする方法となります。コマンダーCLIはライブラリとして利用することも、必要に応じてCLIのソースコードをカスタマイズすることも可能です。

### コマンダーリポジトリをクローンする

[こちらのGithubページ](https://github.com/Keeper-Security/Commander)からリポジトリをローカルマシンにクローンします。

```
$ git clone https://github.com/Keeper-Security/Commander
cd Commander
```

「master」ブランチは本番リリースと同期されます。「release」ブランチは今後のリリースに対応しています。任意で「release」ブランチに切り替えるには、以下を実行します。

```
git checkout release
```

### Pythonをインストールする

[python.org](https://www.python.org/)から最新のPython3インストールを使用します。

### 仮想環境を作成して有効にする

{% tabs %}
{% tab title="macOS / Linux" %}

```
python3 -m venv venv
source venv/bin/activate
```

{% endtab %}

{% tab title="Windows" %}

```
python -m venv venv
.\venv\Scripts\activate
```

{% endtab %}
{% endtabs %}

### 依存関係をインストールし、開発モードでセットアップする

```
pip install -r requirements.txt
pip install -e .
```

### オプションのメール依存関係

メールプロバイダーの完全なサポート (OAuth、SendGrid、AWS SES) を利用するには、以下のオプション依存関係をインストールします。

```
pip install -e '.[email]'
```

### セットアップ完了

コマンダーCLIを起動できるようになりました。

```
keeper shell
```

認証プロセスの詳細については「[ログイン](/keeperpam/jp/commander-cli/commander-installation-setup/logging-in.md)」ページをご参照ください。[利用できるコマンド](/keeperpam/jp/commander-cli/command-reference.md)についてもぜひご確認ください。

***

## Pythonコード <a href="#sample-python-code" id="sample-python-code"></a>

### 認証

SDKを使ってKeeper コマンダーにサインインするには、主に2つの方法があります。

#### 手動認証

この方法ではユーザー名を入力した後、コマンダーからログインプロンプトが表示されます。ユーザーが認証を完了すると、プログラムの処理が続行されます。

```python
from keepercommander.params import KeeperParams
from keepercommander import api

params = KeeperParams()
params.user = input('User email: ')
api.login(params) # Initiates login prompts
api.sync_down(params)
```

#### 自動認証

この方法では、[ログイン状態維持の構成を作成](https://github.com/Keeper-Security/gitbook-jp-secrets-manager/blob/main/commander-cli/configuration/README.md#creating-a-persistent-login-config)した際に生成された `config.json` ファイルを使用します。設定ファイルが有効であれば、プログラムはコマンダーにプロンプトなしでサインインします。設定ファイルが無効または期限切れの場合は、プログラムはログインプロンプトに戻ります。

```python
from keepercommander.__main__ import get_params_from_config

params = get_params_from_config("{{path_to_config_file}}")
```

***

### ボルト操作

Pythonスクリプト内でbash形式の[CLIコマンド](/keeperpam/jp/commander-cli/command-reference.md)を直接実行するには、`keepercommander` ライブラリの `cli` パッケージをインポートし、`do_command` 関数を使用します。

```python
from keepercommander.__main__ import get_params_from_config
from keepercommander import cli

# ログイン 
my_params = get_params_from_config("{{path_to_config_file}}")

# コマンドの実行
cli.do_command(my_params, "{{cli_command}}")
```

上記の例では、認証は `config.json` ファイルを参照する `get_params_from_config` 関数によって行われます。構成ファイルの作成方法については、「[永続的なログイン構成の作成](https://github.com/Keeper-Security/gitbook-jp-secrets-manager/blob/main/commander-cli/configuration/README.md#roguinwo)」の項目をご覧ください。

また、コマンダーSDK に含まれる内部クラスや関数を活用することで、さらに柔軟な操作が可能になります。[こちらのGinhubページ](https://github.com/Keeper-Security/Commander/tree/master/examples)で、レコードの検索、チームの作成、フォルダの共有など、スタンドアロンPythonスクリプトをご参照いただけます。

```python
from keepercommander.__main__ import get_params_from_config
from keepercommander import api

# ログイン
params = get_params_from_config("{{path_to_config_file}}")
api.login(params)
api.sync_down(params)

# 共有フォルダを検索
shared_folder_name = 'Example Shared Folder'
folder_search = api.search_shared_folders(params, shared_folder_name)

# フォルダが存在する場合完全なレコードディクショナリを取得
keeper_folder = None
if folder_search:
    keeper_folder = folder_search[0]
    for record in keeper_folder.records:
        record_obj = api.get_record(params, record["record_uid"])
        print(record_obj.to_dictionary())
else:
    print('No folder found')
```

***

### エンタープライズ利用

エンタープライズデータにアクセスする機能は、`params.enterprise` オブジェクトが設定されていない限り、データを取得しません。`params.enterprise` は `api.query_enterprise()` 関数を使って設定できます。以下のプログラムは、`audit-report` コマンドのJSON出力をPython内で直接取得する例です。

```python
from keepercommander.__main__ import get_params_from_config
from keepercommander import api
from keepercommander.commands.aram import AuditReportCommand

from json import loads

# ログイン
params = get_params_from_config("{{path_to_config_file}}")
api.login(params)
api.sync_down(params)

api.query_enterprise(params) # エンタープライズ情報を設定

# Python内でJSONレポートを直接取得
kwargs = {
    'report_type':'raw', 
    'format':'json',
    'limit':-1,
    'aggregate':['occurences']
}
command_class = AuditReportCommand()
command_run = command_class.execute(params, **kwargs)
report = loads(command_run)

```

***

コマンダーSDKの内部クラスや関数を利用することで、SDKの全機能を活用できます。[こちら](https://github.com/Keeper-Security/Commander/tree/master/examples)で、レコード検索、チーム作成、フォルダ共有などの例を含む、独立したPythonスクリプトをご確認ください。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.keeper.io/keeperpam/jp/commander-cli/commander-installation-setup/python-developer-setup/commander-cli-as-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
