From ab465ef9e083a316de737405902a7f47ec43f2ca Mon Sep 17 00:00:00 2001 From: Rustam Gilyazov <16064414+rusq@users.noreply.github.com> Date: Sat, 13 Jan 2024 21:38:29 +1000 Subject: [PATCH] move info tools --- cmd/slackdump/internal/diag/diag.go | 7 +- cmd/slackdump/internal/diag/eztest.go | 4 +- cmd/slackdump/internal/diag/info.go | 31 +++++++++ .../internal/{ => diag}/info/ezlogin.go | 0 .../internal/{ => diag}/info/info.go | 64 +++++++++++++------ cmd/slackdump/internal/{ => diag}/info/os.go | 4 +- .../internal/{ => diag}/info/playwright.go | 4 +- cmd/slackdump/internal/{ => diag}/info/rod.go | 4 +- .../internal/{ => diag}/info/workspace.go | 4 +- cmd/slackdump/internal/info/collector.go | 58 ----------------- cmd/slackdump/main.go | 2 - 11 files changed, 92 insertions(+), 90 deletions(-) create mode 100644 cmd/slackdump/internal/diag/info.go rename cmd/slackdump/internal/{ => diag}/info/ezlogin.go (100%) rename cmd/slackdump/internal/{ => diag}/info/info.go (62%) rename cmd/slackdump/internal/{ => diag}/info/os.go (87%) rename cmd/slackdump/internal/{ => diag}/info/playwright.go (96%) rename cmd/slackdump/internal/{ => diag}/info/rod.go (86%) rename cmd/slackdump/internal/{ => diag}/info/workspace.go (91%) delete mode 100644 cmd/slackdump/internal/info/collector.go diff --git a/cmd/slackdump/internal/diag/diag.go b/cmd/slackdump/internal/diag/diag.go index c68f85cd..2fa3383b 100644 --- a/cmd/slackdump/internal/diag/diag.go +++ b/cmd/slackdump/internal/diag/diag.go @@ -21,11 +21,12 @@ open an issue on Github. PrintFlags: false, RequireAuth: false, Commands: []*base.Command{ - CmdRawOutput, - CmdEzTest, CmdEncrypt, - CmdThread, + CmdEzTest, + CmdInfo, CmdObfuscate, + CmdRawOutput, CmdRecord, + CmdThread, }, } diff --git a/cmd/slackdump/internal/diag/eztest.go b/cmd/slackdump/internal/diag/eztest.go index 31bf2dd7..1ba63b9a 100644 --- a/cmd/slackdump/internal/diag/eztest.go +++ b/cmd/slackdump/internal/diag/eztest.go @@ -33,6 +33,7 @@ be printed and the test will be terminated. } type result struct { + Engine string `json:"engine,omitempty"` HasToken bool `json:"has_token,omitempty"` HasCookies bool `json:"has_cookies,omitempty"` Err *string `json:"error,omitempty"` @@ -72,8 +73,9 @@ func runEzLoginTest(ctx context.Context, cmd *base.Command, args []string) error return err } - token, cookies, err := b.Authenticate(context.Background()) + token, cookies, err := b.Authenticate(ctx) r := result{ + Engine: "playwright", HasToken: len(token) > 0, HasCookies: len(cookies) > 0, } diff --git a/cmd/slackdump/internal/diag/info.go b/cmd/slackdump/internal/diag/info.go new file mode 100644 index 00000000..f02fb94b --- /dev/null +++ b/cmd/slackdump/internal/diag/info.go @@ -0,0 +1,31 @@ +package diag + +import ( + "context" + "encoding/json" + "os" + + "github.com/rusq/slackdump/v2/cmd/slackdump/internal/diag/info" + "github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base" +) + +// CmdInfo is the information command. +var CmdInfo = &base.Command{ + UsageLine: "slackdump tools info", + Short: "show information about slackdump environment", + Run: runInfo, + Long: `# Info Command + +**Info** shows information about Slackdump environment, such as local system paths, etc. +`, +} + +func runInfo(ctx context.Context, cmd *base.Command, args []string) error { + enc := json.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + if err := enc.Encode(info.Collect()); err != nil { + return err + } + + return nil +} diff --git a/cmd/slackdump/internal/info/ezlogin.go b/cmd/slackdump/internal/diag/info/ezlogin.go similarity index 100% rename from cmd/slackdump/internal/info/ezlogin.go rename to cmd/slackdump/internal/diag/info/ezlogin.go diff --git a/cmd/slackdump/internal/info/info.go b/cmd/slackdump/internal/diag/info/info.go similarity index 62% rename from cmd/slackdump/internal/info/info.go rename to cmd/slackdump/internal/diag/info/info.go index 0aae373d..478fecac 100644 --- a/cmd/slackdump/internal/info/info.go +++ b/cmd/slackdump/internal/diag/info/info.go @@ -38,30 +38,58 @@ package info import ( - "context" - "encoding/json" + "io/fs" "os" + "strings" +) + +type SysInfo struct { + OS OSInfo `json:"os"` + Workspace Workspace `json:"workspace"` + Playwright PwInfo `json:"playwright"` + Rod RodInfo `json:"rod"` + EzLogin EZLogin `json:"ez_login"` +} + +func Collect() *SysInfo { + var si = new(SysInfo) + var collectors = []func(){ + si.Workspace.collect, + si.Playwright.collect, + si.Rod.collect, + si.EzLogin.collect, + si.OS.collect, + } + for _, c := range collectors { + c() + } + return si - "github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base" +} + +const ( + home = "$HOME" ) -// CmdInfo is the information command. -var CmdInfo = &base.Command{ - UsageLine: "slackdump info", - Short: "show information about slackdump environment", - Run: runInfo, - Long: `# Info Command - -**Info** shows information about Slackdump environment, such as local system paths, etc. -`, +var replaceFn = strings.NewReplacer(should(os.UserHomeDir()), home).Replace + +func should(v string, err error) string { + if err != nil { + return "$$$ERROR$$$" + } + return v } -func runInfo(ctx context.Context, cmd *base.Command, args []string) error { - enc := json.NewEncoder(os.Stdout) - enc.SetIndent("", " ") - if err := enc.Encode(collect()); err != nil { - return err +func dirnames(des []fs.DirEntry) []string { + var res []string + for _, de := range des { + if de.IsDir() && !strings.HasPrefix(de.Name(), ".") { + res = append(res, de.Name()) + } } + return res +} - return nil +func looser(err error) string { + return "*ERROR: " + replaceFn(err.Error()) + "*" } diff --git a/cmd/slackdump/internal/info/os.go b/cmd/slackdump/internal/diag/info/os.go similarity index 87% rename from cmd/slackdump/internal/info/os.go rename to cmd/slackdump/internal/diag/info/os.go index 4083b908..da69a396 100644 --- a/cmd/slackdump/internal/info/os.go +++ b/cmd/slackdump/internal/diag/info/os.go @@ -7,14 +7,14 @@ import ( "github.com/rusq/slackdump/v2/auth" ) -type osinfo struct { +type OSInfo struct { OS string `json:"os"` Arch string `json:"arch"` IsDocker bool `json:"is_docker"` IsXactive bool `json:"is_x_active"` } -func (inf *osinfo) collect() { +func (inf *OSInfo) collect() { inf.OS = runtime.GOOS inf.Arch = runtime.GOARCH inf.IsDocker = auth.IsDocker() diff --git a/cmd/slackdump/internal/info/playwright.go b/cmd/slackdump/internal/diag/info/playwright.go similarity index 96% rename from cmd/slackdump/internal/info/playwright.go rename to cmd/slackdump/internal/diag/info/playwright.go index 55bfc063..4be8ff62 100644 --- a/cmd/slackdump/internal/info/playwright.go +++ b/cmd/slackdump/internal/diag/info/playwright.go @@ -8,7 +8,7 @@ import ( "github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg" ) -type pwinfo struct { +type PwInfo struct { Path string `json:"path"` InstalledVersions []string `json:"installed_versions"` InstalledBrowsers []string `json:"installed_browsers"` @@ -17,7 +17,7 @@ type pwinfo struct { ScriptPerm string `json:"script_perm"` } -func (inf *pwinfo) collect() { +func (inf *PwInfo) collect() { pwdrv, err := playwright.NewDriver(&playwright.RunOptions{ Browsers: []string{cfg.Browser.String()}, SkipInstallBrowsers: true}, diff --git a/cmd/slackdump/internal/info/rod.go b/cmd/slackdump/internal/diag/info/rod.go similarity index 86% rename from cmd/slackdump/internal/info/rod.go rename to cmd/slackdump/internal/diag/info/rod.go index 48968cde..00b964ba 100644 --- a/cmd/slackdump/internal/info/rod.go +++ b/cmd/slackdump/internal/diag/info/rod.go @@ -6,12 +6,12 @@ import ( "github.com/go-rod/rod/lib/launcher" ) -type rodinfo struct { +type RodInfo struct { Path string `json:"path"` Browsers []string `json:"browsers"` } -func (inf *rodinfo) collect() { +func (inf *RodInfo) collect() { inf.Path = replaceFn(launcher.DefaultBrowserDir) if de, err := os.ReadDir(launcher.DefaultBrowserDir); err == nil { inf.Browsers = dirnames(de) diff --git a/cmd/slackdump/internal/info/workspace.go b/cmd/slackdump/internal/diag/info/workspace.go similarity index 91% rename from cmd/slackdump/internal/info/workspace.go rename to cmd/slackdump/internal/diag/info/workspace.go index 75bbe41f..53d8271f 100644 --- a/cmd/slackdump/internal/info/workspace.go +++ b/cmd/slackdump/internal/diag/info/workspace.go @@ -5,14 +5,14 @@ import ( "github.com/rusq/slackdump/v2/internal/cache" ) -type workspace struct { +type Workspace struct { Path string `json:"path"` TxtExists bool `json:"txt_exists"` HasDefault bool `json:"has_default"` Count int `json:"count"` } -func (inf *workspace) collect() { +func (inf *Workspace) collect() { inf.Path = replaceFn(cfg.LocalCacheDir) inf.Count = -1 // Workspace information diff --git a/cmd/slackdump/internal/info/collector.go b/cmd/slackdump/internal/info/collector.go deleted file mode 100644 index a274e12b..00000000 --- a/cmd/slackdump/internal/info/collector.go +++ /dev/null @@ -1,58 +0,0 @@ -package info - -import ( - "io/fs" - "os" - "strings" -) - -type sysinfo struct { - OS osinfo `json:"os"` - Workspace workspace `json:"workspace"` - Playwright pwinfo `json:"playwright"` - Rod rodinfo `json:"rod"` - EzLogin EZLogin `json:"ez_login"` -} - -func collect() *sysinfo { - var si = new(sysinfo) - var collectors = []func(){ - si.Workspace.collect, - si.Playwright.collect, - si.Rod.collect, - si.EzLogin.collect, - si.OS.collect, - } - for _, c := range collectors { - c() - } - return si - -} - -const ( - home = "$HOME" -) - -var replaceFn = strings.NewReplacer(should(os.UserHomeDir()), home).Replace - -func should(v string, err error) string { - if err != nil { - return "$$$ERROR$$$" - } - return v -} - -func dirnames(des []fs.DirEntry) []string { - var res []string - for _, de := range des { - if de.IsDir() && !strings.HasPrefix(de.Name(), ".") { - res = append(res, de.Name()) - } - } - return res -} - -func looser(err error) string { - return "*ERROR: " + replaceFn(err.Error()) + "*" -} diff --git a/cmd/slackdump/main.go b/cmd/slackdump/main.go index 5a093c62..3c6f8fab 100644 --- a/cmd/slackdump/main.go +++ b/cmd/slackdump/main.go @@ -28,7 +28,6 @@ import ( "github.com/rusq/slackdump/v2/cmd/slackdump/internal/format" "github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base" "github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/help" - "github.com/rusq/slackdump/v2/cmd/slackdump/internal/info" "github.com/rusq/slackdump/v2/cmd/slackdump/internal/list" "github.com/rusq/slackdump/v2/cmd/slackdump/internal/man" "github.com/rusq/slackdump/v2/cmd/slackdump/internal/record" @@ -57,7 +56,6 @@ func init() { diag.CmdDiag, apiconfig.CmdConfig, format.CmdFormat, - info.CmdInfo, CmdVersion, man.WhatsNew,