2024-05-30 13:54:00 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "surdeus.su/core/amo"
|
2024-06-04 23:16:12 +03:00
|
|
|
import "surdeus.su/core/amo/api"
|
2024-05-30 13:54:00 +03:00
|
|
|
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
|
|
|
|
|
2024-05-30 23:45:36 +03:00
|
|
|
var updateComs =
|
2024-06-05 00:08:54 +03:00
|
|
|
mtool.T("update-companies").Func(func(flags *mtool.Flags){
|
2024-05-30 13:54:00 +03:00
|
|
|
var (
|
2024-06-04 23:16:12 +03:00
|
|
|
opts DefaultFlags
|
2024-05-30 13:54:00 +03:00
|
|
|
)
|
2024-06-04 23:16:12 +03:00
|
|
|
MakeDefaultFlags(&opts, flags)
|
2024-05-30 13:54:00 +03:00
|
|
|
flags.Parse()
|
|
|
|
|
2024-06-04 23:16:12 +03:00
|
|
|
client, err := amo.NewClient(opts.SecretPath)
|
2024-05-30 13:54:00 +03:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|
2024-05-30 13:54:00 +03:00
|
|
|
}
|
|
|
|
})
|