Skip to content

Commit

Permalink
persist login info and add logout function
Browse files Browse the repository at this point in the history
  • Loading branch information
jicheng1014 committed Mar 17, 2023
1 parent 34c0179 commit 83c7371
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go_fir_cli_build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: go_fir_cli build
name: go-fir-cli build
on:
release:
types: [created]
Expand All @@ -25,4 +25,4 @@ jobs:
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "1.19.1"
binary_name: "go_fir_cli"
binary_name: "go-fir-cli"
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
icon.png
qr.png
go_fir_cli
go_fir_cli.exe
go-fir-cli
go-fir-cli.exe
test.go
*.apk
*.ipa
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ go-fir-cli 只实现了 fir-cli 的部分功能, 并无计划实现全部功能,
由于作者不善 golang, 所以大部分代码皆来自于 Copilot 和 chatGPT 生成, 作者仅在此基础上做了一些修改与调试, 以便于使用. 如果您在使用中发现任何问题, 欢迎提 issue 或者 pr.

## 更新说明
- 0.0.7 支持了持久化登录
- 0.0.6 支持了windows平台
- 0.0.5 支持了上传进度条显示
- 0.0.4 支持了 arm64 的二进制文件, 上传时会有命令行的提示
- 0.0.3 支持了 企业微信、飞书和钉钉的通知机器人
Expand Down Expand Up @@ -51,7 +53,7 @@ go-fir-cli 只实现了 fir-cli 的部分功能, 并无计划实现全部功能,

### 检测API 是否可用

```bash
```bash
./go-fir-cli login -t 您的API_TOKEN

#
Expand Down Expand Up @@ -79,4 +81,3 @@ go-fir-cli 只实现了 fir-cli 的部分功能, 并无计划实现全部功能,
### 自行编译

下载好代码 安装好依赖即可运行 go build

4 changes: 2 additions & 2 deletions api/rio_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"path"
"time"

"betaqr.com/go_fir_cli/analysis"
"betaqr.com/go_fir_cli/constants"
"github.com/PGYER/go-fir-cli/analysis"
"github.com/PGYER/go-fir-cli/constants"
"github.com/cheggaaa/pb"
"github.com/go-resty/resty/v2"
)
Expand Down
2 changes: 1 addition & 1 deletion constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package constants

const VERSION = "0.0.4"
const VERSION = "0.0.6"
const USER_AGENT = "go-fir-" + VERSION
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module betaqr.com/go_fir_cli
module github.com/PGYER/go-fir-cli

go 1.19

Expand Down
41 changes: 29 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package main

import (
"betaqr.com/go_fir_cli/api"
"betaqr.com/go_fir_cli/constants"
"betaqr.com/go_fir_cli/notifiers"
"betaqr.com/go_fir_cli/utils"
"fmt"
"github.com/skip2/go-qrcode"
"gopkg.in/urfave/cli.v1"
"io/ioutil"
"os"

"github.com/PGYER/go-fir-cli/api"
"github.com/PGYER/go-fir-cli/constants"
"github.com/PGYER/go-fir-cli/notifiers"
"github.com/PGYER/go-fir-cli/utils"
"github.com/skip2/go-qrcode"
"gopkg.in/urfave/cli.v1"
)

func main() {
initCli()
}
func initCli() {
app := cli.NewApp()
app.Name = "go_fir_cli"
app.Name = "go-fir-cli"

app.Usage = "完成 fir.im 的命令行操作"
app.Version = constants.VERSION
Expand All @@ -31,22 +32,23 @@ func initCli() {

app.Commands = []cli.Command{
initLogin(),
logoutCommand(),
testWebhook(),
uploadFile(),
cli.Command{
Name: "version",
ShortName: "v",
Usage: "查看 go_fir_cli 版本",
Usage: "查看 go-fir-cli 版本",
Action: func(c *cli.Context) error {
fmt.Println(constants.VERSION)
return nil
},
},
cli.Command{
Name: "upgrade",
Usage: "如何升级 go_fir_cli",
Usage: "如何升级 go-fir-cli",
Action: func(c *cli.Context) error {
fmt.Println("请访问 https://github.com/PGYER/go-fir-cli/releases 下载对应版本, 并替换原有的 go_fir_cli 文件")
fmt.Println("请访问 https://github.com/PGYER/go-fir-cli/releases 下载对应版本, 并替换原有的 go-fir-cli 文件")
return nil
},
},
Expand All @@ -65,13 +67,28 @@ func initLogin() cli.Command {
Usage: "fir.im 的 api token",
},
},

Action: func(c *cli.Context) error {
api_token := c.String("token")
fir_api := &api.FirApi{}
if err := fir_api.Login(api_token); err != nil {
fmt.Println("登录失败, 请检查 token 是否正确")
} else {
fmt.Println(fir_api.Email + " 登录成功")
return utils.SaveToLocal(fir_api.Email, api_token)
}
fmt.Println(fir_api.Email)
return nil
},
}
}

func logoutCommand() cli.Command {
return cli.Command{
Name: "logout",
Usage: "退出登录",
Action: func(c *cli.Context) error {
utils.DelConfig()
fmt.Println("已经退出登录")
return nil
},
}
Expand Down Expand Up @@ -114,7 +131,7 @@ func testWebhook() cli.Command {
func uploadFile() cli.Command {
return cli.Command{
Name: "upload",
Usage: "上传文件, 例如 go_fir_cli -t FIR_TOKEN upload -f FILE_PATH -c CHANGELOG",
Usage: "上传文件, 例如 go-fir-cli -t FIR_TOKEN upload -f FILE_PATH -c CHANGELOG",

Flags: []cli.Flag{
cli.StringFlag{
Expand Down
2 changes: 1 addition & 1 deletion notifiers/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/url"
"time"

"betaqr.com/go_fir_cli/api"
"github.com/PGYER/go-fir-cli/api"
"github.com/go-resty/resty/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion notifiers/lark.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"time"

"betaqr.com/go_fir_cli/api"
"github.com/PGYER/go-fir-cli/api"
"github.com/go-resty/resty/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion notifiers/wecom.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/url"
"time"

"betaqr.com/go_fir_cli/api"
"github.com/PGYER/go-fir-cli/api"
"github.com/go-resty/resty/v2"
)

Expand Down
19 changes: 17 additions & 2 deletions utils/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
)

const CONFIG_FILE = ".go-fir-cli"

func LoadLocalToken() string {
config, err := LoadLocalConfig()
if err != nil {
Expand All @@ -15,12 +17,25 @@ func LoadLocalToken() string {
return config["token"]
}

func DelConfig() error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
tokenPath := filepath.Join(home, CONFIG_FILE)
// 如果存在, 则删除文件
if _, err := os.Stat(tokenPath); err == nil {
return os.Remove(tokenPath)
}
return nil
}

func LoadLocalConfig() (map[string]string, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
tokenPath := filepath.Join(home, ".fir-cli")
tokenPath := filepath.Join(home, CONFIG_FILE)
raw, err := os.ReadFile(tokenPath)
if err != nil {
return nil, err
Expand All @@ -45,6 +60,6 @@ func SaveToLocal(email string, token string) error {
if err != nil {
return err
}
tokenPath := filepath.Join(home, ".fir-cli")
tokenPath := filepath.Join(home, CONFIG_FILE)
return os.WriteFile(tokenPath, []byte(raw), 0644)
}

0 comments on commit 83c7371

Please sign in to comment.