fix: add openim server deploy (#1036)
* fix: add openim server deploy Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: add openim im ctl Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: add more deployment docs Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: add openim save images and release Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * build: set IBM sarama and set k8s deployment helm Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * fix: add openim code fix Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * fix: add openim code fix Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> --------- Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>test/release-v3.3
parent
de50331d7a
commit
da8912121f
@ -0,0 +1,274 @@
|
|||||||
|
# Copyright © 2023 OpenIM open source community. All rights reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
name: Build OpenIM Web Docker image
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- release-*
|
||||||
|
tags:
|
||||||
|
- v*
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
env:
|
||||||
|
# Common versions
|
||||||
|
GO_VERSION: "1.20"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-dockerhub:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v2
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
|
||||||
|
# docker.io/openim/openim-server:latest
|
||||||
|
- name: Extract metadata (tags, labels) for Docker
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@v4.6.0
|
||||||
|
with:
|
||||||
|
images: openim/openim-server
|
||||||
|
# generate Docker tags based on the following events/attributes
|
||||||
|
tags: |
|
||||||
|
type=schedule
|
||||||
|
type=ref,event=branch
|
||||||
|
type=ref,event=pr
|
||||||
|
type=semver,pattern={{version}}
|
||||||
|
type=semver,pattern={{major}}.{{minor}}
|
||||||
|
type=semver,pattern={{major}}
|
||||||
|
type=sha
|
||||||
|
|
||||||
|
- name: Log in to Docker Hub
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Build and push Docker image
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
# linux/ppc64le,linux/s390x
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
|
||||||
|
build-aliyun:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v2
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
# registry.cn-hangzhou.aliyuncs.com/openimsdk/openim-server:latest
|
||||||
|
- name: Extract metadata (tags, labels) for Docker
|
||||||
|
id: meta2
|
||||||
|
uses: docker/metadata-action@v4.6.0
|
||||||
|
with:
|
||||||
|
images: registry.cn-hangzhou.aliyuncs.com/openimsdk/openim-server
|
||||||
|
|
||||||
|
- name: Log in to AliYun Docker Hub
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
registry: registry.cn-hangzhou.aliyuncs.com
|
||||||
|
username: ${{ secrets.ALIREGISTRY_USERNAME }}
|
||||||
|
password: ${{ secrets.ALIREGISTRY_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build and push Docker image
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
# linux/ppc64le,linux/s390x
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.meta2.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta2.outputs.labels }}
|
||||||
|
|
||||||
|
build-ghcr:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v2
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
# ghcr.io/openimsdk/openim-server:latest
|
||||||
|
- name: Extract metadata (tags, labels) for Docker
|
||||||
|
id: meta3
|
||||||
|
uses: docker/metadata-action@v4.6.0
|
||||||
|
with:
|
||||||
|
images: ghcr.io/openimsdk/openim-server
|
||||||
|
|
||||||
|
- name: Log in to GitHub Container Registry
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.repository_owner }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build and push Docker image
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
# linux/ppc64le,linux/s390x
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.meta3.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta3.outputs.labels }}
|
||||||
|
|
||||||
|
build-openim-web-dockerhub:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v2
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
|
||||||
|
# docker.io/openim/openim-server:latest
|
||||||
|
- name: Extract metadata (tags, labels) for Docker
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@v4.6.0
|
||||||
|
with:
|
||||||
|
images: openim/openim-server
|
||||||
|
# generate Docker tags based on the following events/attributes
|
||||||
|
tags: |
|
||||||
|
type=schedule
|
||||||
|
type=ref,event=branch
|
||||||
|
type=ref,event=pr
|
||||||
|
type=semver,pattern={{version}}
|
||||||
|
type=semver,pattern={{major}}.{{minor}}
|
||||||
|
type=semver,pattern={{major}}
|
||||||
|
type=sha
|
||||||
|
|
||||||
|
- name: Log in to Docker Hub
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Build and push Docker image
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./build/images/openim-tools/openim-web/Dockerfile
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
|
||||||
|
build-openim-web-aliyun:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v2
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
# registry.cn-hangzhou.aliyuncs.com/openimsdk/openim-server:latest
|
||||||
|
- name: Extract metadata (tags, labels) for Docker
|
||||||
|
id: meta2
|
||||||
|
uses: docker/metadata-action@v4.6.0
|
||||||
|
with:
|
||||||
|
images: registry.cn-hangzhou.aliyuncs.com/openimsdk/openim-server
|
||||||
|
|
||||||
|
- name: Log in to AliYun Docker Hub
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
registry: registry.cn-hangzhou.aliyuncs.com
|
||||||
|
username: ${{ secrets.ALIREGISTRY_USERNAME }}
|
||||||
|
password: ${{ secrets.ALIREGISTRY_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build and push Docker image
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./build/images/openim-tools/openim-web/Dockerfile
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.meta2.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta2.outputs.labels }}
|
||||||
|
|
||||||
|
build-openim-web-ghcr:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v2
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
with:
|
||||||
|
install: true
|
||||||
|
- name: Cache Docker layers
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: /tmp/.buildx-cache
|
||||||
|
key: ${{ runner.os }}-buildx-${{ github.sha }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-buildx-
|
||||||
|
|
||||||
|
- name: Log in to GitHub Container Registry
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.repository_owner }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Extract metadata (tags, labels) for Docker openim-web
|
||||||
|
id: meta1
|
||||||
|
uses: docker/metadata-action@v4.6.0
|
||||||
|
with:
|
||||||
|
images: ghcr.io/openimsdk/openim-web
|
||||||
|
|
||||||
|
- name: Build and push Docker image for openim-web
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./build/images/openim-tools/openim-web/Dockerfile
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.meta1.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta1.outputs.labels }}
|
||||||
|
cache-from: type=local,src=/tmp/.buildx-cache
|
||||||
|
cache-to: type=local,dest=/tmp/.buildx-cache
|
||||||
|
|
||||||
|
- name: Extract metadata (tags, labels) for Docker openim-web
|
||||||
|
id: meta2
|
||||||
|
uses: docker/metadata-action@v4.6.0
|
||||||
|
with:
|
||||||
|
images: ghcr.io/openimsdk/component
|
||||||
|
|
||||||
|
- name: Build and push Docker image for component
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./build/images/openim-tools/component/Dockerfile
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.meta2.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta2.outputs.labels }}
|
||||||
|
cache-from: type=local,src=/tmp/.buildx-cache
|
||||||
|
cache-to: type=local,dest=/tmp/.buildx-cache
|
@ -0,0 +1,138 @@
|
|||||||
|
# OpenIM enviroment
|
||||||
|
|
||||||
|
|
||||||
|
## How to change the configuration
|
||||||
|
|
||||||
|
|
||||||
|
**Modify the configuration files:**
|
||||||
|
|
||||||
|
Three ways to modify the configuration:
|
||||||
|
|
||||||
|
#### **1. Recommended using environment variables:**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
export PASSWORD="openIM123" # Set password
|
||||||
|
export USER="root" # Set username
|
||||||
|
# Choose chat version and server version https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/images.md, eg: main, release-v*.*
|
||||||
|
export CHAT_BRANCH="main"
|
||||||
|
export SERVER_BRANCH="main"
|
||||||
|
#... Other environment variables
|
||||||
|
# MONGO_USERNAME: This sets the MongoDB username
|
||||||
|
# MONGO_PASSWORD: Set the MongoDB password
|
||||||
|
# MONGO_DATABASE: Sets the MongoDB database name
|
||||||
|
# MINIO_ENDPOINT: set the MinIO service address
|
||||||
|
# API_URL: under network environment, set OpenIM Server API address
|
||||||
|
export API_URL="http://127.0.0.1:10002"
|
||||||
|
```
|
||||||
|
|
||||||
|
Next, update the configuration using `make init`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
make init
|
||||||
|
```
|
||||||
|
|
||||||
|
#### **2. Modify the automation script:**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scripts/install/environment.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
Next, update the configuration using `make init`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
make init
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. Modify `config.yaml` and `.env` files (but will be overwritten when using `make init` again).
|
||||||
|
|
||||||
|
The `config/config.yaml` file has detailed configuration instructions for the storage components.
|
||||||
|
|
||||||
|
|
||||||
|
The config file is available via [environment.sh](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/scripts/install/environment.sh) configuration [openim.yaml](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/deployments/templates/openim.yaml) template, and then through the `make init` to automatically generate a new configuration.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Configuration Details
|
||||||
|
|
||||||
|
###### Zookeeper
|
||||||
|
|
||||||
|
- **Purpose**: Used for RPC service discovery and registration, cluster support.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
zookeeper:
|
||||||
|
schema: openim # Not recommended to modify
|
||||||
|
address: [ 127.0.0.1:2181 ] # Address
|
||||||
|
username: # Username
|
||||||
|
password: # Password
|
||||||
|
```
|
||||||
|
|
||||||
|
###### MySQL
|
||||||
|
|
||||||
|
- **Purpose**: Used for storing users, relationships, and groups. Supports master-slave database.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mysql:
|
||||||
|
address: [ 127.0.0.1:13306 ] # Address
|
||||||
|
username: root # Username
|
||||||
|
password: openIM123 # Password
|
||||||
|
database: openIM_v2 # Not recommended to modify
|
||||||
|
maxOpenConn: 1000 # Maximum connection
|
||||||
|
maxIdleConn: 100 # Maximum idle connection
|
||||||
|
maxLifeTime: 60 # Max time a connection can be reused (seconds)
|
||||||
|
logLevel: 4 # Log level (1=silent, 2=error, 3=warn, 4=info)
|
||||||
|
slowThreshold: 500 # Slow statement threshold (milliseconds)
|
||||||
|
```
|
||||||
|
|
||||||
|
###### Mongo
|
||||||
|
|
||||||
|
- **Purpose**: Used for storing offline messages. Supports mongo sharded clusters.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mongo:
|
||||||
|
uri: # Use this value directly if not empty
|
||||||
|
address: [ 127.0.0.1:37017 ] # Address
|
||||||
|
database: openIM # Default mongo db
|
||||||
|
username: root # Username
|
||||||
|
password: openIM123 # Password
|
||||||
|
maxPoolSize: 100 # Maximum connections
|
||||||
|
```
|
||||||
|
|
||||||
|
###### Redis
|
||||||
|
|
||||||
|
- **Purpose**: Used for storing message sequence numbers, latest messages, user tokens, and MySQL cache. Supports cluster deployment.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
redis:
|
||||||
|
address: [ 127.0.0.1:16379 ] # Address
|
||||||
|
username: # Username
|
||||||
|
password: openIM123 # Password
|
||||||
|
```
|
||||||
|
|
||||||
|
###### Kafka
|
||||||
|
|
||||||
|
- **Purpose**: Used for message queues for decoupling. Supports cluster deployment.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
kafka:
|
||||||
|
username: # Username
|
||||||
|
password: # Password
|
||||||
|
addr: [ 127.0.0.1:9092 ] # Address
|
||||||
|
topics:
|
||||||
|
latestMsgToRedis: "latestMsgToRedis"
|
||||||
|
offlineMsgToMongo: "offlineMsgToMongoMysql"
|
||||||
|
msgToPush: "msgToPush"
|
||||||
|
msgToModify: "msgToModify"
|
||||||
|
consumerGroupID:
|
||||||
|
msgToRedis: redis
|
||||||
|
msgToMongo: mongo
|
||||||
|
msgToMySql: mysql
|
||||||
|
msgToPush: push
|
||||||
|
msgToModify: modify
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Config options
|
||||||
|
|
||||||
|
...
|
@ -0,0 +1,89 @@
|
|||||||
|
# [RFC #0005] OpenIM CTL Module Proposal
|
||||||
|
|
||||||
|
## Meta
|
||||||
|
|
||||||
|
- Name: OpenIM CTL Module Enhancement
|
||||||
|
- Start Date: 2023-08-23
|
||||||
|
- Author(s): @cubxxw
|
||||||
|
- Status: Draft
|
||||||
|
- RFC Pull Request: (leave blank)
|
||||||
|
- OpenIMSDK Pull Request: (leave blank)
|
||||||
|
- OpenIMSDK Issue: https://github.com/OpenIMSDK/Open-IM-Server/issues/924
|
||||||
|
- Supersedes: N/A
|
||||||
|
|
||||||
|
## 📇Topics
|
||||||
|
|
||||||
|
- RFC #0000 OpenIMSDK CTL Module Proposal
|
||||||
|
- [Meta](#meta)
|
||||||
|
- [Summary](#summary)
|
||||||
|
- [Definitions](#definitions)
|
||||||
|
- [Motivation](#motivation)
|
||||||
|
- [What it is](#what-it-is)
|
||||||
|
- [How it Works](#how-it-works)
|
||||||
|
- [Migration](#migration)
|
||||||
|
- [Drawbacks](#drawbacks)
|
||||||
|
- [Alternatives](#alternatives)
|
||||||
|
- [Prior Art](#prior-art)
|
||||||
|
- [Unresolved Questions](#unresolved-questions)
|
||||||
|
- [Spec. Changes (OPTIONAL)](#spec-changes-optional)
|
||||||
|
- [History](#history)
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
The OpenIM CTL module proposal aims to provide an integrated tool for the OpenIM system, offering utilities for user management, system monitoring, debugging, configuration, and more. This tool will enhance the extensibility of the OpenIM system and reduce dependencies on individual modules.
|
||||||
|
|
||||||
|
## Definitions
|
||||||
|
|
||||||
|
- **OpenIM**: An Instant Messaging system.
|
||||||
|
- **`imctl`**: The control command-line tool for OpenIM.
|
||||||
|
- **E2E Testing**: End-to-End Testing.
|
||||||
|
- **API**: Application Programming Interface.
|
||||||
|
|
||||||
|
## Motivation
|
||||||
|
|
||||||
|
- Improve the OpenIM system's extensibility and reduce dependencies on individual modules.
|
||||||
|
- Simplify the process for testers to perform automated tests.
|
||||||
|
- Enhance interaction with scripts and reduce the system's coupling.
|
||||||
|
- Implement a consistent tool similar to kubectl for a streamlined user experience.
|
||||||
|
|
||||||
|
## What it is
|
||||||
|
|
||||||
|
`imctl` is a command-line utility designed for OpenIM to provide functionalities including:
|
||||||
|
|
||||||
|
- User Management: Add, delete, or disable user accounts.
|
||||||
|
- System Monitoring: View metrics like online users, message transfer rate.
|
||||||
|
- Debugging: View logs, adjust log levels, check system states.
|
||||||
|
- Configuration Management: Update system settings, manage plugins/modules.
|
||||||
|
- Data Management: Backup, restore, import, or export data.
|
||||||
|
- System Maintenance: Update, restart services, or maintenance mode.
|
||||||
|
|
||||||
|
## How it Works
|
||||||
|
|
||||||
|
`imctl`, inspired by kubectl, will have sub-commands and options for the functionalities mentioned. Developers, operations, and testers can invoke these commands to manage and monitor the OpenIM system.
|
||||||
|
|
||||||
|
## Migration
|
||||||
|
|
||||||
|
Currently, the `imctl` will be housed in `tools/imctl`, and later on, the plan is to move it to `cmd/imctl`. Migration guidelines will be provided to ensure smooth transitions.
|
||||||
|
|
||||||
|
## Drawbacks
|
||||||
|
|
||||||
|
- Overhead in learning and adapting to a new tool for existing users.
|
||||||
|
- Potential complexities in implementing some of the advanced functionalities.
|
||||||
|
|
||||||
|
## Alternatives
|
||||||
|
|
||||||
|
- Continue using individual modules for OpenIM management.
|
||||||
|
- Utilize third-party tools or platforms with similar functionalities, customizing them for OpenIM.
|
||||||
|
|
||||||
|
## Prior Art
|
||||||
|
|
||||||
|
Kubectl from Kubernetes is a significant inspiration for `imctl`, offering a comprehensive command-line tool for managing clusters.
|
||||||
|
|
||||||
|
## Unresolved Questions
|
||||||
|
|
||||||
|
- What other functionalities might be required in future versions of `imctl`?
|
||||||
|
- What's the expected timeline for transitioning from `tools/imctl` to `cmd/imctl`?
|
||||||
|
|
||||||
|
## Spec. Changes (OPTIONAL)
|
||||||
|
|
||||||
|
As of now, there are no proposed changes to the core specifications or extensions. Future changes based on community feedback might necessitate spec changes, which will be documented accordingly.
|
@ -0,0 +1,49 @@
|
|||||||
|
# OpenIM `man` Module README
|
||||||
|
|
||||||
|
Welcome to the `man` module of OpenIM, the comprehensive guide for using OpenIM's range of powerful commands. Here, you'll find in-depth details for each command, its options, and examples to help you harness the full power of the OpenIM suite.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
OpenIM is a robust instant messaging solution. To ensure users can effectively harness its capabilities, OpenIM provides a suite of commands that serve different functionalities, from the API level to RPC calls and utilities.
|
||||||
|
|
||||||
|
The `man` module ensures that users, both new and experienced, have a reliable source of information and documentation to use these commands effectively.
|
||||||
|
|
||||||
|
## Available Commands
|
||||||
|
|
||||||
|
The OpenIM commands are divided into core services and tools. Below is a brief overview of each:
|
||||||
|
|
||||||
|
### Core Services
|
||||||
|
|
||||||
|
- **openim-api**: Interface to the main functionalities of OpenIM.
|
||||||
|
- **openim-cmdutils**: Utilities for executing common tasks.
|
||||||
|
- **openim-crontask**: Schedule and manage routine tasks within OpenIM.
|
||||||
|
- **openim-msggateway**: Gateway for managing messages within the OpenIM system.
|
||||||
|
- **openim-msgtransfer**: Handle message transfers across different parts of OpenIM.
|
||||||
|
- **openim-push**: Service for pushing notifications and updates.
|
||||||
|
- **openim-rpc-auth**: RPC interface for authentication tasks.
|
||||||
|
- **openim-rpc-conversation**: RPC service for handling conversations.
|
||||||
|
- **openim-rpc-friend**: Manage friend lists and related functionalities through RPC.
|
||||||
|
- **openim-rpc-group**: Group management via RPC.
|
||||||
|
- **openim-rpc-msg**: Message handling at the RPC level.
|
||||||
|
- **openim-rpc-third**: Third-party integrations and related tasks through RPC.
|
||||||
|
- **openim-rpc-user**: User management and tasks via RPC.
|
||||||
|
|
||||||
|
### Tools
|
||||||
|
|
||||||
|
- **changelog**: Track and manage changes in OpenIM.
|
||||||
|
- **component**: Utilities related to different components within OpenIM.
|
||||||
|
- **infra**: Infrastructure and backend management tools.
|
||||||
|
- **ncpu**: Monitor and manage CPU usage and related tasks.
|
||||||
|
- **yamlfmt**: A tool for formatting and linting YAML files within the OpenIM configuration.
|
||||||
|
|
||||||
|
## How to Use
|
||||||
|
|
||||||
|
To view the manual page for any of the OpenIM commands, use the `man` command followed by the command name. For example:
|
||||||
|
|
||||||
|
```
|
||||||
|
man openim-api
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contributions
|
||||||
|
|
||||||
|
We welcome contributions to enhance the `man` pages. If you discover inconsistencies, errors, or areas where further details are required, feel free to raise an issue or submit a pull request.
|
@ -0,0 +1,62 @@
|
|||||||
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"k8s.io/kubernetes/cmd/genutils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// TODO use os.Args instead of "flags" because "flags" will mess up the man pages!
|
||||||
|
path := "docs/man/man1"
|
||||||
|
module := ""
|
||||||
|
if len(os.Args) == 3 {
|
||||||
|
path = os.Args[1]
|
||||||
|
module = os.Args[2]
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(os.Stderr, "usage: %s [output directory] [module] \n", os.Args[0])
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
outDir, err := genutils.OutDir(path)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set environment variables used by command so the output is consistent,
|
||||||
|
// regardless of where we run.
|
||||||
|
os.Setenv("HOME", "/home/username")
|
||||||
|
|
||||||
|
// openim-api
|
||||||
|
// openim-cmdutils
|
||||||
|
// openim-crontask
|
||||||
|
// openim-msggateway
|
||||||
|
// openim-msgtransfer
|
||||||
|
// openim-push
|
||||||
|
// openim-rpc-auth
|
||||||
|
// openim-rpc-conversation
|
||||||
|
// openim-rpc-friend
|
||||||
|
// openim-rpc-group
|
||||||
|
// openim-rpc-msg
|
||||||
|
// openim-rpc-third
|
||||||
|
// openim-rpc-user
|
||||||
|
switch module {
|
||||||
|
case "openim-api":
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,243 @@
|
|||||||
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package genericclioptions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/AlekSi/pointer"
|
||||||
|
"github.com/marmotedu/marmotedu-sdk-go/rest"
|
||||||
|
"github.com/marmotedu/marmotedu-sdk-go/tools/clientcmd"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Defines flag for imctl.
|
||||||
|
const (
|
||||||
|
FlagIMConfig = "imconfig"
|
||||||
|
FlagBearerToken = "user.token"
|
||||||
|
FlagUsername = "user.username"
|
||||||
|
FlagPassword = "user.password"
|
||||||
|
FlagSecretID = "user.secret-id"
|
||||||
|
FlagSecretKey = "user.secret-key"
|
||||||
|
FlagCertFile = "user.client-certificate"
|
||||||
|
FlagKeyFile = "user.client-key"
|
||||||
|
FlagTLSServerName = "server.tls-server-name"
|
||||||
|
FlagInsecure = "server.insecure-skip-tls-verify"
|
||||||
|
FlagCAFile = "server.certificate-authority"
|
||||||
|
FlagAPIServer = "server.address"
|
||||||
|
FlagTimeout = "server.timeout"
|
||||||
|
FlagMaxRetries = "server.max-retries"
|
||||||
|
FlagRetryInterval = "server.retry-interval"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RESTClientGetter is an interface that the ConfigFlags describe to provide an easier way to mock for commands
|
||||||
|
// and eliminate the direct coupling to a struct type. Users may wish to duplicate this type in their own packages
|
||||||
|
// as per the golang type overlapping.
|
||||||
|
type RESTClientGetter interface {
|
||||||
|
// ToRESTConfig returns restconfig
|
||||||
|
ToRESTConfig() (*rest.Config, error)
|
||||||
|
// ToRawIMConfigLoader return imconfig loader as-is
|
||||||
|
ToRawIMConfigLoader() clientcmd.ClientConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ RESTClientGetter = &ConfigFlags{}
|
||||||
|
|
||||||
|
// ConfigFlags composes the set of values necessary
|
||||||
|
// for obtaining a REST client config.
|
||||||
|
type ConfigFlags struct {
|
||||||
|
IMConfig *string
|
||||||
|
|
||||||
|
BearerToken *string
|
||||||
|
Username *string
|
||||||
|
Password *string
|
||||||
|
SecretID *string
|
||||||
|
SecretKey *string
|
||||||
|
|
||||||
|
Insecure *bool
|
||||||
|
TLSServerName *string
|
||||||
|
CertFile *string
|
||||||
|
KeyFile *string
|
||||||
|
CAFile *string
|
||||||
|
|
||||||
|
APIServer *string
|
||||||
|
Timeout *time.Duration
|
||||||
|
MaxRetries *int
|
||||||
|
RetryInterval *time.Duration
|
||||||
|
|
||||||
|
clientConfig clientcmd.ClientConfig
|
||||||
|
lock sync.Mutex
|
||||||
|
// If set to true, will use persistent client config and
|
||||||
|
// propagate the config to the places that need it, rather than
|
||||||
|
// loading the config multiple times
|
||||||
|
usePersistentConfig bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToRESTConfig implements RESTClientGetter.
|
||||||
|
// Returns a REST client configuration based on a provided path
|
||||||
|
// to a .imconfig file, loading rules, and config flag overrides.
|
||||||
|
// Expects the AddFlags method to have been called.
|
||||||
|
func (f *ConfigFlags) ToRESTConfig() (*rest.Config, error) {
|
||||||
|
return f.ToRawIMConfigLoader().ClientConfig()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToRawIMConfigLoader binds config flag values to config overrides
|
||||||
|
// Returns an interactive clientConfig if the password flag is enabled,
|
||||||
|
// or a non-interactive clientConfig otherwise.
|
||||||
|
func (f *ConfigFlags) ToRawIMConfigLoader() clientcmd.ClientConfig {
|
||||||
|
if f.usePersistentConfig {
|
||||||
|
return f.toRawIMPersistentConfigLoader()
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.toRawIMConfigLoader()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *ConfigFlags) toRawIMConfigLoader() clientcmd.ClientConfig {
|
||||||
|
config := clientcmd.NewConfig()
|
||||||
|
if err := viper.Unmarshal(&config); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return clientcmd.NewClientConfigFromConfig(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
// toRawIMPersistentConfigLoader binds config flag values to config overrides
|
||||||
|
// Returns a persistent clientConfig for propagation.
|
||||||
|
func (f *ConfigFlags) toRawIMPersistentConfigLoader() clientcmd.ClientConfig {
|
||||||
|
f.lock.Lock()
|
||||||
|
defer f.lock.Unlock()
|
||||||
|
|
||||||
|
if f.clientConfig == nil {
|
||||||
|
f.clientConfig = f.toRawIMConfigLoader()
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.clientConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddFlags binds client configuration flags to a given flagset.
|
||||||
|
func (f *ConfigFlags) AddFlags(flags *pflag.FlagSet) {
|
||||||
|
if f.IMConfig != nil {
|
||||||
|
flags.StringVar(f.IMConfig, FlagIMConfig, *f.IMConfig,
|
||||||
|
fmt.Sprintf("Path to the %s file to use for CLI requests", FlagIMConfig))
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.BearerToken != nil {
|
||||||
|
flags.StringVar(
|
||||||
|
f.BearerToken,
|
||||||
|
FlagBearerToken,
|
||||||
|
*f.BearerToken,
|
||||||
|
"Bearer token for authentication to the API server",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.Username != nil {
|
||||||
|
flags.StringVar(f.Username, FlagUsername, *f.Username, "Username for basic authentication to the API server")
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.Password != nil {
|
||||||
|
flags.StringVar(f.Password, FlagPassword, *f.Password, "Password for basic authentication to the API server")
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.SecretID != nil {
|
||||||
|
flags.StringVar(f.SecretID, FlagSecretID, *f.SecretID, "SecretID for JWT authentication to the API server")
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.SecretKey != nil {
|
||||||
|
flags.StringVar(f.SecretKey, FlagSecretKey, *f.SecretKey, "SecretKey for jwt authentication to the API server")
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.CertFile != nil {
|
||||||
|
flags.StringVar(f.CertFile, FlagCertFile, *f.CertFile, "Path to a client certificate file for TLS")
|
||||||
|
}
|
||||||
|
if f.KeyFile != nil {
|
||||||
|
flags.StringVar(f.KeyFile, FlagKeyFile, *f.KeyFile, "Path to a client key file for TLS")
|
||||||
|
}
|
||||||
|
if f.TLSServerName != nil {
|
||||||
|
flags.StringVar(f.TLSServerName, FlagTLSServerName, *f.TLSServerName, ""+
|
||||||
|
"Server name to use for server certificate validation. If it is not provided, the hostname used to contact the server is used")
|
||||||
|
}
|
||||||
|
if f.Insecure != nil {
|
||||||
|
flags.BoolVar(f.Insecure, FlagInsecure, *f.Insecure, ""+
|
||||||
|
"If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure")
|
||||||
|
}
|
||||||
|
if f.CAFile != nil {
|
||||||
|
flags.StringVar(f.CAFile, FlagCAFile, *f.CAFile, "Path to a cert file for the certificate authority")
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.APIServer != nil {
|
||||||
|
flags.StringVarP(f.APIServer, FlagAPIServer, "s", *f.APIServer, "The address and port of the IM API server")
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.Timeout != nil {
|
||||||
|
flags.DurationVar(
|
||||||
|
f.Timeout,
|
||||||
|
FlagTimeout,
|
||||||
|
*f.Timeout,
|
||||||
|
"The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests.",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.MaxRetries != nil {
|
||||||
|
flag.IntVar(f.MaxRetries, FlagMaxRetries, *f.MaxRetries, "Maximum number of retries.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.RetryInterval != nil {
|
||||||
|
flags.DurationVar(
|
||||||
|
f.RetryInterval,
|
||||||
|
FlagRetryInterval,
|
||||||
|
*f.RetryInterval,
|
||||||
|
"The interval time between each attempt.",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithDeprecatedPasswordFlag enables the username and password config flags.
|
||||||
|
func (f *ConfigFlags) WithDeprecatedPasswordFlag() *ConfigFlags {
|
||||||
|
f.Username = pointer.ToString("")
|
||||||
|
f.Password = pointer.ToString("")
|
||||||
|
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithDeprecatedSecretFlag enables the secretID and secretKey config flags.
|
||||||
|
func (f *ConfigFlags) WithDeprecatedSecretFlag() *ConfigFlags {
|
||||||
|
f.SecretID = pointer.ToString("")
|
||||||
|
f.SecretKey = pointer.ToString("")
|
||||||
|
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewConfigFlags returns ConfigFlags with default values set.
|
||||||
|
func NewConfigFlags(usePersistentConfig bool) *ConfigFlags {
|
||||||
|
return &ConfigFlags{
|
||||||
|
IMConfig: pointer.ToString(""),
|
||||||
|
|
||||||
|
BearerToken: pointer.ToString(""),
|
||||||
|
Insecure: pointer.ToBool(false),
|
||||||
|
TLSServerName: pointer.ToString(""),
|
||||||
|
CertFile: pointer.ToString(""),
|
||||||
|
KeyFile: pointer.ToString(""),
|
||||||
|
CAFile: pointer.ToString(""),
|
||||||
|
|
||||||
|
APIServer: pointer.ToString(""),
|
||||||
|
Timeout: pointer.ToDuration(30 * time.Second),
|
||||||
|
MaxRetries: pointer.ToInt(0),
|
||||||
|
RetryInterval: pointer.ToDuration(1 * time.Second),
|
||||||
|
usePersistentConfig: usePersistentConfig,
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// iactl is the command line tool for openim platform.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
command := cmd.NewDefaultIMCtlCommand()
|
||||||
|
if err := command.Execute(); err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
module github.com/OpenIMSDK/Open-IM-Server/tools/imctl
|
||||||
|
|
||||||
|
go 1.18
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/MakeNowJust/heredoc/v2 v2.0.1
|
||||||
|
github.com/mitchellh/go-wordwrap v1.0.1
|
||||||
|
github.com/moby/term v0.5.0
|
||||||
|
github.com/russross/blackfriday v1.6.0
|
||||||
|
github.com/spf13/cobra v1.7.0
|
||||||
|
github.com/spf13/pflag v1.0.5
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
|
golang.org/x/sys v0.1.0 // indirect
|
||||||
|
)
|
@ -0,0 +1,24 @@
|
|||||||
|
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
|
||||||
|
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
|
||||||
|
github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZYIR/J6A=
|
||||||
|
github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM=
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
|
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
|
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
|
||||||
|
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
|
||||||
|
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
|
||||||
|
github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
|
||||||
|
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
|
||||||
|
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
|
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
|
||||||
|
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
|
||||||
|
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
|
||||||
|
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
@ -0,0 +1,146 @@
|
|||||||
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
cliflag "github.com/openimsdk/component-base/pkg/cli/flag"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/cli/genericclioptions"
|
||||||
|
|
||||||
|
cmdutil "github.com/OpenIMSDK/Open-IM-Server/tools/imctl/inernal/iamctl/cmd/util"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/color"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/completion"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/info"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/jwt"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/new"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/options"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/policy"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/secret"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/set"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/user"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/validate"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/version"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/util/templates"
|
||||||
|
genericapiserver "github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/pkg/server"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/cli/genericclioptions"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewDefaultIAMCtlCommand creates the `imctl` command with default arguments.
|
||||||
|
func NewDefaultIMCtlCommand() *cobra.Command {
|
||||||
|
return NewIMCtlCommand(os.Stdin, os.Stdout, os.Stderr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewIAMCtlCommand returns new initialized instance of 'imctl' root command.
|
||||||
|
func NewIMCtlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
|
||||||
|
// Parent command to which all subcommands are added.
|
||||||
|
cmds := &cobra.Command{
|
||||||
|
Use: "imctl",
|
||||||
|
Short: "imctl controls the IM platform",
|
||||||
|
Long: templates.LongDesc(`
|
||||||
|
imctl controls the IM platform, is the client side tool for IM platform.
|
||||||
|
|
||||||
|
Find more information at:
|
||||||
|
// TODO: add link to docs, from auto scripts and gendocs
|
||||||
|
https://github.com/OpenIMSDK/Open-IM-Server/tree/main/docs`),
|
||||||
|
Run: runHelp,
|
||||||
|
// Hook before and after Run initialize and write profiles to disk,
|
||||||
|
// respectively.
|
||||||
|
PersistentPreRunE: func(*cobra.Command, []string) error {
|
||||||
|
return initProfiling()
|
||||||
|
},
|
||||||
|
PersistentPostRunE: func(*cobra.Command, []string) error {
|
||||||
|
return flushProfiling()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
flags := cmds.PersistentFlags()
|
||||||
|
flags.SetNormalizeFunc(cliflag.WarnWordSepNormalizeFunc) // Warn for "_" flags
|
||||||
|
|
||||||
|
// Normalize all flags that are coming from other packages or pre-configurations
|
||||||
|
// a.k.a. change all "_" to "-". e.g. glog package
|
||||||
|
flags.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
|
||||||
|
|
||||||
|
addProfilingFlags(flags)
|
||||||
|
|
||||||
|
iamConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag().WithDeprecatedSecretFlag()
|
||||||
|
iamConfigFlags.AddFlags(flags)
|
||||||
|
matchVersionIAMConfigFlags := cmdutil.NewMatchVersionFlags(iamConfigFlags)
|
||||||
|
matchVersionIAMConfigFlags.AddFlags(cmds.PersistentFlags())
|
||||||
|
|
||||||
|
_ = viper.BindPFlags(cmds.PersistentFlags())
|
||||||
|
cobra.OnInitialize(func() {
|
||||||
|
genericapiserver.LoadConfig(viper.GetString(genericclioptions.FlagIAMConfig), "iamctl")
|
||||||
|
})
|
||||||
|
cmds.PersistentFlags().AddGoFlagSet(flag.CommandLine)
|
||||||
|
|
||||||
|
f := cmdutil.NewFactory(matchVersionIAMConfigFlags)
|
||||||
|
|
||||||
|
// From this point and forward we get warnings on flags that contain "_" separators
|
||||||
|
cmds.SetGlobalNormalizationFunc(cliflag.WarnWordSepNormalizeFunc)
|
||||||
|
|
||||||
|
ioStreams := genericclioptions.IOStreams{In: in, Out: out, ErrOut: err}
|
||||||
|
|
||||||
|
groups := templates.CommandGroups{
|
||||||
|
{
|
||||||
|
Message: "Basic Commands:",
|
||||||
|
Commands: []*cobra.Command{
|
||||||
|
info.NewCmdInfo(f, ioStreams),
|
||||||
|
color.NewCmdColor(f, ioStreams),
|
||||||
|
new.NewCmdNew(f, ioStreams),
|
||||||
|
jwt.NewCmdJWT(f, ioStreams),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Message: "Identity and Access Management Commands:",
|
||||||
|
Commands: []*cobra.Command{
|
||||||
|
user.NewCmdUser(f, ioStreams),
|
||||||
|
secret.NewCmdSecret(f, ioStreams),
|
||||||
|
policy.NewCmdPolicy(f, ioStreams),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Message: "Troubleshooting and Debugging Commands:",
|
||||||
|
Commands: []*cobra.Command{
|
||||||
|
validate.NewCmdValidate(f, ioStreams),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Message: "Settings Commands:",
|
||||||
|
Commands: []*cobra.Command{
|
||||||
|
set.NewCmdSet(f, ioStreams),
|
||||||
|
completion.NewCmdCompletion(ioStreams.Out, ""),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
groups.Add(cmds)
|
||||||
|
|
||||||
|
filters := []string{"options"}
|
||||||
|
templates.ActsAsRootCommand(cmds, filters, groups...)
|
||||||
|
|
||||||
|
cmds.AddCommand(version.NewCmdVersion(f, ioStreams))
|
||||||
|
cmds.AddCommand(options.NewCmdOptions(ioStreams.Out))
|
||||||
|
|
||||||
|
return cmds
|
||||||
|
}
|
||||||
|
|
||||||
|
func runHelp(cmd *cobra.Command, args []string) {
|
||||||
|
_ = cmd.Help()
|
||||||
|
}
|
@ -0,0 +1,352 @@
|
|||||||
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package color print colors supported by the current terminal.
|
||||||
|
package color
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
|
"github.com/olekukonko/tablewriter"
|
||||||
|
"github.com/openim-sigs/component-base/util/stringutil"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/util/templates"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/cli/genericclioptions"
|
||||||
|
cmdutil "github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ColorOptions is an options struct to support color subcommands.
|
||||||
|
type ColorOptions struct {
|
||||||
|
Type []string
|
||||||
|
Example bool
|
||||||
|
|
||||||
|
genericclioptions.IOStreams
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
colorLong = templates.LongDesc(`Print the colors supported by the current terminal.
|
||||||
|
|
||||||
|
Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang).
|
||||||
|
It has support for Windows too! The API can be used in several ways, pick one that suits you.
|
||||||
|
|
||||||
|
Find more information at:
|
||||||
|
https://github.com/fatih/color`)
|
||||||
|
|
||||||
|
colorExample = templates.Examples(`
|
||||||
|
# Print supported foreground and background colors
|
||||||
|
imctl color
|
||||||
|
|
||||||
|
# Print supported colors by type
|
||||||
|
imctl color -t fg-hi
|
||||||
|
|
||||||
|
# Print all supported colors
|
||||||
|
imctl color -t all`)
|
||||||
|
|
||||||
|
availableTypes = []string{"fg", "fg-hi", "bg", "bg-hi", "base", "all"}
|
||||||
|
|
||||||
|
colorCodeExample = templates.Examples(`
|
||||||
|
### 1. Standard colors
|
||||||
|
|
||||||
|
// Print with default helper functions
|
||||||
|
color.Cyan("Prints text in cyan.")
|
||||||
|
|
||||||
|
// A newline will be appended automatically
|
||||||
|
color.Blue("Prints %s in blue.", "text")
|
||||||
|
|
||||||
|
// These are using the default foreground colors
|
||||||
|
color.Red("We have red")
|
||||||
|
color.Magenta("And many others ..")
|
||||||
|
|
||||||
|
### 2. Mix and reuse colors
|
||||||
|
|
||||||
|
// Create a new color object
|
||||||
|
c := color.New(color.FgCyan).Add(color.Underline)
|
||||||
|
c.Println("Prints cyan text with an underline.")
|
||||||
|
|
||||||
|
// Or just add them to New()
|
||||||
|
d := color.New(color.FgCyan, color.Bold)
|
||||||
|
d.Printf("This prints bold cyan %s\n", "too!.")
|
||||||
|
|
||||||
|
// Mix up foreground and background colors, create new mixes!
|
||||||
|
red := color.New(color.FgRed)
|
||||||
|
|
||||||
|
boldRed := red.Add(color.Bold)
|
||||||
|
boldRed.Println("This will print text in bold red.")
|
||||||
|
|
||||||
|
whiteBackground := red.Add(color.BgWhite)
|
||||||
|
whiteBackground.Println("Red text with white background.")
|
||||||
|
|
||||||
|
### 3. Use your own output (io.Writer)
|
||||||
|
|
||||||
|
// Use your own io.Writer output
|
||||||
|
color.New(color.FgBlue).Fprintln(myWriter, "blue color!")
|
||||||
|
|
||||||
|
blue := color.New(color.FgBlue)
|
||||||
|
blue.Fprint(writer, "This will print text in blue.")
|
||||||
|
|
||||||
|
### 4. Custom print functions (PrintFunc)
|
||||||
|
|
||||||
|
// Create a custom print function for convenience
|
||||||
|
red := color.New(color.FgRed).PrintfFunc()
|
||||||
|
red("Warning")
|
||||||
|
red("Error: %s", err)
|
||||||
|
|
||||||
|
// Mix up multiple attributes
|
||||||
|
notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
|
||||||
|
notice("Don't forget this...")
|
||||||
|
|
||||||
|
### 5. Custom fprint functions (FprintFunc)
|
||||||
|
|
||||||
|
blue := color.New(FgBlue).FprintfFunc()
|
||||||
|
blue(myWriter, "important notice: %s", stars)
|
||||||
|
|
||||||
|
// Mix up with multiple attributes
|
||||||
|
success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
|
||||||
|
success(myWriter, "Don't forget this...")
|
||||||
|
|
||||||
|
### 6. Insert into noncolor strings (SprintFunc)
|
||||||
|
|
||||||
|
// Create SprintXxx functions to mix strings with other non-colorized strings:
|
||||||
|
yellow := color.New(color.FgYellow).SprintFunc()
|
||||||
|
red := color.New(color.FgRed).SprintFunc()
|
||||||
|
fmt.Printf("This is a %s and this is %s.\n", yellow("warning"), red("error"))
|
||||||
|
|
||||||
|
info := color.New(color.FgWhite, color.BgGreen).SprintFunc()
|
||||||
|
fmt.Printf("This %s rocks!\n", info("package"))
|
||||||
|
|
||||||
|
// Use helper functions
|
||||||
|
fmt.Println("This", color.RedString("warning"), "should be not neglected.")
|
||||||
|
fmt.Printf("%v %v\n", color.GreenString("Info:"), "an important message.")
|
||||||
|
|
||||||
|
// Windows supported too! Just don't forget to change the output to color.Output
|
||||||
|
fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
|
||||||
|
|
||||||
|
### 7. Plug into existing code
|
||||||
|
|
||||||
|
// Use handy standard colors
|
||||||
|
color.Set(color.FgYellow)
|
||||||
|
|
||||||
|
fmt.Println("Existing text will now be in yellow")
|
||||||
|
fmt.Printf("This one %s\n", "too")
|
||||||
|
|
||||||
|
color.Unset() // Don't forget to unset
|
||||||
|
|
||||||
|
// You can mix up parameters
|
||||||
|
color.Set(color.FgMagenta, color.Bold)
|
||||||
|
defer color.Unset() // Use it in your function
|
||||||
|
|
||||||
|
fmt.Println("All text will now be bold magenta.")
|
||||||
|
|
||||||
|
### 8. Disable/Enable color
|
||||||
|
|
||||||
|
There might be a case where you want to explicitly disable/enable color output. the
|
||||||
|
go-isatty package will automatically disable color output for non-tty output streams
|
||||||
|
(for example if the output were piped directly to less)
|
||||||
|
|
||||||
|
Color has support to disable/enable colors both globally and for single color
|
||||||
|
definitions. For example suppose you have a CLI app and a --no-color bool flag. You
|
||||||
|
can easily disable the color output with:
|
||||||
|
|
||||||
|
|
||||||
|
var flagNoColor = flag.Bool("no-color", false, "Disable color output")
|
||||||
|
|
||||||
|
if *flagNoColor {
|
||||||
|
color.NoColor = true // disables colorized output
|
||||||
|
}
|
||||||
|
|
||||||
|
It also has support for single color definitions (local). You can
|
||||||
|
disable/enable color output on the fly:
|
||||||
|
|
||||||
|
c := color.New(color.FgCyan)
|
||||||
|
c.Println("Prints cyan text")
|
||||||
|
|
||||||
|
c.DisableColor()
|
||||||
|
c.Println("This is printed without any color")
|
||||||
|
|
||||||
|
c.EnableColor()
|
||||||
|
c.Println("This prints again cyan...")`)
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewColorOptions returns an initialized ColorOptions instance.
|
||||||
|
func NewColorOptions(ioStreams genericclioptions.IOStreams) *ColorOptions {
|
||||||
|
return &ColorOptions{
|
||||||
|
Type: []string{},
|
||||||
|
Example: false,
|
||||||
|
IOStreams: ioStreams,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCmdColor returns new initialized instance of color sub command.
|
||||||
|
func NewCmdColor(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
||||||
|
o := NewColorOptions(ioStreams)
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "color",
|
||||||
|
DisableFlagsInUseLine: true,
|
||||||
|
Aliases: []string{},
|
||||||
|
Short: "Print colors supported by the current terminal",
|
||||||
|
TraverseChildren: true,
|
||||||
|
Long: colorLong,
|
||||||
|
Example: colorExample,
|
||||||
|
// NoArgs, ArbitraryArgs, OnlyValidArgs, MinimumArgs, MaximumArgs, ExactArgs, ExactArgs
|
||||||
|
ValidArgs: []string{},
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
cmdutil.CheckErr(o.Complete(f, cmd, args))
|
||||||
|
cmdutil.CheckErr(o.Validate(cmd, args))
|
||||||
|
cmdutil.CheckErr(o.Run(args))
|
||||||
|
},
|
||||||
|
SuggestFor: []string{},
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.Flags().StringSliceVarP(&o.Type, "type", "t", o.Type,
|
||||||
|
fmt.Sprintf("Specify the color type, available types: `%s`.", strings.Join(availableTypes, ",")))
|
||||||
|
cmd.Flags().BoolVar(&o.Example, "example", o.Example, "Print code to show how to use color package.")
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// Complete completes all the required options.
|
||||||
|
func (o *ColorOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
|
||||||
|
if len(o.Type) == 0 {
|
||||||
|
o.Type = []string{"fg", "bg"}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if stringutil.StringIn("all", o.Type) {
|
||||||
|
o.Type = []string{"fg", "fg-hi", "bg", "bg-hi", "base"}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate makes sure there is no discrepency in command options.
|
||||||
|
func (o *ColorOptions) Validate(cmd *cobra.Command, args []string) error {
|
||||||
|
for _, t := range o.Type {
|
||||||
|
if !stringutil.StringIn(t, availableTypes) {
|
||||||
|
return cmdutil.UsageErrorf(cmd, "--type must be in: %s", strings.Join(availableTypes, ","))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes a color subcommand using the specified options.
|
||||||
|
func (o *ColorOptions) Run(args []string) error {
|
||||||
|
if o.Example {
|
||||||
|
fmt.Fprint(o.Out, colorCodeExample+"\n")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var data [][]string
|
||||||
|
|
||||||
|
// print table to stdout
|
||||||
|
table := tablewriter.NewWriter(o.Out)
|
||||||
|
|
||||||
|
// set table attributes
|
||||||
|
table.SetAutoMergeCells(true)
|
||||||
|
table.SetRowLine(false)
|
||||||
|
table.SetBorder(false)
|
||||||
|
table.SetAutoWrapText(false)
|
||||||
|
table.SetAutoFormatHeaders(true)
|
||||||
|
table.SetHeaderAlignment(tablewriter.ALIGN_CENTER)
|
||||||
|
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
||||||
|
table.SetHeader([]string{"Category", "Color Name", "Effect"})
|
||||||
|
table.SetHeaderColor(tablewriter.Colors{tablewriter.FgGreenColor},
|
||||||
|
tablewriter.Colors{tablewriter.FgRedColor},
|
||||||
|
tablewriter.Colors{tablewriter.FgCyanColor})
|
||||||
|
|
||||||
|
for _, t := range o.Type {
|
||||||
|
switch t {
|
||||||
|
// Foreground text colors
|
||||||
|
case "fg":
|
||||||
|
fg := [][]string{
|
||||||
|
{"fg", "black", color.BlackString("color.BlackString")},
|
||||||
|
{"fg", "red", color.RedString("color.RedString")},
|
||||||
|
{"fg", "green", color.GreenString("color.GreenString")},
|
||||||
|
{"fg", "yellow", color.YellowString("color.YellowString")},
|
||||||
|
{"fg", "blue", color.BlueString("color.BlueString")},
|
||||||
|
{"fg", "magenta", color.MagentaString("color.MagentaString")},
|
||||||
|
{"fg", "Cyan", color.CyanString("color.CyanString")},
|
||||||
|
{"fg", "white", color.WhiteString("color.WhiteString")},
|
||||||
|
}
|
||||||
|
data = append(data, fg...)
|
||||||
|
// Foreground Hi-Intensity text colors
|
||||||
|
case "fg-hi":
|
||||||
|
fg := [][]string{
|
||||||
|
{"fg-hi", "black", color.HiBlackString("color.HiBlackString")},
|
||||||
|
{"fg-hi", "red", color.HiRedString("color.HiRedString")},
|
||||||
|
{"fg-hi", "green", color.HiGreenString("color.HiGreenString")},
|
||||||
|
{"fg-hi", "yellow", color.HiYellowString("color.HiYellowString")},
|
||||||
|
{"fg-hi", "blue", color.HiBlueString("color.HiBlueString")},
|
||||||
|
{"fg-hi", "magenta", color.HiMagentaString("color.HiMagentaString")},
|
||||||
|
{"fg-hi", "Cyan", color.HiCyanString("color.HiCyanString")},
|
||||||
|
{"fg-hi", "white", color.HiWhiteString("color.HiWhiteString")},
|
||||||
|
}
|
||||||
|
data = append(data, fg...)
|
||||||
|
|
||||||
|
// Background text colors
|
||||||
|
case "bg":
|
||||||
|
bg := [][]string{
|
||||||
|
{"bg", "black", color.New(color.FgWhite, color.BgBlack).SprintFunc()("color.BgBlack")},
|
||||||
|
{"bg", "red", color.New(color.FgBlack, color.BgRed).SprintFunc()("color.BgRed")},
|
||||||
|
{"bg", "greep", color.New(color.FgBlack, color.BgGreen).SprintFunc()("color.BgGreen")},
|
||||||
|
{"bg", "yellow", color.New(color.FgWhite, color.BgYellow).SprintFunc()("color.BgYellow")},
|
||||||
|
{"bg", "blue", color.New(color.FgWhite, color.BgBlue).SprintFunc()("color.BgBlue")},
|
||||||
|
{"bg", "magenta", color.New(color.FgWhite, color.BgMagenta).SprintFunc()("color.BgMagenta")},
|
||||||
|
{"bg", "cyan", color.New(color.FgWhite, color.BgCyan).SprintFunc()("color.BgCyan")},
|
||||||
|
{"bg", "white", color.New(color.FgBlack, color.BgWhite).SprintFunc()("color.BgWhite")},
|
||||||
|
}
|
||||||
|
data = append(data, bg...)
|
||||||
|
// Background Hi-Intensity text colors
|
||||||
|
case "bg-hi":
|
||||||
|
bghi := [][]string{
|
||||||
|
{"bg-hi", "black", color.New(color.FgWhite, color.BgHiBlack).SprintFunc()("color.BgHiBlack")},
|
||||||
|
{"bg-hi", "red", color.New(color.FgBlack, color.BgHiRed).SprintFunc()("color.BgHiRed")},
|
||||||
|
{"bg-hi", "greep", color.New(color.FgBlack, color.BgHiGreen).SprintFunc()("color.BgHiGreen")},
|
||||||
|
{"bg-hi", "yellow", color.New(color.FgWhite, color.BgHiYellow).SprintFunc()("color.BgHiYellow")},
|
||||||
|
{"bg-hi", "blue", color.New(color.FgWhite, color.BgHiBlue).SprintFunc()("color.BgHiBlue")},
|
||||||
|
{"bg-hi", "magenta", color.New(color.FgWhite, color.BgHiMagenta).SprintFunc()("color.BgHiMagenta")},
|
||||||
|
{"bg-hi", "cyan", color.New(color.FgWhite, color.BgHiCyan).SprintFunc()("color.BgHiCyan")},
|
||||||
|
{"bg-hi", "white", color.New(color.FgBlack, color.BgHiWhite).SprintFunc()("color.BgHiWhite")},
|
||||||
|
}
|
||||||
|
data = append(data, bghi...)
|
||||||
|
// Base attributes
|
||||||
|
case "base":
|
||||||
|
base := [][]string{
|
||||||
|
{"base", "black", color.New(color.FgGreen, color.Reset).SprintFunc()("color.Reset")},
|
||||||
|
{"base", "red", color.New(color.FgGreen, color.Bold).SprintFunc()("color.Bold")},
|
||||||
|
{"base", "greep", color.New(color.FgGreen, color.Faint).SprintFunc()("color.Faint")},
|
||||||
|
{"base", "yellow", color.New(color.FgGreen, color.Italic).SprintFunc()("color.Italic")},
|
||||||
|
{"base", "blue", color.New(color.FgGreen, color.Underline).SprintFunc()("color.Underline")},
|
||||||
|
{"base", "magenta", color.New(color.FgGreen, color.BlinkSlow).SprintFunc()("color.BlinkSlow")},
|
||||||
|
{"base", "cyan", color.New(color.FgGreen, color.BlinkRapid).SprintFunc()("color.BlinkRapid")},
|
||||||
|
{"base", "white", color.New(color.FgGreen, color.ReverseVideo).SprintFunc()("color.ReverseVideo")},
|
||||||
|
{"base", "white", color.New(color.FgGreen, color.Concealed).SprintFunc()("color.Concealed")},
|
||||||
|
{"base", "white", color.New(color.FgGreen, color.CrossedOut).SprintFunc()("color.CrossedOut")},
|
||||||
|
}
|
||||||
|
data = append(data, base...)
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
table.AppendBulk(data)
|
||||||
|
table.Render()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,283 @@
|
|||||||
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package completion output shell completion code for the specified shell (bash or zsh).
|
||||||
|
package completion
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/util/templates"
|
||||||
|
cmdutil "github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultBoilerPlate = `
|
||||||
|
# Copyright © 2023 OpenIM. All rights reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
`
|
||||||
|
|
||||||
|
var (
|
||||||
|
completionLong = templates.LongDesc(`
|
||||||
|
Output shell completion code for the specified shell (bash or zsh).
|
||||||
|
The shell code must be evaluated to provide interactive
|
||||||
|
completion of imctl commands. This can be done by sourcing it from
|
||||||
|
the .bash_profile.
|
||||||
|
|
||||||
|
Detailed instructions on how to do this are available here:
|
||||||
|
http://github.com/marmotedu/iam/docs/installation/imctl.md#enabling-shell-autocompletion
|
||||||
|
|
||||||
|
Note for zsh users: [1] zsh completions are only supported in versions of zsh >= 5.2`)
|
||||||
|
|
||||||
|
completionExample = templates.Examples(`
|
||||||
|
# Installing bash completion on macOS using homebrew
|
||||||
|
## If running Bash 3.2 included with macOS
|
||||||
|
brew install bash-completion
|
||||||
|
## or, if running Bash 4.1+
|
||||||
|
brew install bash-completion@2
|
||||||
|
## If imctl is installed via homebrew, this should start working immediately.
|
||||||
|
## If you've installed via other means, you may need add the completion to your completion directory
|
||||||
|
imctl completion bash > $(brew --prefix)/etc/bash_completion.d/imctl
|
||||||
|
|
||||||
|
|
||||||
|
# Installing bash completion on Linux
|
||||||
|
## If bash-completion is not installed on Linux, please install the 'bash-completion' package
|
||||||
|
## via your distribution's package manager.
|
||||||
|
## Load the imctl completion code for bash into the current shell
|
||||||
|
source <(imctl completion bash)
|
||||||
|
## Write bash completion code to a file and source if from .bash_profile
|
||||||
|
imctl completion bash > ~/.iam/completion.bash.inc
|
||||||
|
printf "
|
||||||
|
# IAM shell completion
|
||||||
|
source '$HOME/.iam/completion.bash.inc'
|
||||||
|
" >> $HOME/.bash_profile
|
||||||
|
source $HOME/.bash_profile
|
||||||
|
|
||||||
|
# Load the imctl completion code for zsh[1] into the current shell
|
||||||
|
source <(imctl completion zsh)
|
||||||
|
# Set the imctl completion code for zsh[1] to autoload on startup
|
||||||
|
imctl completion zsh > "${fpath[1]}/_imctl"`)
|
||||||
|
)
|
||||||
|
|
||||||
|
var completionShells = map[string]func(out io.Writer, boilerPlate string, cmd *cobra.Command) error{
|
||||||
|
"bash": runCompletionBash,
|
||||||
|
"zsh": runCompletionZsh,
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCmdCompletion creates the `completion` command.
|
||||||
|
func NewCmdCompletion(out io.Writer, boilerPlate string) *cobra.Command {
|
||||||
|
shells := []string{}
|
||||||
|
for s := range completionShells {
|
||||||
|
shells = append(shells, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "completion SHELL",
|
||||||
|
DisableFlagsInUseLine: true,
|
||||||
|
Short: "Output shell completion code for the specified shell (bash or zsh)",
|
||||||
|
Long: completionLong,
|
||||||
|
Example: completionExample,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
err := RunCompletion(out, boilerPlate, cmd, args)
|
||||||
|
cmdutil.CheckErr(err)
|
||||||
|
},
|
||||||
|
ValidArgs: shells,
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunCompletion checks given arguments and executes command.
|
||||||
|
func RunCompletion(out io.Writer, boilerPlate string, cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) == 0 {
|
||||||
|
return cmdutil.UsageErrorf(cmd, "Shell not specified.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(args) > 1 {
|
||||||
|
return cmdutil.UsageErrorf(cmd, "Too many arguments. Expected only the shell type.")
|
||||||
|
}
|
||||||
|
|
||||||
|
run, found := completionShells[args[0]]
|
||||||
|
if !found {
|
||||||
|
return cmdutil.UsageErrorf(cmd, "Unsupported shell type %q.", args[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
return run(out, boilerPlate, cmd.Parent())
|
||||||
|
}
|
||||||
|
|
||||||
|
func runCompletionBash(out io.Writer, boilerPlate string, imctl *cobra.Command) error {
|
||||||
|
if len(boilerPlate) == 0 {
|
||||||
|
boilerPlate = defaultBoilerPlate
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := out.Write([]byte(boilerPlate)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return imctl.GenBashCompletion(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runCompletionZsh(out io.Writer, boilerPlate string, imctl *cobra.Command) error {
|
||||||
|
zshHead := "#compdef imctl\n"
|
||||||
|
|
||||||
|
if _, err := out.Write([]byte(zshHead)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(boilerPlate) == 0 {
|
||||||
|
boilerPlate = defaultBoilerPlate
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := out.Write([]byte(boilerPlate)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
zshInitialization := `
|
||||||
|
__imctl_bash_source() {
|
||||||
|
alias shopt=':'
|
||||||
|
emulate -L sh
|
||||||
|
setopt kshglob noshglob braceexpand
|
||||||
|
|
||||||
|
source "$@"
|
||||||
|
}
|
||||||
|
|
||||||
|
__imctl_type() {
|
||||||
|
# -t is not supported by zsh
|
||||||
|
if [ "$1" == "-t" ]; then
|
||||||
|
shift
|
||||||
|
|
||||||
|
# fake Bash 4 to disable "complete -o nospace". Instead
|
||||||
|
# "compopt +-o nospace" is used in the code to toggle trailing
|
||||||
|
# spaces. We don't support that, but leave trailing spaces on
|
||||||
|
# all the time
|
||||||
|
if [ "$1" = "__imctl_compopt" ]; then
|
||||||
|
echo builtin
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
type "$@"
|
||||||
|
}
|
||||||
|
|
||||||
|
__imctl_compgen() {
|
||||||
|
local completions w
|
||||||
|
completions=( $(compgen "$@") ) || return $?
|
||||||
|
|
||||||
|
# filter by given word as prefix
|
||||||
|
while [[ "$1" = -* && "$1" != -- ]]; do
|
||||||
|
shift
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
if [[ "$1" == -- ]]; then
|
||||||
|
shift
|
||||||
|
fi
|
||||||
|
for w in "${completions[@]}"; do
|
||||||
|
if [[ "${w}" = "$1"* ]]; then
|
||||||
|
echo "${w}"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
__imctl_compopt() {
|
||||||
|
true # don't do anything. Not supported by bashcompinit in zsh
|
||||||
|
}
|
||||||
|
|
||||||
|
__imctl_ltrim_colon_completions()
|
||||||
|
{
|
||||||
|
if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then
|
||||||
|
# Remove colon-word prefix from COMPREPLY items
|
||||||
|
local colon_word=${1%${1##*:}}
|
||||||
|
local i=${#COMPREPLY[*]}
|
||||||
|
while [[ $((--i)) -ge 0 ]]; do
|
||||||
|
COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"}
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
__imctl_get_comp_words_by_ref() {
|
||||||
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
|
prev="${COMP_WORDS[${COMP_CWORD}-1]}"
|
||||||
|
words=("${COMP_WORDS[@]}")
|
||||||
|
cword=("${COMP_CWORD[@]}")
|
||||||
|
}
|
||||||
|
|
||||||
|
__imctl_filedir() {
|
||||||
|
# Don't need to do anything here.
|
||||||
|
# Otherwise we will get trailing space without "compopt -o nospace"
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
autoload -U +X bashcompinit && bashcompinit
|
||||||
|
|
||||||
|
# use word boundary patterns for BSD or GNU sed
|
||||||
|
LWORD='[[:<:]]'
|
||||||
|
RWORD='[[:>:]]'
|
||||||
|
if sed --help 2>&1 | grep -q 'GNU\|BusyBox'; then
|
||||||
|
LWORD='\<'
|
||||||
|
RWORD='\>'
|
||||||
|
fi
|
||||||
|
|
||||||
|
__imctl_convert_bash_to_zsh() {
|
||||||
|
sed \
|
||||||
|
-e 's/declare -F/whence -w/' \
|
||||||
|
-e 's/_get_comp_words_by_ref "\$@"/_get_comp_words_by_ref "\$*"/' \
|
||||||
|
-e 's/local \([a-zA-Z0-9_]*\)=/local \1; \1=/' \
|
||||||
|
-e 's/flags+=("\(--.*\)=")/flags+=("\1"); two_word_flags+=("\1")/' \
|
||||||
|
-e 's/must_have_one_flag+=("\(--.*\)=")/must_have_one_flag+=("\1")/' \
|
||||||
|
-e "s/${LWORD}_filedir${RWORD}/__imctl_filedir/g" \
|
||||||
|
-e "s/${LWORD}_get_comp_words_by_ref${RWORD}/__imctl_get_comp_words_by_ref/g" \
|
||||||
|
-e "s/${LWORD}__ltrim_colon_completions${RWORD}/__imctl_ltrim_colon_completions/g" \
|
||||||
|
-e "s/${LWORD}compgen${RWORD}/__imctl_compgen/g" \
|
||||||
|
-e "s/${LWORD}compopt${RWORD}/__imctl_compopt/g" \
|
||||||
|
-e "s/${LWORD}declare${RWORD}/builtin declare/g" \
|
||||||
|
-e "s/\\\$(type${RWORD}/\$(__imctl_type/g" \
|
||||||
|
<<'BASH_COMPLETION_EOF'
|
||||||
|
`
|
||||||
|
if _, err := out.Write([]byte(zshInitialization)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
if err := imctl.GenBashCompletion(buf); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := out.Write(buf.Bytes()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
zshTail := `
|
||||||
|
BASH_COMPLETION_EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
__imctl_bash_source <(__imctl_convert_bash_to_zsh)
|
||||||
|
`
|
||||||
|
if _, err := out.Write([]byte(zshTail)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,119 @@
|
|||||||
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package info print the host information.
|
||||||
|
package info
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
hoststat "github.com/likexian/host-stat-go"
|
||||||
|
"github.com/openim-sigs/component-base/util/iputil"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/util/templates"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/cli/genericclioptions"
|
||||||
|
cmdutil "github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Info defines the host information struct.
|
||||||
|
type Info struct {
|
||||||
|
HostName string
|
||||||
|
IPAddress string
|
||||||
|
OSRelease string
|
||||||
|
CPUCore uint64
|
||||||
|
MemTotal string
|
||||||
|
MemFree string
|
||||||
|
}
|
||||||
|
|
||||||
|
// InfoOptions is an options struct to support 'info' sub command.
|
||||||
|
type InfoOptions struct {
|
||||||
|
genericclioptions.IOStreams
|
||||||
|
}
|
||||||
|
|
||||||
|
var infoExample = templates.Examples(`
|
||||||
|
# Print the host information
|
||||||
|
imctl info`)
|
||||||
|
|
||||||
|
// NewInfoOptions returns an initialized InfoOptions instance.
|
||||||
|
func NewInfoOptions(ioStreams genericclioptions.IOStreams) *InfoOptions {
|
||||||
|
return &InfoOptions{
|
||||||
|
IOStreams: ioStreams,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCmdInfo returns new initialized instance of 'info' sub command.
|
||||||
|
func NewCmdInfo(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
||||||
|
o := NewInfoOptions(ioStreams)
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "info",
|
||||||
|
DisableFlagsInUseLine: true,
|
||||||
|
Aliases: []string{},
|
||||||
|
Short: "Print the host information",
|
||||||
|
Long: "Print the host information.",
|
||||||
|
Example: infoExample,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
cmdutil.CheckErr(o.Run(args))
|
||||||
|
},
|
||||||
|
SuggestFor: []string{},
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes an info sub command using the specified options.
|
||||||
|
func (o *InfoOptions) Run(args []string) error {
|
||||||
|
var info Info
|
||||||
|
|
||||||
|
hostInfo, err := hoststat.GetHostInfo()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("get host info failed!error:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
info.HostName = hostInfo.HostName
|
||||||
|
info.OSRelease = hostInfo.Release + " " + hostInfo.OSBit
|
||||||
|
|
||||||
|
memStat, err := hoststat.GetMemStat()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("get mem stat failed!error:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
info.MemTotal = strconv.FormatUint(memStat.MemTotal, 10) + "M"
|
||||||
|
info.MemFree = strconv.FormatUint(memStat.MemFree, 10) + "M"
|
||||||
|
info.IPAddress = iputil.GetLocalIP()
|
||||||
|
|
||||||
|
cpuStat, err := hoststat.GetCPUInfo()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("get cpu stat failed!error:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
info.CPUCore = cpuStat.CoreCount
|
||||||
|
|
||||||
|
s := reflect.ValueOf(&info).Elem()
|
||||||
|
typeOfInfo := s.Type()
|
||||||
|
|
||||||
|
for i := 0; i < s.NumField(); i++ {
|
||||||
|
f := s.Field(i)
|
||||||
|
|
||||||
|
v := fmt.Sprintf("%v", f.Interface())
|
||||||
|
if v != "" {
|
||||||
|
fmt.Fprintf(o.Out, "%12s %v\n", typeOfInfo.Field(i).Name+":", f.Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,619 @@
|
|||||||
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package new used to generate demo command code.
|
||||||
|
// nolint: predeclared
|
||||||
|
package new
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/openim-sigs/component-base/pkg/util/fileutil"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"golang.org/x/text/cases"
|
||||||
|
"golang.org/x/text/language"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/util/templates"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/cli/genericclioptions"
|
||||||
|
cmdutil "github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
newUsageStr = "new CMD_NAME | CMD_NAME CMD_DESCRIPTION"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
newLong = templates.LongDesc(`Used to generate demo command source code file.
|
||||||
|
|
||||||
|
Can use this command generate a command template file, and do some modify based on your needs.
|
||||||
|
This can improve your R&D efficiency.`)
|
||||||
|
|
||||||
|
newExample = templates.Examples(`
|
||||||
|
# Create a default 'test' command file without a description
|
||||||
|
imctl new test
|
||||||
|
|
||||||
|
# Create a default 'test' command file in /tmp/
|
||||||
|
imctl new test -d /tmp/
|
||||||
|
|
||||||
|
# Create a default 'test' command file with a description
|
||||||
|
imctl new test "This is a test command"
|
||||||
|
|
||||||
|
# Create command 'test' with two subcommands
|
||||||
|
imctl new -g test "This is a test command with two subcommands"`)
|
||||||
|
|
||||||
|
newUsageErrStr = fmt.Sprintf(
|
||||||
|
"expected '%s'.\nat least CMD_NAME is a required argument for the new command",
|
||||||
|
newUsageStr,
|
||||||
|
)
|
||||||
|
|
||||||
|
cmdTemplate = `package {{.CommandName}}
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
cmdutil "github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/util"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/util/templates"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/cli/genericclioptions"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
{{.CommandName}}UsageStr = "{{.CommandName}} USERNAME PASSWORD"
|
||||||
|
maxStringLength = 17
|
||||||
|
)
|
||||||
|
|
||||||
|
// {{.CommandFunctionName}}Options is an options struct to support '{{.CommandName}}' sub command.
|
||||||
|
type {{.CommandFunctionName}}Options struct {
|
||||||
|
// options
|
||||||
|
StringOption string
|
||||||
|
StringSliceOption []string
|
||||||
|
IntOption int
|
||||||
|
BoolOption bool
|
||||||
|
|
||||||
|
// args
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
|
||||||
|
genericclioptions.IOStreams
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
{{.CommandName}}Long = templates.LongDesc({{.Dot}}A longer description that spans multiple lines and likely contains examples
|
||||||
|
and usage of using your command. For example:
|
||||||
|
|
||||||
|
Cobra is a CLI library for Go that empowers applications.
|
||||||
|
This application is a tool to generate the needed files
|
||||||
|
to quickly create a Cobra application.{{.Dot}})
|
||||||
|
|
||||||
|
{{.CommandName}}Example = templates.Examples({{.Dot}}
|
||||||
|
# Print all option values for {{.CommandName}}
|
||||||
|
imctl {{.CommandName}} marmotedu marmotedupass{{.Dot}})
|
||||||
|
|
||||||
|
{{.CommandName}}UsageErrStr = fmt.Sprintf("expected '%s'.\nUSERNAME and PASSWORD are required arguments for the {{.CommandName}} command", {{.CommandName}}UsageStr)
|
||||||
|
)
|
||||||
|
|
||||||
|
// New{{.CommandFunctionName}}Options returns an initialized {{.CommandFunctionName}}Options instance.
|
||||||
|
func New{{.CommandFunctionName}}Options(ioStreams genericclioptions.IOStreams) *{{.CommandFunctionName}}Options {
|
||||||
|
return &{{.CommandFunctionName}}Options{
|
||||||
|
StringOption: "default",
|
||||||
|
IOStreams: ioStreams,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCmd{{.CommandFunctionName}} returns new initialized instance of '{{.CommandName}}' sub command.
|
||||||
|
func NewCmd{{.CommandFunctionName}}(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
||||||
|
o := New{{.CommandFunctionName}}Options(ioStreams)
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: {{.CommandName}}UsageStr,
|
||||||
|
DisableFlagsInUseLine: true,
|
||||||
|
Aliases: []string{},
|
||||||
|
Short: "{{.CommandDescription}}",
|
||||||
|
TraverseChildren: true,
|
||||||
|
Long: {{.CommandName}}Long,
|
||||||
|
Example: {{.CommandName}}Example,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
cmdutil.CheckErr(o.Complete(f, cmd, args))
|
||||||
|
cmdutil.CheckErr(o.Validate(cmd, args))
|
||||||
|
cmdutil.CheckErr(o.Run(args))
|
||||||
|
},
|
||||||
|
SuggestFor: []string{},
|
||||||
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
|
// nolint: gomnd // no need
|
||||||
|
if len(args) < 2 {
|
||||||
|
return cmdutil.UsageErrorf(cmd, {{.CommandName}}UsageErrStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// if need args equal to zero, uncomment the following code
|
||||||
|
/*
|
||||||
|
if len(args) != 0 {
|
||||||
|
return cmdutil.UsageErrorf(cmd, "Unexpected args: %v", args)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// mark flag as deprecated
|
||||||
|
_ = cmd.Flags().MarkDeprecated("deprecated-opt", "This flag is deprecated and will be removed in future.")
|
||||||
|
cmd.Flags().StringVarP(&o.StringOption, "string", "", o.StringOption, "String option.")
|
||||||
|
cmd.Flags().StringSliceVar(&o.StringSliceOption, "slice", o.StringSliceOption, "String slice option.")
|
||||||
|
cmd.Flags().IntVarP(&o.IntOption, "int", "i", o.IntOption, "Int option.")
|
||||||
|
cmd.Flags().BoolVarP(&o.BoolOption, "bool", "b", o.BoolOption, "Bool option.")
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// Complete completes all the required options.
|
||||||
|
func (o *{{.CommandFunctionName}}Options) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
|
||||||
|
if o.StringOption != "" {
|
||||||
|
o.StringOption += "(complete)"
|
||||||
|
}
|
||||||
|
|
||||||
|
o.Username = args[0]
|
||||||
|
o.Password = args[1]
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate makes sure there is no discrepency in command options.
|
||||||
|
func (o *{{.CommandFunctionName}}Options) Validate(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(o.StringOption) > maxStringLength {
|
||||||
|
return cmdutil.UsageErrorf(cmd, "--string length must less than 18")
|
||||||
|
}
|
||||||
|
|
||||||
|
if o.IntOption < 0 {
|
||||||
|
return cmdutil.UsageErrorf(cmd, "--int must be a positive integer: %v", o.IntOption)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes a {{.CommandName}} sub command using the specified options.
|
||||||
|
func (o *{{.CommandFunctionName}}Options) Run(args []string) error {
|
||||||
|
fmt.Fprintf(o.Out, "The following is option values:\n")
|
||||||
|
fmt.Fprintf(o.Out, "==> --string: %v\n==> --slice: %v\n==> --int: %v\n==> --bool: %v\n",
|
||||||
|
o.StringOption, o.StringSliceOption, o.IntOption, o.BoolOption)
|
||||||
|
|
||||||
|
fmt.Fprintf(o.Out, "\nThe following is args values:\n")
|
||||||
|
fmt.Fprintf(o.Out, "==> username: %v\n==> password: %v\n", o.Username, o.Password)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
maincmdTemplate = `package {{.CommandName}}
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
|
cmdutil "github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/util"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/util/templates"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/cli/genericclioptions"
|
||||||
|
)
|
||||||
|
|
||||||
|
const maxStringLength = 17
|
||||||
|
|
||||||
|
var (
|
||||||
|
{{.CommandName}}Long = templates.LongDesc({{.Dot}}
|
||||||
|
Demo command.
|
||||||
|
|
||||||
|
This commands show you how to implement a command with two sub commands.{{.Dot}})
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewCmd{{.CommandFunctionName}} returns new initialized instance of '{{.CommandName}}' sub command.
|
||||||
|
func NewCmd{{.CommandFunctionName}}(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "{{.CommandName}} SUBCOMMAND",
|
||||||
|
DisableFlagsInUseLine: true,
|
||||||
|
Short: "{{.CommandDescription}}",
|
||||||
|
Long: {{.CommandName}}Long,
|
||||||
|
Run: cmdutil.DefaultSubCommandRun(ioStreams.ErrOut),
|
||||||
|
}
|
||||||
|
|
||||||
|
// add subcommands
|
||||||
|
cmd.AddCommand(NewCmdSubCmd1(f, ioStreams))
|
||||||
|
cmd.AddCommand(NewCmdSubCmd2(f, ioStreams))
|
||||||
|
|
||||||
|
// add persistent flags for '{{.CommandName}}'
|
||||||
|
cmdutil.AddCleanFlags(cmd)
|
||||||
|
|
||||||
|
// persistent flag, we can get the value in subcommand via {{.Dot}}viper.Get{{.Dot}}
|
||||||
|
cmd.PersistentFlags().StringP("persistent", "p", "this is a persistent option", "Cobra persistent option.")
|
||||||
|
|
||||||
|
// bind flags with viper
|
||||||
|
viper.BindPFlag("persistent", cmd.PersistentFlags().Lookup("persistent"))
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
`
|
||||||
|
subcmd1Template = `package {{.CommandName}}
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
|
cmdutil "github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/util"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/util/templates"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/cli/genericclioptions"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
subcmd1UsageStr = "subcmd1 USERNAME PASSWORD"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SubCmd1Options is an options struct to support subcmd1 subcommands.
|
||||||
|
type SubCmd1Options struct {
|
||||||
|
// options
|
||||||
|
StringOption string
|
||||||
|
StringSliceOption []string
|
||||||
|
IntOption int
|
||||||
|
BoolOption bool
|
||||||
|
PersistentOption string
|
||||||
|
|
||||||
|
// args
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
|
||||||
|
genericclioptions.IOStreams
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
subcmd1Long = templates.LongDesc({{.Dot}}A longer description that spans multiple lines and likely contains examples
|
||||||
|
and usage of using your command. For example:
|
||||||
|
|
||||||
|
Cobra is a CLI library for Go that empowers applications.
|
||||||
|
This application is a tool to generate the needed files
|
||||||
|
to quickly create a Cobra application.{{.Dot}})
|
||||||
|
|
||||||
|
subcmd1Example = templates.Examples({{.Dot}}
|
||||||
|
# Print all option values for subcmd1
|
||||||
|
imctl {{.CommandName}} subcmd1 marmotedu marmotedupass
|
||||||
|
|
||||||
|
# Print all option values for subcmd1 with --persistent specified
|
||||||
|
imctl {{.CommandName}} subcmd1 marmotedu marmotedupass --persistent="specified persistent option in command line"{{.Dot}})
|
||||||
|
|
||||||
|
subcmd1UsageErrStr = fmt.Sprintf("expected '%s'.\nUSERNAME and PASSWORD are required arguments for the subcmd1 command", subcmd1UsageStr)
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewSubCmd1Options returns an initialized SubCmd1Options instance.
|
||||||
|
func NewSubCmd1Options(ioStreams genericclioptions.IOStreams) *SubCmd1Options {
|
||||||
|
return &SubCmd1Options{
|
||||||
|
StringOption: "default",
|
||||||
|
IOStreams: ioStreams,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCmdSubCmd1 returns new initialized instance of subcmd1 sub command.
|
||||||
|
func NewCmdSubCmd1(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
||||||
|
o := NewSubCmd1Options(ioStreams)
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "subcmd1 USERNAME PASSWORD",
|
||||||
|
DisableFlagsInUseLine: true,
|
||||||
|
Aliases: []string{"sub1"},
|
||||||
|
Short: "A brief description of your command",
|
||||||
|
TraverseChildren: true,
|
||||||
|
Long: subcmd1Long,
|
||||||
|
Example: subcmd1Example,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
cmdutil.CheckErr(o.Complete(f, cmd, args))
|
||||||
|
cmdutil.CheckErr(o.Validate(cmd, args))
|
||||||
|
cmdutil.CheckErr(o.Run(args))
|
||||||
|
},
|
||||||
|
SuggestFor: []string{},
|
||||||
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
|
// nolint: gomnd // no need
|
||||||
|
if len(args) < 2 {
|
||||||
|
return cmdutil.UsageErrorf(cmd, subcmd1UsageErrStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// mark flag as deprecated
|
||||||
|
_ = cmd.Flags().MarkDeprecated("deprecated-opt", "This flag is deprecated and will be removed in future.")
|
||||||
|
cmd.Flags().StringVarP(&o.StringOption, "string", "", o.StringOption, "String option.")
|
||||||
|
cmd.Flags().StringSliceVar(&o.StringSliceOption, "slice", o.StringSliceOption, "String slice option.")
|
||||||
|
cmd.Flags().IntVarP(&o.IntOption, "int", "i", o.IntOption, "Int option.")
|
||||||
|
cmd.Flags().BoolVarP(&o.BoolOption, "bool", "b", o.BoolOption, "Bool option.")
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// Complete completes all the required options.
|
||||||
|
func (o *SubCmd1Options) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
|
||||||
|
if o.StringOption != "" {
|
||||||
|
o.StringOption += "(complete)"
|
||||||
|
}
|
||||||
|
|
||||||
|
o.PersistentOption = viper.GetString("persistent")
|
||||||
|
o.Username = args[0]
|
||||||
|
o.Password = args[1]
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate makes sure there is no discrepency in command options.
|
||||||
|
func (o *SubCmd1Options) Validate(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(o.StringOption) > maxStringLength {
|
||||||
|
return cmdutil.UsageErrorf(cmd, "--string length must less than 18")
|
||||||
|
}
|
||||||
|
|
||||||
|
if o.IntOption < 0 {
|
||||||
|
return cmdutil.UsageErrorf(cmd, "--int must be a positive integer: %v", o.IntOption)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes a subcmd1 subcommand using the specified options.
|
||||||
|
func (o *SubCmd1Options) Run(args []string) error {
|
||||||
|
fmt.Fprintf(o.Out, "The following is option values:\n")
|
||||||
|
fmt.Fprintf(o.Out, "==> --string: %v\n==> --slice: %v\n==> --int: %v\n==> --bool: %v\n==> --persistent: %v\n",
|
||||||
|
o.StringOption, o.StringSliceOption, o.IntOption, o.BoolOption, o.PersistentOption)
|
||||||
|
|
||||||
|
fmt.Fprintf(o.Out, "\nThe following is args values:\n")
|
||||||
|
fmt.Fprintf(o.Out, "==> username: %v\n==> password: %v\n", o.Username, o.Password)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
`
|
||||||
|
subcmd2Template = `package {{.CommandName}}
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
cmdutil "github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/util"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/util/templates"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/cli/genericclioptions"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SubCmd2Options is an options struct to support subcmd2 subcommands.
|
||||||
|
type SubCmd2Options struct {
|
||||||
|
StringOption string
|
||||||
|
StringSliceOption []string
|
||||||
|
IntOption int
|
||||||
|
BoolOption bool
|
||||||
|
|
||||||
|
genericclioptions.IOStreams
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
subcmd2Long = templates.LongDesc({{.Dot}}A longer description that spans multiple lines and likely contains examples
|
||||||
|
and usage of using your command. For example:
|
||||||
|
|
||||||
|
Cobra is a CLI library for Go that empowers applications.
|
||||||
|
This application is a tool to generate the needed files
|
||||||
|
to quickly create a Cobra application.{{.Dot}})
|
||||||
|
|
||||||
|
subcmd2Example = templates.Examples({{.Dot}}
|
||||||
|
# Print all option values for subcmd2
|
||||||
|
imctl {{.CommandName}} subcmd2{{.Dot}})
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewSubCmd2Options returns an initialized SubCmd2Options instance.
|
||||||
|
func NewSubCmd2Options(ioStreams genericclioptions.IOStreams) *SubCmd2Options {
|
||||||
|
return &SubCmd2Options{
|
||||||
|
StringOption: "default",
|
||||||
|
IOStreams: ioStreams,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCmdSubCmd2 returns new initialized instance of subcmd2 sub command.
|
||||||
|
func NewCmdSubCmd2(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
||||||
|
o := NewSubCmd2Options(ioStreams)
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "subcmd2",
|
||||||
|
DisableFlagsInUseLine: true,
|
||||||
|
Aliases: []string{"sub2"},
|
||||||
|
Short: "A brief description of your command",
|
||||||
|
TraverseChildren: true,
|
||||||
|
Long: subcmd2Long,
|
||||||
|
Example: subcmd2Example,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
cmdutil.CheckErr(o.Complete(f, cmd, args))
|
||||||
|
cmdutil.CheckErr(o.Validate(cmd, args))
|
||||||
|
cmdutil.CheckErr(o.Run(args))
|
||||||
|
},
|
||||||
|
SuggestFor: []string{},
|
||||||
|
}
|
||||||
|
|
||||||
|
// mark flag as deprecated
|
||||||
|
cmd.Flags().StringVarP(&o.StringOption, "string", "", o.StringOption, "String option.")
|
||||||
|
cmd.Flags().StringSliceVar(&o.StringSliceOption, "slice", o.StringSliceOption, "String slice option.")
|
||||||
|
cmd.Flags().IntVarP(&o.IntOption, "int", "i", o.IntOption, "Int option.")
|
||||||
|
cmd.Flags().BoolVarP(&o.BoolOption, "bool", "b", o.BoolOption, "Bool option.")
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// Complete completes all the required options.
|
||||||
|
func (o *SubCmd2Options) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) != 0 {
|
||||||
|
return cmdutil.UsageErrorf(cmd, "Unexpected args: %v", args)
|
||||||
|
}
|
||||||
|
|
||||||
|
if o.StringOption != "" {
|
||||||
|
o.StringOption += "(complete)"
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate makes sure there is no discrepency in command options.
|
||||||
|
func (o *SubCmd2Options) Validate(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(o.StringOption) > maxStringLength {
|
||||||
|
return cmdutil.UsageErrorf(cmd, "--string length must less than 18")
|
||||||
|
}
|
||||||
|
|
||||||
|
if o.IntOption < 0 {
|
||||||
|
return cmdutil.UsageErrorf(cmd, "--int must be a positive integer: %v", o.IntOption)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes a subcmd2 subcommand using the specified options.
|
||||||
|
func (o *SubCmd2Options) Run(args []string) error {
|
||||||
|
fmt.Fprintf(o.Out, "The following is option values:\n")
|
||||||
|
fmt.Fprintf(o.Out, "==> --string: %v\n==> --slice: %v\n==> --int: %v\n==> --bool: %v\n",
|
||||||
|
o.StringOption, o.StringSliceOption, o.IntOption, o.BoolOption)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewOptions is an options struct to support 'new' sub command.
|
||||||
|
type NewOptions struct {
|
||||||
|
Group bool
|
||||||
|
Outdir string
|
||||||
|
|
||||||
|
// command template options, will render to command template
|
||||||
|
CommandName string
|
||||||
|
CommandDescription string
|
||||||
|
CommandFunctionName string
|
||||||
|
Dot string
|
||||||
|
|
||||||
|
genericclioptions.IOStreams
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewNewOptions returns an initialized NewOptions instance.
|
||||||
|
func NewNewOptions(ioStreams genericclioptions.IOStreams) *NewOptions {
|
||||||
|
return &NewOptions{
|
||||||
|
Group: false,
|
||||||
|
Outdir: ".",
|
||||||
|
CommandDescription: "A brief description of your command",
|
||||||
|
Dot: "`",
|
||||||
|
IOStreams: ioStreams,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCmdNew returns new initialized instance of 'new' sub command.
|
||||||
|
func NewCmdNew(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
||||||
|
o := NewNewOptions(ioStreams)
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: newUsageStr,
|
||||||
|
DisableFlagsInUseLine: true,
|
||||||
|
Short: "Generate demo command code",
|
||||||
|
Long: newLong,
|
||||||
|
Example: newExample,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
cmdutil.CheckErr(o.Complete(cmd, args))
|
||||||
|
cmdutil.CheckErr(o.Validate(cmd))
|
||||||
|
cmdutil.CheckErr(o.Run(args))
|
||||||
|
},
|
||||||
|
Aliases: []string{},
|
||||||
|
SuggestFor: []string{},
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.Flags().BoolVarP(&o.Group, "group", "g", o.Group, "Generate two subcommands.")
|
||||||
|
cmd.Flags().StringVarP(&o.Outdir, "outdir", "d", o.Outdir, "Where to create demo command files.")
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// Complete completes all the required options.
|
||||||
|
func (o *NewOptions) Complete(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) < 1 {
|
||||||
|
return cmdutil.UsageErrorf(cmd, newUsageErrStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
o.CommandName = strings.ToLower(args[0])
|
||||||
|
if len(args) > 1 {
|
||||||
|
o.CommandDescription = args[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
o.CommandFunctionName = cases.Title(language.English).String(o.CommandName)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate makes sure there is no discrepency in command options.
|
||||||
|
func (o *NewOptions) Validate(cmd *cobra.Command) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes a new sub command using the specified options.
|
||||||
|
func (o *NewOptions) Run(args []string) error {
|
||||||
|
if o.Group {
|
||||||
|
return o.CreateCommandWithSubCommands()
|
||||||
|
}
|
||||||
|
|
||||||
|
return o.CreateCommand()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateCommand create the command with options.
|
||||||
|
func (o *NewOptions) CreateCommand() error {
|
||||||
|
return o.GenerateGoCode(o.CommandName+".go", cmdTemplate)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateCommandWithSubCommands create sub commands with options.
|
||||||
|
func (o *NewOptions) CreateCommandWithSubCommands() error {
|
||||||
|
if err := o.GenerateGoCode(o.CommandName+".go", maincmdTemplate); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := o.GenerateGoCode(o.CommandName+"_subcmd1.go", subcmd1Template); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := o.GenerateGoCode(o.CommandName+"_subcmd2.go", subcmd2Template); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateGoCode generate go source file.
|
||||||
|
func (o *NewOptions) GenerateGoCode(name, codeTemplate string) error {
|
||||||
|
tmpl, err := template.New("cmd").Parse(codeTemplate)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fileutil.EnsureDirAll(o.Outdir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := filepath.Join(o.Outdir, name)
|
||||||
|
fd, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer fd.Close()
|
||||||
|
|
||||||
|
err = tmpl.Execute(fd, o)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Command file generated: %s\n", filename)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package options print a list of global command-line options (applies to all commands).
|
||||||
|
package options
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/util/templates"
|
||||||
|
)
|
||||||
|
|
||||||
|
var optionsExample = templates.Examples(`
|
||||||
|
# Print flags inherited by all commands
|
||||||
|
iamctl options`)
|
||||||
|
|
||||||
|
// NewCmdOptions implements the options command.
|
||||||
|
func NewCmdOptions(out io.Writer) *cobra.Command {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "options",
|
||||||
|
Short: "Print the list of flags inherited by all commands",
|
||||||
|
Long: "Print the list of flags inherited by all commands",
|
||||||
|
Example: optionsExample,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
_ = cmd.Usage()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// The `options` command needs write its output to the `out` stream
|
||||||
|
// (typically stdout). Without calling SetOutput here, the Usage()
|
||||||
|
// function call will fall back to stderr.
|
||||||
|
cmd.SetOutput(out)
|
||||||
|
|
||||||
|
templates.UseOptionsTemplates(cmd)
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
@ -0,0 +1,95 @@
|
|||||||
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"runtime/pprof"
|
||||||
|
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
)
|
||||||
|
|
||||||
|
// profiling configuration variables
|
||||||
|
var (
|
||||||
|
profileName string = "none" // Name of the profile to capture.
|
||||||
|
profileOutput string = "profile.pprof" // File to write the profile data.
|
||||||
|
)
|
||||||
|
|
||||||
|
// addProfilingFlags registers profiling related flags to the given FlagSet.
|
||||||
|
func addProfilingFlags(flags *pflag.FlagSet) {
|
||||||
|
flags.StringVar(
|
||||||
|
&profileName,
|
||||||
|
"profile",
|
||||||
|
"none",
|
||||||
|
"Type of profile to capture. Options: none, cpu, heap, goroutine, threadcreate, block, mutex",
|
||||||
|
)
|
||||||
|
flags.StringVar(&profileOutput, "profile-output", "profile.pprof", "File to write the profile data")
|
||||||
|
}
|
||||||
|
|
||||||
|
// initProfiling sets up profiling based on the user's choice.
|
||||||
|
// If 'cpu' is selected, it starts the CPU profile. For block and mutex profiles,
|
||||||
|
// sampling rates are set up.
|
||||||
|
func initProfiling() error {
|
||||||
|
switch profileName {
|
||||||
|
case "none":
|
||||||
|
return nil
|
||||||
|
case "cpu":
|
||||||
|
f, err := os.Create(profileOutput)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return pprof.StartCPUProfile(f)
|
||||||
|
case "block":
|
||||||
|
runtime.SetBlockProfileRate(1) // Sampling every block event
|
||||||
|
return nil
|
||||||
|
case "mutex":
|
||||||
|
runtime.SetMutexProfileFraction(1) // Sampling every mutex event
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
if profile := pprof.Lookup(profileName); profile == nil {
|
||||||
|
return fmt.Errorf("unknown profile type: '%s'", profileName)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// flushProfiling writes the profiling data to the specified file.
|
||||||
|
// For heap profiles, it runs the GC before capturing the data.
|
||||||
|
// It stops the CPU profile if it was started.
|
||||||
|
func flushProfiling() error {
|
||||||
|
switch profileName {
|
||||||
|
case "none":
|
||||||
|
return nil
|
||||||
|
case "cpu":
|
||||||
|
pprof.StopCPUProfile()
|
||||||
|
return nil
|
||||||
|
case "heap":
|
||||||
|
runtime.GC() // Run garbage collection before writing heap profile
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
profile := pprof.Lookup(profileName)
|
||||||
|
if profile == nil {
|
||||||
|
return errors.New("invalid profile type")
|
||||||
|
}
|
||||||
|
f, err := os.Create(profileOutput)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return profile.WriteTo(f, 0)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,165 @@
|
|||||||
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package version print the client and server version information.
|
||||||
|
package version
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/ghodss/yaml"
|
||||||
|
"github.com/openim-sigs/component-base/pkg/json"
|
||||||
|
"github.com/openim-sigs/component-base/pkg/version"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/util/templates"
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/cli/genericclioptions"
|
||||||
|
cmdutil "github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Version is a struct for version information.
|
||||||
|
type Version struct {
|
||||||
|
ClientVersion *version.Info `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"`
|
||||||
|
ServerVersion *version.Info `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var versionExample = templates.Examples(`
|
||||||
|
# Print the client and server versions for the current context
|
||||||
|
imctl version`)
|
||||||
|
|
||||||
|
// Options is a struct to support version command.
|
||||||
|
type Options struct {
|
||||||
|
ClientOnly bool
|
||||||
|
Short bool
|
||||||
|
Output string
|
||||||
|
|
||||||
|
client *restclient.RESTClient
|
||||||
|
genericclioptions.IOStreams
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewOptions returns initialized Options.
|
||||||
|
func NewOptions(ioStreams genericclioptions.IOStreams) *Options {
|
||||||
|
return &Options{
|
||||||
|
IOStreams: ioStreams,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCmdVersion returns a cobra command for fetching versions.
|
||||||
|
func NewCmdVersion(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
||||||
|
o := NewOptions(ioStreams)
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "version",
|
||||||
|
Short: "Print the client and server version information",
|
||||||
|
Long: "Print the client and server version information for the current context",
|
||||||
|
Example: versionExample,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
cmdutil.CheckErr(o.Complete(f, cmd))
|
||||||
|
cmdutil.CheckErr(o.Validate())
|
||||||
|
cmdutil.CheckErr(o.Run())
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.Flags().BoolVar(
|
||||||
|
&o.ClientOnly,
|
||||||
|
"client",
|
||||||
|
o.ClientOnly,
|
||||||
|
"If true, shows client version only (no server required).",
|
||||||
|
)
|
||||||
|
cmd.Flags().BoolVar(&o.Short, "short", o.Short, "If true, print just the version number.")
|
||||||
|
cmd.Flags().StringVarP(&o.Output, "output", "o", o.Output, "One of 'yaml' or 'json'.")
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// Complete completes all the required options.
|
||||||
|
func (o *Options) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
|
||||||
|
var err error
|
||||||
|
if o.ClientOnly {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
o.client, err = f.RESTClient()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate validates the provided options.
|
||||||
|
func (o *Options) Validate() error {
|
||||||
|
if o.Output != "" && o.Output != "yaml" && o.Output != "json" {
|
||||||
|
return errors.New(`--output must be 'yaml' or 'json'`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes version command.
|
||||||
|
func (o *Options) Run() error {
|
||||||
|
var (
|
||||||
|
serverVersion *version.Info
|
||||||
|
serverErr error
|
||||||
|
versionInfo Version
|
||||||
|
)
|
||||||
|
|
||||||
|
clientVersion := version.Get()
|
||||||
|
versionInfo.ClientVersion = &clientVersion
|
||||||
|
|
||||||
|
if !o.ClientOnly && o.client != nil {
|
||||||
|
// Always request fresh data from the server
|
||||||
|
if err := o.client.Get().AbsPath("/version").Do(context.TODO()).Into(&serverVersion); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
versionInfo.ServerVersion = serverVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
switch o.Output {
|
||||||
|
case "":
|
||||||
|
if o.Short {
|
||||||
|
fmt.Fprintf(o.Out, "Client Version: %s\n", clientVersion.GitVersion)
|
||||||
|
|
||||||
|
if serverVersion != nil {
|
||||||
|
fmt.Fprintf(o.Out, "Server Version: %s\n", serverVersion.GitVersion)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(o.Out, "Client Version: %s\n", fmt.Sprintf("%#v", clientVersion))
|
||||||
|
if serverVersion != nil {
|
||||||
|
fmt.Fprintf(o.Out, "Server Version: %s\n", fmt.Sprintf("%#v", *serverVersion))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "yaml":
|
||||||
|
marshaled, err := yaml.Marshal(&versionInfo)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintln(o.Out, string(marshaled))
|
||||||
|
case "json":
|
||||||
|
marshaled, err := json.MarshalIndent(&versionInfo, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintln(o.Out, string(marshaled))
|
||||||
|
default:
|
||||||
|
// There is a bug in the program if we hit this case.
|
||||||
|
// However, we follow a policy of never panicking.
|
||||||
|
return fmt.Errorf("VersionOptions were not validated: --output=%q should have been rejected", o.Output)
|
||||||
|
}
|
||||||
|
|
||||||
|
return serverErr
|
||||||
|
}
|
Loading…
Reference in new issue