增加策略列表、绑定、解绑

This commit is contained in:
algotao
2025-08-09 15:54:57 +08:00
parent 6ca9fe7a02
commit edb12c3b1f
22 changed files with 1674 additions and 125 deletions

46
cmd/saastool/bind.go Normal file
View File

@@ -0,0 +1,46 @@
package main
import (
"fmt"
"os"
"strings"
)
func RunBind(args ...string) error {
name, args := ParseCommandName(args)
// 从参数中解析出命令
switch name {
case "", "help":
return RunBindHelp(args...)
case "setaccount":
return RunBindSetAccount(args...)
case "setad":
return RunBindSetAd(args...)
case "delete":
return RunBindDelete(args...)
default:
err := fmt.Errorf(`unknown command "%s"`+"\n"+`Run 'saastool bind help' for usage`, name)
fmt.Fprintln(os.Stderr, err)
return err
}
}
func RunBindHelp(args ...string) error {
fmt.Println(strings.TrimSpace(bindUsage))
return nil
}
const bindUsage = `
Usage: saastoola bind COMMAND [OPTIONS]
Commands:
setaccount Set Account binds
setad Set AdGroup binds
delete Delete binds
"help" is the default command.
Use "saastool bind COMMAND -help" for more information about a command.
`

105
cmd/saastool/bind_delete.go Normal file
View File

@@ -0,0 +1,105 @@
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"e.coding.net/rta/public/saasapi"
"e.coding.net/rta/public/saasapi/pkg/saashttp"
"google.golang.org/protobuf/encoding/protojson"
)
type bindDeleteParams struct {
idType int
ids []int64
saasHttp *saashttp.SaasClient
}
func RunBindDelete(args ...string) error {
fs := flag.NewFlagSet("delete", flag.ExitOnError)
cfgFile := paramConfig(fs)
idtype := paramIdType(fs)
ids := paramIDs(fs)
if err := fs.Parse(args); err != nil {
fmt.Fprintln(os.Stderr, "command line parse error", "err", err)
return err
}
// 切割字符串
idsSlice := strings.Split(*ids, ",")
if len(idsSlice) == 1 && idsSlice[0] == "" {
idsSlice = []string{}
}
if fs.NArg() > 0 || len(idsSlice) == 0 || (len(idsSlice) == 1 && idsSlice[0] == "") {
fs.PrintDefaults()
return nil
}
numIds := []int64{}
for _, v := range idsSlice {
n, err := strconv.ParseInt(v, 10, 64)
if err != nil {
fmt.Fprintln(os.Stderr, "id parse error", "value", v, "err", err)
return err
}
numIds = append(numIds, n)
}
cfg, err := LoadConfigFile(*cfgFile)
if err != nil {
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
return err
}
bindDeleteParams := bindDeleteParams{
idType: *idtype,
ids: numIds,
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
}
return doBindDeleteAccount(bindDeleteParams)
}
func doBindDeleteAccount(p bindDeleteParams) error {
bindDelete := &saasapi.BindDelete{}
saasReq := &saasapi.SaasReq{
Cmd: &saasapi.SaasReq_BindDelete{
BindDelete: bindDelete,
},
}
for _, v := range p.ids {
bindDelete.Binds = append(bindDelete.Binds, &saasapi.Bind{
BindId: v,
BindType: saasapi.BindType(p.idType),
})
}
res, err := p.saasHttp.BindDelete(saasReq)
if err != nil {
fmt.Fprintln(os.Stderr, "submit Bind Delete error", "err", err)
return err
}
if res.Code != saasapi.ErrorCode_SUCC {
fmt.Fprintln(os.Stderr, "Bind Delete failed", "code", res.Code, "status", res.Status)
return nil
}
bindRes := res.GetBindDeleteRes()
fmt.Printf("bind res: %v\n", protojson.Format(bindRes))
return nil
}

View File

