-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cookiecloud sync cookies / import sites features
- Loading branch information
Showing
11 changed files
with
617 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package importsites | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
"strings" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/sagan/ptool/cmd/cookiecloud" | ||
"github.com/sagan/ptool/config" | ||
"github.com/sagan/ptool/site" | ||
"github.com/sagan/ptool/site/tpl" | ||
"github.com/sagan/ptool/util" | ||
) | ||
|
||
var ( | ||
doAction = false | ||
profile = "" | ||
) | ||
|
||
var command = &cobra.Command{ | ||
Use: "import", | ||
Annotations: map[string]string{"cobra-prompt-dynamic-suggestions": "cookiecloud.import"}, | ||
Short: "Import sites from cookies of cookiecloud servers.", | ||
Long: `Import sites from cookies of cookiecloud servers. | ||
It will get latest cookies from cookiecloud servers, find sites that do NOT exist in config file currently, | ||
Test their cookies are valid, then add them to config file. | ||
It will ask for confirm before updating config file, unless --do flag is set. | ||
Be aware that all existing comments in config file will be LOST when updating config file.`, | ||
RunE: importsites, | ||
} | ||
|
||
func init() { | ||
command.Flags().BoolVarP(&doAction, "do", "", false, "Do update the config file without confirm. Be aware that all existing comments in config file will be LOST") | ||
command.Flags().StringVarP(&profile, "profile", "", "", "Comma-separated string, Set the used cookiecloud profile name(s). If not set, All cookiecloud profiles in config will be used") | ||
cookiecloud.Command.AddCommand(command) | ||
} | ||
|
||
func importsites(cmd *cobra.Command, args []string) error { | ||
cntError := int64(0) | ||
cookiecloudProfiles := cookiecloud.ParseProfile(profile) | ||
if len(cookiecloudProfiles) == 0 { | ||
return fmt.Errorf("no cookiecloud profile specified or found") | ||
} | ||
cookiecloudDatas := []cookiecloud.Ccdata_struct{} | ||
for _, profile := range cookiecloudProfiles { | ||
data, err := cookiecloud.GetCookiecloudData(profile.Server, profile.Uuid, profile.Password, profile.Proxy) | ||
if err != nil { | ||
log.Errorf("Cookiecloud server %s (uuid %s) connection failed: %v\n", profile.Server, profile.Uuid, err) | ||
cntError++ | ||
} else { | ||
log.Infof("Cookiecloud server %s (uuid %s) connection ok: %d site cookies found\n", | ||
profile.Server, profile.Uuid, len(data.Cookie_data)) | ||
cookiecloudDatas = append(cookiecloudDatas, cookiecloud.Ccdata_struct{ | ||
Domain: util.GetUrlDomain(profile.Server), | ||
Uuid: profile.Uuid, | ||
Sites: profile.Sites, | ||
Data: data, | ||
}) | ||
} | ||
} | ||
if len(cookiecloudDatas) == 0 { | ||
return fmt.Errorf("no cookiecloud server can be connected") | ||
} | ||
|
||
addSites := []*config.SiteConfigStruct{} | ||
tplExistingFlags := map[string]bool{} | ||
for _, tplname := range tpl.SITENAMES { | ||
tplInfo := tpl.SITES[tplname] | ||
for _, site := range config.Get().Sites { | ||
if site.Type == tplname || slices.Index(tplInfo.Aliases, site.Type) != -1 { | ||
tplExistingFlags[tplname] = true | ||
break | ||
} | ||
} | ||
if sitename, _ := tpl.GuessSiteByDomain(util.ParseUrlHostname(tplInfo.Url), ""); sitename != "" { | ||
tplExistingFlags[tplname] = true | ||
} | ||
} | ||
for _, cookiecloudData := range cookiecloudDatas { | ||
for _, tplname := range tpl.SITENAMES { | ||
if tplExistingFlags[tplname] { | ||
continue | ||
} | ||
cookie, _ := cookiecloudData.Data.GetEffectiveCookie(tpl.SITES[tplname].Url) | ||
if cookie == "" { | ||
continue | ||
} | ||
newsiteconfig := &config.SiteConfigStruct{Type: tplname, Cookie: cookie} | ||
siteInstance, err := site.CreateSiteInternal(tplname, newsiteconfig, config.Get()) | ||
if err != nil { | ||
log.Debugf("New Site %s from cookiecloud %s - %s is invalid (create instance error: %v", | ||
tplname, cookiecloudData.Domain, cookiecloudData.Uuid, err) | ||
continue | ||
} | ||
sitestatus, err := siteInstance.GetStatus() | ||
if err != nil { | ||
log.Debugf("New Site %s from cookiecloud %s - %s is invalid (get status error: %v", | ||
tplname, cookiecloudData.Domain, cookiecloudData.Uuid, err) | ||
continue | ||
} | ||
log.Infof("✓✓New site %s from cookiecloud %s - %s is valid (username: %s)", | ||
tplname, cookiecloudData.Domain, cookiecloudData.Uuid, sitestatus.UserName) | ||
sitename := "" | ||
if config.GetSiteConfig(tplname) != nil { | ||
i := 1 | ||
for { | ||
sitename = fmt.Sprint(tplname, i) | ||
if config.GetSiteConfig(sitename) == nil { | ||
break | ||
} | ||
i++ | ||
} | ||
} | ||
log.Infof("Add new site type=%s, name=%s", tplname, sitename) | ||
addSites = append(addSites, &config.SiteConfigStruct{ | ||
Name: sitename, | ||
Type: tplname, | ||
Cookie: cookie, | ||
}) | ||
tplExistingFlags[tplname] = true | ||
} | ||
} | ||
|
||
if len(addSites) > 0 { | ||
fmt.Printf("✓new sites found (%d): %s", len(addSites), | ||
strings.Join(util.Map(addSites, func(site *config.SiteConfigStruct) string { | ||
return site.Type | ||
}), ", ")) | ||
|
||
configFile := fmt.Sprintf("%s/%s", config.ConfigDir, config.ConfigFile) | ||
fmt.Printf("\n") | ||
if !doAction { | ||
fmt.Printf("Will update the config file (%s). Please be aware that all existing comments will be LOST., are you sure? (yes/no): ", configFile) | ||
input := "" | ||
fmt.Scanf("%s", &input) | ||
if input != "yes" { | ||
return fmt.Errorf("abort") | ||
} | ||
} | ||
config.UpdateSites(addSites) | ||
err := config.Set() | ||
if err == nil { | ||
fmt.Printf("Successfully update config file %s", configFile) | ||
} else { | ||
log.Fatalf("Failed to update config file %s : %v", configFile, err) | ||
} | ||
} else { | ||
fmt.Printf("!No new sites found in cookiecloud datas") | ||
} | ||
|
||
if cntError > 0 { | ||
return fmt.Errorf("%d errors", cntError) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.