From cd33e725029cfd98ab89775834922c83b6e2a13b Mon Sep 17 00:00:00 2001 From: "sheng.wang" Date: Mon, 9 Sep 2024 16:54:58 +0800 Subject: [PATCH] feat: add aliyun job --- cmd/aliyun.go | 9 +++++++ pkg/aliyun/aliyun.go | 11 ++++++++ pkg/aliyun/api/sign.go | 2 +- pkg/aliyun/cmd/account.go | 52 ++++++++++++++++++++++++++++++++++++ pkg/aliyun/cmd/aliyun.go | 10 +++++++ pkg/aliyun/config/account.go | 4 +-- 6 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 cmd/aliyun.go create mode 100644 pkg/aliyun/cmd/account.go create mode 100644 pkg/aliyun/cmd/aliyun.go diff --git a/cmd/aliyun.go b/cmd/aliyun.go new file mode 100644 index 0000000..c191676 --- /dev/null +++ b/cmd/aliyun.go @@ -0,0 +1,9 @@ +package main + +import ( + "github.com/starudream/sign-task/pkg/aliyun/cmd" +) + +func init() { + rootCmd.AddCommand(cmd.AliyunCmd) +} diff --git a/pkg/aliyun/aliyun.go b/pkg/aliyun/aliyun.go index 64cf253..95bc167 100644 --- a/pkg/aliyun/aliyun.go +++ b/pkg/aliyun/aliyun.go @@ -1,6 +1,9 @@ package aliyun import ( + "fmt" + + "github.com/starudream/sign-task/pkg/aliyun/api" "github.com/starudream/sign-task/pkg/aliyun/config" "github.com/starudream/sign-task/pkg/cron" ) @@ -22,4 +25,12 @@ func (j aliyun) Do() { } func (j aliyun) do(a config.Account) { + c := api.NewClient(a) + + balance, err := c.QueryAccountBalance() + if err != nil { + cron.Ntfy(j, "阿里云", fmt.Sprintf("执行失败(%s)", err)) + } else { + cron.Ntfy(j, "阿里云", fmt.Sprintf("可用额度:%s,现金余额:%s", balance.AvailableAmount, balance.AvailableCashAmount)) + } } diff --git a/pkg/aliyun/api/sign.go b/pkg/aliyun/api/sign.go index d355b3b..e9d1a18 100644 --- a/pkg/aliyun/api/sign.go +++ b/pkg/aliyun/api/sign.go @@ -36,7 +36,7 @@ func addSign(r *resty.Request, method, addr, path, action, version string, accou signStr := strings.Join([]string{strings.ToUpper(method), path, queryStr, headerStr, headerKeys, bodyHex}, "\n") signature := strings.ToLower(util.HMAC256Hex(account.Secret, Algorithm+"\n"+util.SHA256Hex(signStr))) - r.SetHeader("Authorization", fmt.Sprintf("%s Credential=%s,SignedHeaders=%s,Signature=%s", Algorithm, account.Key, headerKeys, signature)) + r.SetHeader("Authorization", fmt.Sprintf("%s Credential=%s,SignedHeaders=%s,Signature=%s", Algorithm, account.Id, headerKeys, signature)) } func genBody(body any) string { diff --git a/pkg/aliyun/cmd/account.go b/pkg/aliyun/cmd/account.go new file mode 100644 index 0000000..75a7d52 --- /dev/null +++ b/pkg/aliyun/cmd/account.go @@ -0,0 +1,52 @@ +package cmd + +import ( + "fmt" + + "github.com/starudream/go-lib/cobra/v2" + "github.com/starudream/go-lib/core/v2/utils/fmtutil" + "github.com/starudream/go-lib/tablew/v2" + + "github.com/starudream/sign-task/pkg/aliyun/config" +) + +var ( + accountCmd = cobra.NewCommand(func(c *cobra.Command) { + c.Use = "account" + c.Short = "Manage account" + }) + + accountInitCmd = cobra.NewCommand(func(c *cobra.Command) { + c.Use = "init" + c.Short = "Init account information" + c.Long = c.Short + "\n" + "Aliyun RAM: https://ram.console.aliyun.com/users" + c.RunE = func(cmd *cobra.Command, args []string) error { + id := fmtutil.Scan("please enter AccessKey ID: ") + if id == "" { + return nil + } + secret := fmtutil.Scan("please enter AccessKey Secret: ") + if secret == "" { + return nil + } + config.AddAccount(config.Account{Id: id, Secret: secret}) + return config.Save() + } + }) + + accountListCmd = cobra.NewCommand(func(c *cobra.Command) { + c.Use = "list" + c.Aliases = []string{"ls"} + c.Short = "List account" + c.Run = func(cmd *cobra.Command, args []string) { + fmt.Println(tablew.Structs(config.C().Accounts)) + } + }) +) + +func init() { + accountCmd.AddCommand(accountInitCmd) + accountCmd.AddCommand(accountListCmd) + + AliyunCmd.AddCommand(accountCmd) +} diff --git a/pkg/aliyun/cmd/aliyun.go b/pkg/aliyun/cmd/aliyun.go new file mode 100644 index 0000000..c373874 --- /dev/null +++ b/pkg/aliyun/cmd/aliyun.go @@ -0,0 +1,10 @@ +package cmd + +import ( + "github.com/starudream/go-lib/cobra/v2" +) + +var AliyunCmd = cobra.NewCommand(func(c *cobra.Command) { + c.Use = "aliyun" + c.Short = "Manage aliyun" +}) diff --git a/pkg/aliyun/config/account.go b/pkg/aliyun/config/account.go index c68f38d..73932b2 100644 --- a/pkg/aliyun/config/account.go +++ b/pkg/aliyun/config/account.go @@ -1,12 +1,12 @@ package config type Account struct { - Key string `json:"key" yaml:"key"` + Id string `json:"id" yaml:"id"` Secret string `json:"secret" yaml:"secret"` } func (account Account) GetKey() string { - return account.Key + return account.Id } func AddAccount(account Account) {