@@ -0,0 +1,107 @@
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"e.coding.net/rta/public/saasapi"
"e.coding.net/rta/public/saasapi/pkg/saashttp"
"google.golang.org/protobuf/encoding/protojson"
)
type bindSetAccountParams struct {
target string
accounts []int64
saasHttp *saashttp.SaasClient
}
func RunBindSetAccount(args ...string) error {
fs := flag.NewFlagSet("setaccount", flag.ExitOnError)
cfgFile := paramConfig(fs)
target := paramTarget(fs)
accounts := paramAccounts(fs)
if err := fs.Parse(args); err != nil {
fmt.Fprintln(os.Stderr, "command line parse error", "err", err)
return err
}
// 切割字符串
idsSlice := strings.Split(*accounts, ",")
if len(idsSlice) == 1 && idsSlice[0] == "" {
idsSlice = []string{}
}
if fs.NArg() > 0 || len(idsSlice) == 0 || (len(idsSlice) == 1 && idsSlice[0] == "") {
fs.PrintDefaults()
return nil
}
numAccounts := []int64{}
for _, v := range idsSlice {
n, err := strconv.ParseInt(v, 10, 64)
if err != nil {
fmt.Fprintln(os.Stderr, "account parse error", "value", v, "err", err)
return err
}
numAccounts = append(numAccounts, n)
}
cfg, err := LoadConfigFile(*cfgFile)
if err != nil {
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
return err
}
bindSetAccountParams := bindSetAccountParams{
target: *target,
accounts: numAccounts,
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
}
return doBindSetAccount(bindSetAccountParams)
}
func doBindSetAccount(p bindSetAccountParams) error {
bindSet := &saasapi.BindSet{}
saasReq := &saasapi.SaasReq{
Cmd: &saasapi.SaasReq_BindSet{
BindSet: bindSet,
},
}
for _, v := range p.accounts {
bindSet.Binds = append(bindSet.Binds, &saasapi.Bind{
BindId: v,
BindType: saasapi.BindType_AccountId,
TargetId: p.target,
AccountId: v,
})
}
res, err := p.saasHttp.BindSet(saasReq)
if err != nil {
fmt.Fprintln(os.Stderr, "submit Bind Set error", "err", err)
return err
}
if res.Code != saasapi.ErrorCode_SUCC {
fmt.Fprintln(os.Stderr, "Bind Set failed", "code", res.Code, "status", res.Status)
return nil
}
bindRes := res.GetBindSetRes()
fmt.Printf("bind res: %v\n", protojson.Format(bindRes))
return nil
}

110
cmd/saastool/bind_setad.go Normal file
View File

@@ -0,0 +1,110 @@
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"e.coding.net/rta/public/saasapi"
"e.coding.net/rta/public/saasapi/pkg/saashttp"
"google.golang.org/protobuf/encoding/protojson"
)
type bindSetAdParams struct {
target string
account int64
ads []int64
saasHttp *saashttp.SaasClient
}
func RunBindSetAd(args ...string) error {
fs := flag.NewFlagSet("setad", flag.ExitOnError)
cfgFile := paramConfig(fs)
target := paramTarget(fs)
account := paramAccount(fs)
ads := paramAds(fs)
if err := fs.Parse(args); err != nil {
fmt.Fprintln(os.Stderr, "command line parse error", "err", err)
return err
}
// 切割字符串
idsSlice := strings.Split(*ads, ",")
if len(idsSlice) == 1 && idsSlice[0] == "" {
idsSlice = []string{}
}
if fs.NArg() > 0 || len(idsSlice) == 0 || (len(idsSlice) == 1 && idsSlice[0] == "") {
fs.PrintDefaults()
return nil
}
numAds := []int64{}
for _, v := range idsSlice {
n, err := strconv.ParseInt(v, 10, 64)
if err != nil {
fmt.Fprintln(os.Stderr, "account parse error", "value", v, "err", err)
return err
}
numAds = append(numAds, n)
}
cfg, err := LoadConfigFile(*cfgFile)
if err != nil {
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
return err
}
bindSetAdParams := bindSetAdParams{
target: *target,
account: *account,
ads: numAds,
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
}
return doBindSetAd(bindSetAdParams)
}
func doBindSetAd(p bindSetAdParams) error {
bindSet := &saasapi.BindSet{}
saasReq := &saasapi.SaasReq{
Cmd: &saasapi.SaasReq_BindSet{
BindSet: bindSet,
},
}
for _, v := range p.ads {
bindSet.Binds = append(bindSet.Binds, &saasapi.Bind{
BindId: v,
BindType: saasapi.BindType_AdgroupId,
TargetId: p.target,
AccountId: p.account,
})
}
res, err := p.saasHttp.BindSet(saasReq)
if err != nil {
fmt.Fprintln(os.Stderr, "submit Bind Set error", "err", err)
return err
}
if res.Code != saasapi.ErrorCode_SUCC {
fmt.Fprintln(os.Stderr, "Bind Set failed", "code", res.Code, "status", res.Status)
return nil
}
bindRes := res.GetBindSetRes()
fmt.Printf("bind res: %v\n", protojson.Format(bindRes))
return nil
}

