latest - does not work
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
2022-12-06 06:29:01 +04:00
parent ec3e7c23eb
commit a2bf7ee607
9 changed files with 203 additions and 66 deletions

View File

@@ -1,40 +1,54 @@
package cli
import (
"fmt"
"bytes"
"path/filepath"
"git.eeqj.de/sneak/mfer/internal/log"
"git.eeqj.de/sneak/mfer/mfer"
"github.com/davecgh/go-spew/spew"
"github.com/urfave/cli/v2"
)
func (mfa *CLIApp) generateManifestOperation(ctx *cli.Context) error {
fmt.Println("generateManifestOperation()")
log.Debug("generateManifestOperation()")
myArgs := ctx.Args()
spew.Dump(myArgs)
fmt.Printf("%#v\n", ctx.Args().First())
if ctx.Args().Len() > 0 {
fmt.Printf("%#v\n", ctx.Args().Get(1))
}
// fmt.Printf("called with arg: %s\n", c.String("input"))
log.Dump(myArgs)
opts := &mfer.ManifestScanOptions{
IgnoreDotfiles: ctx.Bool("IgnoreDotfiles"),
FollowSymLinks: ctx.Bool("FollowSymLinks"),
}
paths := make([]string, ctx.Args().Len())
paths := make([]string, ctx.Args().Len()-1)
for i := 0; i < ctx.Args().Len(); i++ {
paths = append(paths, ctx.Args().Get(i))
ap, err := filepath.Abs(ctx.Args().Get(i))
if err != nil {
return err
}
log.Dump(ap)
paths = append(paths, ap)
}
mf, err := mfer.NewFromPaths(opts, paths...)
if err != nil {
panic(err)
}
mf.WithContext(ctx)
mf.WithContext(ctx.Context)
spew.Dump(mf)
log.Dump(mf)
err = mf.Scan()
if err != nil {
return err
}
buf := new(bytes.Buffer)
err = mf.WriteTo(buf)
if err != nil {
return err
}
dat := buf.Bytes()
log.Dump(dat)
return nil
}

View File

@@ -6,7 +6,6 @@ import (
"time"
"git.eeqj.de/sneak/mfer/internal/log"
"github.com/pterm/pterm"
"github.com/urfave/cli/v2"
)
@@ -19,15 +18,35 @@ type CLIApp struct {
app *cli.App
}
const banner = ` ___ ___ ___ ___
/__/\ / /\ / /\ / /\
| |::\ / /:/_ / /:/_ / /::\
| |:|:\ / /:/ /\ / /:/ /\ / /:/\:\
__|__|:|\:\ / /:/ /:/ / /:/ /:/_ / /:/~/:/
/__/::::| \:\ /__/:/ /:/ /__/:/ /:/ /\ /__/:/ /:/___
\ \:\~~\__\/ \ \:\/:/ \ \:\/:/ /:/ \ \:\/:::::/
\ \:\ \ \::/ \ \::/ /:/ \ \::/~~~~
\ \:\ \ \:\ \ \:\/:/ \ \:\
\ \:\ \ \:\ \ \::/ \ \:\
\__\/ \__\/ \__\/ \__\/`
func (mfa *CLIApp) printBanner() {
s, _ := pterm.DefaultBigText.WithLetters(pterm.NewLettersFromString(mfa.appname)).Srender()
pterm.DefaultCenter.Println(s) // Print BigLetters with the default CenterPrinter
fmt.Println(banner)
}
func (mfa *CLIApp) VersionString() string {
return fmt.Sprintf("%s (%s)", mfa.version, mfa.gitrev)
}
func (mfa *CLIApp) setVerbosity(v int) {
_, present := os.LookupEnv("MFER_DEBUG")
if present {
log.EnableDebugLogging()
} else {
log.SetLevelFromVerbosity(v)
}
}
func (mfa *CLIApp) run() {
mfa.startupTime = time.Now()
@@ -38,6 +57,8 @@ func (mfa *CLIApp) run() {
log.Init()
var verbosity int
mfa.app = &cli.App{
Name: mfa.appname,
Usage: "Manifest generator",
@@ -48,6 +69,7 @@ func (mfa *CLIApp) run() {
Name: "verbose",
Usage: "Verbosity level",
Aliases: []string{"v"},
Count: &verbosity,
},
&cli.BoolFlag{
Name: "quiet",
@@ -55,12 +77,6 @@ func (mfa *CLIApp) run() {
Aliases: []string{"q"},
},
},
Action: func(c *cli.Context) error {
if c.Bool("verbose") {
log.IncreaseLevel()
}
return nil
},
Commands: []*cli.Command{
{
Name: "generate",
@@ -70,6 +86,7 @@ func (mfa *CLIApp) run() {
if !c.Bool("quiet") {
mfa.printBanner()
}
mfa.setVerbosity(verbosity)
return mfa.generateManifestOperation(c)
},
Flags: []cli.Flag{
@@ -83,13 +100,6 @@ func (mfa *CLIApp) run() {
Aliases: []string{"ignore-dotfiles"},
Usage: "Ignore any dot (hidden) files encountered",
},
// FIXME this should be a positional arg
&cli.StringFlag{
Name: "input",
Value: ".",
Aliases: []string{"i"},
Usage: "Specify input directory.",
},
&cli.StringFlag{
Name: "output",
Value: "./index.mf",
@@ -105,6 +115,7 @@ func (mfa *CLIApp) run() {
if !c.Bool("quiet") {
mfa.printBanner()
}
mfa.setVerbosity(verbosity)
return mfa.checkManifestOperation(c)
},
},
@@ -123,6 +134,7 @@ func (mfa *CLIApp) run() {
if !c.Bool("quiet") {
mfa.printBanner()
}
mfa.setVerbosity(verbosity)
return mfa.fetchManifestOperation(c)
},
},

32
internal/cli/misc.go Normal file
View File

@@ -0,0 +1,32 @@
package cli
import "fmt"
// FIXME make this write to a bytes.Buffer with fprintf
func dumpByteSlice(b []byte) {
var a [16]byte
n := (len(b) + 15) &^ 15
for i := 0; i < n; i++ {
if i%16 == 0 {
fmt.Printf("%4d", i)
}
if i%8 == 0 {
fmt.Print(" ")
}
if i < len(b) {
fmt.Printf(" %02X", b[i])
} else {
fmt.Print(" ")
}
if i >= len(b) {
a[i%16] = ' '
} else if b[i] < 32 || b[i] > 126 {
a[i%16] = '.'
} else {
a[i%16] = b[i]
}
if i%16 == 15 {
fmt.Printf(" %s\n", string(a[:]))
}
}
}