amo/cmd/amocli/updatecom.go

57 lines
1.1 KiB
Go
Raw Normal View History

package main
import "surdeus.su/core/amo"
2024-06-04 23:16:12 +03:00
import "surdeus.su/core/amo/api"
import "surdeus.su/core/cli/mtool"
import "surdeus.su/core/amo/companies"
import "encoding/json"
import "log"
import "os"
2024-06-04 23:16:12 +03:00
const MEPR = api.MaxEntitiesPerRequest
var updateComs =
2024-06-05 00:08:54 +03:00
mtool.T("update-companies").Func(func(flags *mtool.Flags){
var (
2024-06-04 23:16:12 +03:00
opts DefaultFlags
)
2024-06-04 23:16:12 +03:00
MakeDefaultFlags(&opts, flags)
flags.Parse()
2024-06-04 23:16:12 +03:00
client, err := amo.NewClient(opts.SecretPath)
if err != nil {
log.Fatalf("NewAmoClient(...): %s\n", err)
}
cs := []companies.Company{}
dec := json.NewDecoder(os.Stdin)
err = dec.Decode(&cs)
if err != nil {
log.Fatalf("json.Decode(...): %s\n", err)
}
2024-06-04 23:16:12 +03:00
n := len(cs)/MEPR + 1
for i:=0 ; i<n ; i++ {
start := i * MEPR
end := start + MEPR
if end >= len(cs) {
end = len(cs)
}
if len(cs) == 0 {
continue
}
slice := cs[start:end]
if opts.Verbose {
log.Printf("Updating %d companies...\n", len(slice))
}
err = client.UpdateCompanies(slice)
if err != nil {
log.Fatalf(
"client.UpdateCompanies(...) len(...) =%d: %s\n",
len(slice),
err,
)
}
}
})