-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from shijilin0116/main
init
- Loading branch information
Showing
8 changed files
with
501 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,102 @@ | ||
# ecnu-datasync-cli | ||
ecnu-datasync-cli | ||
|
||
## 简介 | ||
ecnu-datasync-cli 是一个开源的轻量级工具,用于数据同步。它提供了一种灵活、快速、方便的方式,将数据同步至 csv 或 xlsx 文件。 | ||
|
||
## 优势 | ||
|
||
无需代码编写,只需要编写相关配置文件,即可通过命令行命令实现同步数据至文件。 | ||
|
||
|
||
## 用法 | ||
|
||
### 获取 ecnu-datasync-cli 安装程序可执行文件 | ||
1. 获取可执行文件 | ||
* 方法一:下载 ecnu-datasync-cli 可执行文件 [Releases page](https://github.com/ECNU/ecnu-datasync-cli/releases/latest) | ||
|
||
下载解压后可直接使用。 | ||
|
||
* 方法二:从源代码生成二进制文件 | ||
```shell | ||
git clone https://github.com/ECNU/ecnu-datasync-cli.git | ||
cd datasync-sdk-cli | ||
go build | ||
``` | ||
> 需要 go 1.20+ 环境 | ||
win 环境下请自行添加 .exe 后缀 | ||
|
||
生成的二进制文件在 datasync-sdk-cli 中 | ||
|
||
2. 验证文件可用 | ||
```shell | ||
cd {PATH_TO_CLI}/datasync-sdk-cli | ||
./ecnu-datasync-cli -v | ||
``` | ||
应输出版本信息 | ||
|
||
### 通过 ecnu-datasync-cli 同步数据到 csv 或 xlsx 文件 | ||
|
||
#### 不使用配置文件进行同步 | ||
|
||
不使用配置文件进行同步时,直接在命令行中指定参数。 | ||
|
||
##### 命令 | ||
|
||
> 请确保处于校园网环境或使用校园网VPN | ||
|
||
```shell | ||
./ecnu-datasync-cli -c {client_id} -s {client_secret} -a {api_path} -o {output_file} | ||
``` | ||
> -o 的值既可以填写 csv 文件也可以填写 xlsx 文件,会自动根据文件的后缀名来判断导出的类型 | ||
##### 例子 | ||
|
||
* 使用示例密钥访问接口 https://api.ecnu.edu.cn/api/v1/sync/fakewithts?ts=0 ,同步数据到 path_to_csv/test.csv 文件 | ||
|
||
```shell | ||
./ecnu-datasync-cli -c=123456 -s=abcdef -a='/api/v1/sync/fakewithts?ts=0' -o='path_to_csv/test.csv' | ||
``` | ||
|
||
* 使用示例密钥访问接口 https://api.ecnu.edu.cn/api/v1/sync/fakewithts?ts=0 ,同步数据到 path_to_xlsx/test.xlsx 文件 | ||
|
||
```shell | ||
./ecnu-datasync-cli -c=123456 -s=abcdef -a='/api/v1/sync/fakewithts?ts=0' -o='path_to_xlsx/test.xlsx' | ||
``` | ||
|
||
#### 使用配置文件进行同步 | ||
|
||
您还可以使用指定配置文件的方式进行数据同步。 | ||
|
||
> 如果同时指定了 -config 和其他参数,那么会忽略其他参数,仅以配置文件中的配置进行接口调用。 | ||
|
||
##### 命令 | ||
```shell | ||
./ecnu-datasync-cli -config {config_file} | ||
``` | ||
##### 例子 | ||
1. 首先,在某目录 path_to_json 下创建一个示例配置文件 cfg.json ,并根据您的环境修改配置文件 cfg.json | ||
|
||
```json | ||
{ | ||
"oauth2_config":{ | ||
"client_id":"client_id", | ||
"client_secret":"client_secret", | ||
"base_url":"https://api.ecnu.edu.cn", | ||
"scopes":["ECNU-Basic"], | ||
"timeout":10, | ||
"debug":false | ||
}, | ||
"api_config":{ | ||
"api_path":"/api/v1/sync/fakewithts?ts=0", | ||
"page_size":2000 | ||
}, | ||
"output_file":"./test.csv" | ||
} | ||
``` | ||
> 配置文件中,output_file 的值既可以填写 csv 文件也可以填写 xlsx 文件 | ||
2. 然后,使用配置文件 path_to_json/cfg.json 中的配置进行数据同步。 | ||
|
||
```shell | ||
./ecnu-datasync-cli -config path_to_json/cfg.json | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"oauth2_config":{ | ||
"client_id":"client_id", | ||
"client_secret":"client_secret", | ||
"base_url":"https://api.ecnu.edu.cn", | ||
"scopes":["ECNU-Basic"], | ||
"timeout":10, | ||
"debug":false | ||
}, | ||
"api_config":{ | ||
"api_path":"/api/v1/sync/fakewithts?ts=0", | ||
"page_size":2000 | ||
}, | ||
"output_file":"./test.csv" | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package g | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/ecnu/ecnu-openapi-sdk-go/sdk" | ||
"log" | ||
"sync" | ||
|
||
"github.com/toolkits/pkg/file" | ||
) | ||
|
||
/* | ||
GlobalConfig 全局配置 | ||
*/ | ||
type GlobalConfig struct { | ||
OAuth2Config sdk.OAuth2Config `json:"oauth2_config"` | ||
APIConfig sdk.APIConfig `json:"api_config"` | ||
OutputFile string `json:"output_file"` | ||
} | ||
|
||
var ( | ||
ConfigFile string | ||
config *GlobalConfig | ||
lock = new(sync.RWMutex) | ||
) | ||
|
||
/* | ||
Config 安全的读取和修改配置 | ||
*/ | ||
func Config() *GlobalConfig { | ||
lock.RLock() | ||
defer lock.RUnlock() | ||
return config | ||
} | ||
|
||
/* | ||
ParseConfig 加载配置 | ||
*/ | ||
func ParseConfig(cfg string) { | ||
if cfg == "" { | ||
log.Fatalln("use -config to specify configuration file") | ||
} | ||
|
||
if !file.IsExist(cfg) { | ||
log.Fatalln("config file:", cfg, "is not existent. maybe you need `mv cfg.example.json cfg.json`") | ||
} | ||
|
||
ConfigFile = cfg | ||
|
||
configContent, err := file.ToTrimString(cfg) | ||
if err != nil { | ||
log.Fatalln("read config file:", cfg, "fail:", err) | ||
} | ||
|
||
var c GlobalConfig | ||
err = json.Unmarshal([]byte(configContent), &c) | ||
if err != nil { | ||
log.Fatalln("parse config file:", cfg, "fail:", err) | ||
} | ||
|
||
lock.Lock() | ||
defer lock.Unlock() | ||
|
||
config = &c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package g | ||
|
||
const ( | ||
VERSION = "0.5.0" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package g | ||
|
||
import ( | ||
"fmt" | ||
"github.com/ecnu/ecnu-openapi-sdk-go/sdk" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func SyncWithConfig() error { | ||
sdk.InitOAuth2ClientCredentials(Config().OAuth2Config) | ||
|
||
file := Config().OutputFile | ||
// 获取文件后缀名 | ||
extention := filepath.Ext(file) | ||
if extention != "" { | ||
// Remove the leading dot (.) from the extension. | ||
extention = extention[1:] | ||
} | ||
var mode string | ||
switch extention { | ||
case "xlsx": | ||
fallthrough | ||
case "XLSX": | ||
mode = "xlsx" | ||
default: | ||
mode = "csv" | ||
} | ||
rows, err := sdk.SyncToFile(mode, file, Config().APIConfig) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("%s:同步 %d 条数据\n", strings.ToUpper(mode), rows) | ||
return nil | ||
} | ||
func SyncWithoutConfig(clientId *string, clientSecret *string, output *string, apiPath *string) error { | ||
cf := sdk.OAuth2Config{ | ||
ClientId: *clientId, | ||
ClientSecret: *clientSecret, | ||
} | ||
sdk.InitOAuth2ClientCredentials(cf) | ||
file := *output | ||
api := sdk.APIConfig{ | ||
APIPath: *apiPath, | ||
PageSize: 2000, | ||
} | ||
// 获取文件后缀名 | ||
extention := filepath.Ext(file) | ||
if extention != "" { | ||
// Remove the leading dot (.) from the extension. | ||
extention = extention[1:] | ||
} | ||
var mode string | ||
switch extention { | ||
case "xlsx": | ||
fallthrough | ||
case "XLSX": | ||
mode = "xlsx" | ||
default: | ||
mode = "csv" | ||
} | ||
rows, err := sdk.SyncToFile(mode, file, api) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("%s:同步 %d 条数据\n", strings.ToUpper(mode), rows) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module ecnu-datasync-cli | ||
|
||
go 1.20 | ||
|
||
require ( | ||
github.com/ecnu/ecnu-openapi-sdk-go v1.0.0 | ||
github.com/toolkits/pkg v1.3.7 | ||
) | ||
|
||
require ( | ||
github.com/frankban/quicktest v1.14.6 // indirect | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
github.com/google/btree v1.0.0 // indirect | ||
github.com/google/go-cmp v0.5.9 // indirect | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.5 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/kr/pretty v0.3.1 // indirect | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/liamylian/jsontime/v2 v2.0.0 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/peterbourgon/diskv/v3 v3.0.1 // indirect | ||
github.com/rogpeppe/fastuuid v1.2.0 // indirect | ||
github.com/rogpeppe/go-internal v1.9.0 // indirect | ||
github.com/shabbyrobe/xmlwriter v0.0.0-20200208144257-9fca06d00ffa // indirect | ||
github.com/tealeg/xlsx/v3 v3.3.4 // indirect | ||
golang.org/x/oauth2 v0.12.0 // indirect | ||
golang.org/x/text v0.12.0 // indirect | ||
google.golang.org/appengine v1.6.8 // indirect | ||
google.golang.org/protobuf v1.31.0 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gorm.io/gorm v1.25.4 // indirect | ||
) |
Oops, something went wrong.