Skip to content

Commit

Permalink
move info tools
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 13, 2024
1 parent a6c81d1 commit ab465ef
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 90 deletions.
7 changes: 4 additions & 3 deletions cmd/slackdump/internal/diag/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
4 changes: 3 additions & 1 deletion cmd/slackdump/internal/diag/eztest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
}
Expand Down
31 changes: 31 additions & 0 deletions cmd/slackdump/internal/diag/info.go
Original file line number Diff line number Diff line change
@@ -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
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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()) + "*"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 0 additions & 58 deletions cmd/slackdump/internal/info/collector.go

This file was deleted.

2 changes: 0 additions & 2 deletions cmd/slackdump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -57,7 +56,6 @@ func init() {
diag.CmdDiag,
apiconfig.CmdConfig,
format.CmdFormat,
info.CmdInfo,
CmdVersion,

man.WhatsNew,
Expand Down

0 comments on commit ab465ef

Please sign in to comment.