Skip to content

Commit

Permalink
feat: add aliyun job
Browse files Browse the repository at this point in the history
  • Loading branch information
starudream committed Sep 9, 2024
1 parent 637a31b commit cd33e72
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 3 deletions.
9 changes: 9 additions & 0 deletions cmd/aliyun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"github.com/starudream/sign-task/pkg/aliyun/cmd"
)

func init() {
rootCmd.AddCommand(cmd.AliyunCmd)
}
11 changes: 11 additions & 0 deletions pkg/aliyun/aliyun.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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))
}
}
2 changes: 1 addition & 1 deletion pkg/aliyun/api/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
52 changes: 52 additions & 0 deletions pkg/aliyun/cmd/account.go
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 10 additions & 0 deletions pkg/aliyun/cmd/aliyun.go
Original file line number Diff line number Diff line change
@@ -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"
})
4 changes: 2 additions & 2 deletions pkg/aliyun/config/account.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit cd33e72

Please sign in to comment.