102 lines
2.1 KiB
Go
102 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.algo.com.cn/public/saasapi"
|
|
"git.algo.com.cn/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 {
|
|
return fmt.Errorf("Command line parse error: %w", 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 {
|
|
return fmt.Errorf("AD parse error. value: %v. %w", v, err)
|
|
}
|
|
numAds = append(numAds, n)
|
|
}
|
|
|
|
cfg, err := LoadConfigFile(*cfgFile)
|
|
if err != nil {
|
|
return fmt.Errorf("LoadConfigFile error: %w", 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 {
|
|
return fmt.Errorf("Submit Command error: %w", err)
|
|
}
|
|
|
|
if res.Code != saasapi.ErrorCode_SUCC {
|
|
return fmt.Errorf("Command failed. code:%v, status:%v", res.Code, res.Status)
|
|
}
|
|
|
|
fmt.Printf("bind res: %v\n", protojson.Format(res.GetBindSetRes()))
|
|
return nil
|
|
}
|