next (#5)
All checks were successful
continuous-integration/drone/push Build is passing

Co-authored-by: sneak <sneak@sneak.berlin>
Reviewed-on: #5
This commit was merged in pull request #5.
This commit is contained in:
2022-12-09 00:02:33 +00:00
parent 7a8a1b4a4a
commit 7df558d8d0
29 changed files with 923 additions and 220 deletions

15
internal/bork/error.go Normal file
View File

@@ -0,0 +1,15 @@
package bork
import (
"errors"
"fmt"
)
var (
ErrMissingMagic = errors.New("missing magic bytes in file")
ErrFileTruncated = errors.New("file/stream is truncated abnormally")
)
func Newf(format string, args ...interface{}) error {
return fmt.Errorf(format, args...)
}

View File

@@ -0,0 +1,11 @@
package bork
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuild(t *testing.T) {
assert.NotNil(t, ErrMissingMagic)
}

View File

@@ -3,12 +3,11 @@ package cli
import (
"errors"
log "github.com/visionmedia/go-cli-log"
"github.com/apex/log"
"github.com/urfave/cli/v2"
)
func (mfa *CLIApp) checkManifestOperation(c *cli.Context) error {
log.Error(errors.New("unimplemented"))
log.WithError(errors.New("unimplemented"))
return nil
}

View File

@@ -0,0 +1,12 @@
package cli
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuild(t *testing.T) {
m := &CLIApp{}
assert.NotNil(t, m)
}

View File

@@ -1,12 +1,12 @@
package cli
import (
"fmt"
"github.com/apex/log"
"github.com/urfave/cli/v2"
)
func (mfa *CLIApp) fetchManifestOperation(c *cli.Context) error {
fmt.Println("fetchManifestOperation()")
return nil
log.Debugf("fetchManifestOperation()")
panic("not implemented")
return nil //nolint
}

View File

@@ -1,36 +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(c *cli.Context) error {
fmt.Println("generateManifestOperation()")
myArgs := c.Args()
spew.Dump(myArgs)
fmt.Printf("%#v\n", c.Args().First())
if c.Args().Len() > 0 {
fmt.Printf("%#v\n", c.Args().Get(1))
}
// fmt.Printf("called with arg: %s\n", c.String("input"))
func (mfa *CLIApp) generateManifestOperation(ctx *cli.Context) error {
log.Debug("generateManifestOperation()")
myArgs := ctx.Args()
log.Dump(myArgs)
opts := &mfer.ManifestScanOptions{
IgnoreDotfiles: c.Bool("IgnoreDotfiles"),
FollowSymLinks: c.Bool("FollowSymLinks"),
IgnoreDotfiles: ctx.Bool("IgnoreDotfiles"),
FollowSymLinks: ctx.Bool("FollowSymLinks"),
}
// FIXME add command flags for ignoring dotfiles and following symlinks
mf, err := mfer.NewFromPath(c.String("input"), opts)
paths := make([]string, ctx.Args().Len()-1)
for i := 0; i < ctx.Args().Len(); 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.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

@@ -5,9 +5,7 @@ import (
"os"
"time"
log "github.com/visionmedia/go-cli-log"
"github.com/pterm/pterm"
"git.eeqj.de/sneak/mfer/internal/log"
"github.com/urfave/cli/v2"
)
@@ -20,34 +18,47 @@ type CLIApp struct {
app *cli.App
}
func (mfa *CLIApp) printBanner() {
s, _ := pterm.DefaultBigText.WithLetters(pterm.NewLettersFromString(mfa.appname)).Srender()
pterm.DefaultCenter.Println(s) // Print BigLetters with the default CenterPrinter
}
const banner = ` ___ ___ ___ ___
/__/\ / /\ / /\ / /\
| |::\ / /:/_ / /:/_ / /::\
| |:|:\ / /:/ /\ / /:/ /\ / /:/\:\
__|__|:|\:\ / /:/ /:/ / /:/ /:/_ / /:/~/:/
/__/::::| \:\ /__/:/ /:/ /__/:/ /:/ /\ /__/:/ /:/___
\ \:\~~\__\/ \ \:\/:/ \ \:\/:/ /:/ \ \:\/:::::/
\ \:\ \ \::/ \ \::/ /:/ \ \::/~~~~
\ \:\ \ \:\ \ \:\/:/ \ \:\
\ \:\ \ \:\ \ \::/ \ \:\
\__\/ \__\/ \__\/ \__\/`
func (mfa *CLIApp) disableStyling() {
pterm.DisableColor()
pterm.DisableStyling()
pterm.Debug.Prefix.Text = ""
pterm.Info.Prefix.Text = ""
pterm.Success.Prefix.Text = ""
pterm.Warning.Prefix.Text = ""
pterm.Error.Prefix.Text = ""
pterm.Fatal.Prefix.Text = ""
func (mfa *CLIApp) printBanner() {
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()
if NO_COLOR {
// shoutout to rob pike who thinks it's juvenile
mfa.disableStyling()
log.DisableStyling()
}
log.Init()
var verbosity int
mfa.app = &cli.App{
Name: mfa.appname,
Usage: "Manifest generator",
@@ -58,6 +69,7 @@ func (mfa *CLIApp) run() {
Name: "verbose",
Usage: "Verbosity level",
Aliases: []string{"v"},
Count: &verbosity,
},
&cli.BoolFlag{
Name: "quiet",
@@ -74,6 +86,7 @@ func (mfa *CLIApp) run() {
if !c.Bool("quiet") {
mfa.printBanner()
}
mfa.setVerbosity(verbosity)
return mfa.generateManifestOperation(c)
},
Flags: []cli.Flag{
@@ -87,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",
@@ -109,6 +115,7 @@ func (mfa *CLIApp) run() {
if !c.Bool("quiet") {
mfa.printBanner()
}
mfa.setVerbosity(verbosity)
return mfa.checkManifestOperation(c)
},
},
@@ -127,6 +134,7 @@ func (mfa *CLIApp) run() {
if !c.Bool("quiet") {
mfa.printBanner()
}
mfa.setVerbosity(verbosity)
return mfa.fetchManifestOperation(c)
},
},
@@ -137,6 +145,6 @@ func (mfa *CLIApp) run() {
err := mfa.app.Run(os.Args)
if err != nil {
mfa.exitCode = 1
log.Error(err)
log.WithError(err).Debugf("exiting")
}
}

89
internal/log/log.go Normal file
View File

@@ -0,0 +1,89 @@
package log
import (
"fmt"
"runtime"
"github.com/apex/log"
acli "github.com/apex/log/handlers/cli"
"github.com/davecgh/go-spew/spew"
"github.com/pterm/pterm"
)
type Level = log.Level
func DisableStyling() {
pterm.DisableColor()
pterm.DisableStyling()
pterm.Debug.Prefix.Text = ""
pterm.Info.Prefix.Text = ""
pterm.Success.Prefix.Text = ""
pterm.Warning.Prefix.Text = ""
pterm.Error.Prefix.Text = ""
pterm.Fatal.Prefix.Text = ""
}
func Init() {
log.SetHandler(acli.Default)
log.SetLevel(log.InfoLevel)
}
func Debugf(format string, args ...interface{}) {
DebugReal(fmt.Sprintf(format, args...), 2)
}
func Debug(arg string) {
DebugReal(arg, 2)
}
func DebugReal(arg string, cs int) {
_, callerFile, callerLine, ok := runtime.Caller(cs)
if !ok {
return
}
tag := fmt.Sprintf("%s:%d: ", callerFile, callerLine)
log.Debug(tag + arg)
}
func Dump(args ...interface{}) {
DebugReal(spew.Sdump(args...), 2)
}
func EnableDebugLogging() {
SetLevel(log.DebugLevel)
}
func VerbosityStepsToLogLevel(l int) log.Level {
switch l {
case 1:
return log.WarnLevel
case 2:
return log.InfoLevel
case 3:
return log.DebugLevel
}
return log.ErrorLevel
}
func SetLevelFromVerbosity(l int) {
SetLevel(VerbosityStepsToLogLevel(l))
}
func SetLevel(arg log.Level) {
log.SetLevel(arg)
}
func GetLogger() *log.Logger {
if logger, ok := log.Log.(*log.Logger); ok {
return logger
}
panic("unable to get logger")
}
func GetLevel() log.Level {
return GetLogger().Level
}
func WithError(e error) *log.Entry {
return GetLogger().WithError(e)
}

12
internal/log/log_test.go Normal file
View File

@@ -0,0 +1,12 @@
package log
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuild(t *testing.T) {
Init()
assert.True(t, true)
}