162 lines
3.7 KiB
Go
162 lines
3.7 KiB
Go
package main
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
"net/url"
|
||
"os"
|
||
|
||
"github.com/spf13/cobra"
|
||
"github.com/spf13/viper"
|
||
)
|
||
|
||
var (
|
||
env string
|
||
account string
|
||
amount int64
|
||
cfgFile string
|
||
postUrl string
|
||
msg string
|
||
notice bool = false
|
||
)
|
||
|
||
var rootCmd = &cobra.Command{
|
||
Use: "suno-tools",
|
||
Short: "recharge account of suno and notice",
|
||
Run: func(cmd *cobra.Command, args []string) {
|
||
// 如果配置文件存在,则读取配置文件
|
||
if cfgFile != "" {
|
||
viper.SetConfigFile(cfgFile)
|
||
if err := viper.ReadInConfig(); err != nil {
|
||
fmt.Println("Error reading config file:", err)
|
||
os.Exit(1)
|
||
}
|
||
}
|
||
|
||
// 获取配置
|
||
feishu := viper.GetString("feishu")
|
||
msgs := viper.Get("msg").(map[string]interface{})
|
||
envs := viper.Get("env").(map[string]interface{})
|
||
|
||
// 如果配置文件中提供飞书链接,则通知用户
|
||
if feishu != "" {
|
||
// 是否通知
|
||
notice = true
|
||
}
|
||
|
||
// 获取api链接
|
||
for e, url := range envs {
|
||
if e == env {
|
||
postUrl = url.(string)
|
||
}
|
||
}
|
||
|
||
if postUrl == "" {
|
||
postUrl = "http://127.0.0.1:8000/"
|
||
msg = fmt.Sprintf("%s接口链接未提供,使用默认链接%s", env, postUrl)
|
||
}
|
||
|
||
// 获取msg信息
|
||
var msg_ok, msg_err string
|
||
for level, info := range msgs {
|
||
if level == "ok" {
|
||
msg_ok = info.(string)
|
||
}
|
||
if level == "err" {
|
||
msg_err = info.(string)
|
||
}
|
||
}
|
||
|
||
// 生成接口请求链接
|
||
url := generateUrl(postUrl, account, map[string]interface{}{
|
||
"auto_subscribe": amount,
|
||
})
|
||
|
||
// 请求接口
|
||
if err := postRequest(url, nil); err != nil {
|
||
msg = fmt.Sprintf("%s\n%s环境 account: %s 充值: %d 失败\n原因如下: %s", msg_err, env, account, amount, err)
|
||
} else {
|
||
msg = fmt.Sprintf("%s\na%s环境 ccount: %s 充值: %d 成功", msg_ok, env, account, amount)
|
||
}
|
||
if notice {
|
||
if err := postRequest(feishu, map[string]interface{}{
|
||
"msg_type": "text",
|
||
"content": map[string]string{
|
||
"text": msg,
|
||
},
|
||
}); err != nil {
|
||
fmt.Println("Error sending message:", err)
|
||
}
|
||
}
|
||
},
|
||
}
|
||
|
||
func main() {
|
||
cobra.OnInitialize(initConfig)
|
||
|
||
rootCmd.Flags().StringVarP(&env, "env", "e", "dev", "Environment")
|
||
rootCmd.Flags().StringVarP(&account, "account", "a", "account_1", "Account")
|
||
rootCmd.Flags().Int64VarP(&amount, "amount", "m", 500, "Amount")
|
||
rootCmd.Flags().StringVarP(&cfgFile, "config", "c", "", "Config file (default is $HOME/config.yaml and ./config.yaml)")
|
||
|
||
rootCmd.MarkFlagRequired("env")
|
||
rootCmd.MarkFlagRequired("account")
|
||
rootCmd.MarkFlagRequired("amount")
|
||
if err := rootCmd.Execute(); err != nil {
|
||
fmt.Println(err)
|
||
os.Exit(1)
|
||
}
|
||
}
|
||
|
||
func initConfig() {
|
||
if cfgFile != "" {
|
||
viper.SetConfigFile(cfgFile)
|
||
} else {
|
||
home, err := os.UserHomeDir()
|
||
if err != nil {
|
||
fmt.Println(err)
|
||
os.Exit(1)
|
||
}
|
||
viper.AddConfigPath(".")
|
||
viper.AddConfigPath(home)
|
||
viper.SetConfigName("config")
|
||
}
|
||
|
||
viper.AutomaticEnv() // 从环境变量中读取配置
|
||
if err := viper.ReadInConfig(); err == nil {
|
||
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
||
}
|
||
}
|
||
|
||
func generateUrl(baseUrl, source string, parameters map[string]interface{}) string {
|
||
base, err := url.Parse(baseUrl)
|
||
if err != nil {
|
||
return ""
|
||
}
|
||
base.Path += "/" + source
|
||
query := base.Query()
|
||
for param, value := range parameters {
|
||
query.Add(param, fmt.Sprintf("%v", value))
|
||
}
|
||
base.RawQuery = query.Encode()
|
||
|
||
return base.String()
|
||
}
|
||
|
||
func postRequest(url string, data map[string]interface{}) error {
|
||
jsonData, err := json.Marshal(data)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer resp.Body.Close()
|
||
if resp.StatusCode != http.StatusOK {
|
||
return fmt.Errorf("HTTP status code: %d", resp.StatusCode)
|
||
}
|
||
return nil
|
||
}
|