Skip to content

Commit

Permalink
finish the info
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 13, 2024
1 parent b2e5bd0 commit a6c81d1
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 62 deletions.
58 changes: 58 additions & 0 deletions cmd/slackdump/internal/info/collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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()) + "*"
}
16 changes: 16 additions & 0 deletions cmd/slackdump/internal/info/ezlogin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package info

import (
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v2/internal/cache"
)

type EZLogin struct {
Flags map[string]bool `json:"flags"`
Browser string `json:"browser"`
}

func (inf *EZLogin) collect() {
inf.Flags = cache.EzLoginFlags()
inf.Browser = cfg.Browser.String()
}
59 changes: 1 addition & 58 deletions cmd/slackdump/internal/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,9 @@ package info
import (
"context"
"encoding/json"
"io/fs"
"os"
"strings"

"github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v2/internal/cache"
)

// CmdInfo is the information command.
Expand All @@ -60,65 +56,12 @@ var CmdInfo = &base.Command{
`,
}

type sysinfo struct {
OS osinfo `json:"os"`
Workspace workspace `json:"workspace"`
Playwright pwinfo `json:"playwright"`
Rod rodinfo `json:"rod"`
EzLogin ezlogin `json:"ez_login"`
}

type ezlogin struct {
Flags map[string]bool `json:"flags"`
Browser string `json:"browser"`
}

func (inf *ezlogin) collect() {
inf.Flags = cache.EzLoginFlags()
inf.Browser = cfg.Browser.String()
}

func runInfo(ctx context.Context, cmd *base.Command, args []string) error {
var si sysinfo
var collectors = []func(){
si.Workspace.collect,
si.Playwright.collect,
si.Rod.collect,
si.EzLogin.collect,
si.OS.collect,
}
for _, c := range collectors {
c()
}

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if err := enc.Encode(si); err != nil {
if err := enc.Encode(collect()); err != nil {
return err
}

return nil
}

var homerepl = strings.NewReplacer(should(os.UserHomeDir()), "~").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: " + homerepl(err.Error()) + "*"
}
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/info/playwright.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (inf *pwinfo) collect() {
inf.Path = looser(err)
return
}
inf.Path = homerepl(pwdrv.DriverDirectory)
inf.Script = homerepl(pwdrv.DriverBinaryLocation)
inf.Path = replaceFn(pwdrv.DriverDirectory)
inf.Script = replaceFn(pwdrv.DriverBinaryLocation)
if inf.Script != "" {
if stat, err := os.Stat(pwdrv.DriverBinaryLocation); err == nil {
inf.ScriptPerm = stat.Mode().String()
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/info/rod.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type rodinfo struct {
}

func (inf *rodinfo) collect() {
inf.Path = homerepl(launcher.DefaultBrowserDir)
inf.Path = replaceFn(launcher.DefaultBrowserDir)
if de, err := os.ReadDir(launcher.DefaultBrowserDir); err == nil {
inf.Browsers = dirnames(de)
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/info/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type workspace struct {
}

func (inf *workspace) collect() {
inf.Path = homerepl(cfg.LocalCacheDir)
inf.Path = replaceFn(cfg.LocalCacheDir)
inf.Count = -1
// Workspace information
m, err := cache.NewManager(cfg.LocalCacheDir)
Expand Down

0 comments on commit a6c81d1

Please sign in to comment.