View File

@@ -21,7 +21,9 @@ Commands:
convert Convert data to write format
task Task commands
task Task commands
target Target commands
bind Bind commands
"help" is the default command.

View File

@@ -30,6 +30,10 @@ func Run(args ...string) error {
return RunVerify(args...)
case "task":
return RunTask(args...)
case "target":
return RunTarget(args...)
case "bind":
return RunBind(args...)
default:
err := fmt.Errorf(`unknown command "%v"`+"\n"+`Run 'saastool help' for usage`, name)
fmt.Fprintln(os.Stderr, err.Error())

View File

@@ -73,6 +73,38 @@ func paramDataSpaceId(fs *flag.FlagSet) *string {
return fs.String("ds", "", "Data space ID")
}
func paramTargets(fs *flag.FlagSet) *string {
return fs.String("targets", "", "Target IDs. Use commas to separate multiple IDs")
}
func paramListBinds(fs *flag.FlagSet) *bool {
return fs.Bool("b", false, "List Binds")
}
func paramTarget(fs *flag.FlagSet) *string {
return fs.String("target", "", "Target ID")
}
func paramAccount(fs *flag.FlagSet) *int64 {
return fs.Int64("account", 0, "Advertiser ID")
}
func paramAccounts(fs *flag.FlagSet) *string {
return fs.String("accounts", "", "Advertiser IDs. Use commas to separate multiple IDs")
}
func paramAds(fs *flag.FlagSet) *string {
return fs.String("ads", "", "AdGroup IDs. Use commas to separate multiple IDs")
}
func paramIdType(fs *flag.FlagSet) *int {
return fs.Int("idtype", 0, "ID Type. empty is Automatic matching, 1=AdGroup, 3=Account")
}
func paramIDs(fs *flag.FlagSet) *string {
return fs.String("ids", "", "IDs for delete. Use commas to separate multiple IDs")
}
// ParseByteSize 解析字节大小字符串为字节数
func ParseByteSize(sizeStr string) (uint64, error) {
sizeStr = strings.TrimSpace(sizeStr)

View File

@@ -62,7 +62,7 @@ func RunRead(args ...string) error {
ds: *ds,
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: &cfg.ApiUrls,
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
}

40
cmd/saastool/target.go Normal file
View File

@@ -0,0 +1,40 @@
package main
import (
"fmt"
"os"
"strings"
)
func RunTarget(args ...string) error {
name, args := ParseCommandName(args)
// 从参数中解析出命令
switch name {
case "", "help":
return RunTargetHelp(args...)
case "list":
return RunTargetList(args...)
default:
err := fmt.Errorf(`unknown command "%s"`+"\n"+`Run 'saastool target help' for usage`, name)
fmt.Fprintln(os.Stderr, err)
return err
}
}
func RunTargetHelp(args ...string) error {
fmt.Println(strings.TrimSpace(targetUsage))
return nil
}
const targetUsage = `
Usage: saastoola target COMMAND [OPTIONS]
Commands:
list List targets
"help" is the default command.
Use "saastool target COMMAND -help" for more information about a command.
`

View File

@@ -0,0 +1,88 @@
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strings"
"e.coding.net/rta/public/saasapi"
"e.coding.net/rta/public/saasapi/pkg/saashttp"
"google.golang.org/protobuf/encoding/protojson"
)
type listTargetParams struct {
targets []string
listBinds bool
saasHttp *saashttp.SaasClient
}
func RunTargetList(args ...string) error {
fs := flag.NewFlagSet("list", flag.ExitOnError)
cfgFile := paramConfig(fs)
targets := paramTargets(fs)
listBinds := paramListBinds(fs)
if err := fs.Parse(args); err != nil {
fmt.Fprintln(os.Stderr, "command line parse error", "err", err)
return err
}
if fs.NArg() > 0 {
fs.PrintDefaults()
return nil
}
// 切割字符串
idsSlice := strings.Split(*targets, ",")
if len(idsSlice) == 1 && idsSlice[0] == "" {
idsSlice = []string{}
}
cfg, err := LoadConfigFile(*cfgFile)
if err != nil {
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
return err
}
listTargetParams := listTargetParams{
listBinds: *listBinds,
targets: idsSlice,
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
}
return doTargetList(listTargetParams)
}
func doTargetList(listTargetParams listTargetParams) error {
saasReq := &saasapi.SaasReq{
Cmd: &saasapi.SaasReq_TargetList{
TargetList: &saasapi.TargetList{
Targets: listTargetParams.targets,
ListBind: listTargetParams.listBinds,
},
},
}
res, err := listTargetParams.saasHttp.TargetList(saasReq)
if err != nil {
fmt.Fprintln(os.Stderr, "submit List Target error", "err", err)
return err
}
if res.Code != saasapi.ErrorCode_SUCC {
fmt.Fprintln(os.Stderr, "Target list failed", "code", res.Code, "status", res.Status)
return nil
}
targetRes := res.GetTargetListRes()
fmt.Printf("target res: %v\n", protojson.Format(targetRes))
return nil
}

View File

@@ -42,7 +42,7 @@ func RunTaskCreate(args ...string) error {
hashFile: *hashFile,
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: &cfg.ApiUrls,
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
task: &saasapi.Task{},

View File

@@ -41,7 +41,7 @@ func RunTaskDelete(args ...string) error {
taskSha256: *sha256,
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: &cfg.ApiUrls,
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
}

View File

@@ -44,7 +44,7 @@ func RunTaskDownload(args ...string) error {
destPath: *destPath,
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: &cfg.ApiUrls,
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
}
@@ -63,7 +63,7 @@ func doTaskDownload(downloadTaskParams downloadTaskParams) error {
}
if len(taskInfo.GetSourcePath()) == 0 {
err = fmt.Errorf("task download failed. source path is empty")
err = fmt.Errorf("task download failed. task info source path is empty")
fmt.Fprintln(os.Stderr, err)
return err
}

View File

@@ -41,7 +41,7 @@ func RunTaskInfo(args ...string) error {
taskSha256: *sha256,
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: &cfg.ApiUrls,
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
}

View File

@@ -40,7 +40,7 @@ func RunTaskList(args ...string) error {
listTaskParams := listTaskParams{
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: &cfg.ApiUrls,
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
}

View File

@@ -41,7 +41,7 @@ func RunTaskRun(args ...string) error {
taskSha256: *sha256,
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: &cfg.ApiUrls,
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
}

View File

@@ -41,7 +41,7 @@ func RunTaskUpload(args ...string) error {
taskSha256: *sha256,
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: &cfg.ApiUrls,
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
}

View File

@@ -63,7 +63,7 @@ func RunWrite(args ...string) error {
clear: *clear,
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: &cfg.ApiUrls,
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
}