mfer/internal/cli/mfer.go

130 lines
2.6 KiB
Go
Raw Normal View History

2022-12-02 00:31:49 +00:00
package cli
2022-02-02 09:46:25 +00:00
import (
"fmt"
"log"
"os"
"time"
2022-12-02 00:54:01 +00:00
"git.eeqj.de/sneak/mfer/mfer"
"github.com/davecgh/go-spew/spew"
2022-02-02 09:46:25 +00:00
"github.com/pterm/pterm"
"github.com/urfave/cli/v2"
)
2022-12-02 00:31:49 +00:00
type CLIApp struct {
2022-02-02 09:46:25 +00:00
appname string
version string
buildarch string
startupTime time.Time
exitCode int
errorString string
app *cli.App
}
2022-12-02 00:54:01 +00:00
func (mfa *CLIApp) printBanner() {
s, _ := pterm.DefaultBigText.WithLetters(pterm.NewLettersFromString(mfa.appname)).Srender()
2022-02-02 09:46:25 +00:00
pterm.DefaultCenter.Println(s) // Print BigLetters with the default CenterPrinter
}
2022-12-02 00:54:01 +00:00
func (mfa *CLIApp) disableStyling() {
2022-02-02 09:46:25 +00:00
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 = ""
}
2022-12-02 00:54:01 +00:00
func (mfa *CLIApp) run() {
2022-02-02 09:46:25 +00:00
if NO_COLOR {
// shoutout to rob pike who thinks it's juvenile
2022-12-02 00:54:01 +00:00
mfa.disableStyling()
2022-02-02 09:46:25 +00:00
}
2022-12-02 00:54:01 +00:00
mfa.printBanner()
2022-02-02 09:46:25 +00:00
2022-12-02 00:54:01 +00:00
mfa.app = &cli.App{
Name: mfa.appname,
2022-02-02 09:46:25 +00:00
Usage: "Manifest generator",
2022-12-02 00:54:01 +00:00
Version: mfa.version,
2022-02-02 09:46:25 +00:00
EnableBashCompletion: true,
Commands: []*cli.Command{
{
Name: "generate",
Aliases: []string{"gen"},
Usage: "Generate manifest file",
Action: func(c *cli.Context) error {
2022-12-02 00:54:01 +00:00
return mfa.generateManifestOperation(c)
2022-02-02 09:46:25 +00:00
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "input",
Value: ".",
Aliases: []string{"i"},
Usage: "Specify input directory.",
},
&cli.StringFlag{
Name: "output",
Value: "./index.mf",
Aliases: []string{"o"},
Usage: "Specify output filename",
},
},
},
{
Name: "check",
Usage: "Validate files using manifest file",
Action: func(c *cli.Context) error {
2022-12-02 00:54:01 +00:00
return mfa.validateManifestOperation(c)
2022-02-02 09:46:25 +00:00
},
},
},
}
2022-12-02 00:54:01 +00:00
err := mfa.app.Run(os.Args)
2022-02-02 09:46:25 +00:00
if err != nil {
2022-12-02 00:54:01 +00:00
mfa.exitCode = 1
2022-02-02 09:46:25 +00:00
log.Fatal(err)
}
}
2022-12-02 00:54:01 +00:00
func (mfa *CLIApp) validateManifestOperation(c *cli.Context) error {
2022-02-02 09:46:25 +00:00
log.Fatal("unimplemented")
return nil
}
2022-12-02 00:54:01 +00:00
func (mfa *CLIApp) generateManifestOperation(c *cli.Context) error {
fmt.Println("generateManifestOperation()")
fmt.Printf("called with arg: %s\n", c.String("input"))
opts := &mfer.ManifestScanOptions{
IgnoreDotfiles: true,
FollowSymLinks: false,
}
mf, err := mfer.NewFromPath(c.String("input"), opts)
if err != nil {
panic(err)
}
spew.Dump(mf)
2022-12-02 00:31:49 +00:00
/*
mgj, err := NewMFGenerationJobFromFilesystem(c.String("input"))
if err != nil {
log.Fatal(err)
return err
}
mgj.scanForFiles()
//mgj.outputFile = c.String("output")
2022-02-02 09:46:25 +00:00
2022-12-02 00:31:49 +00:00
*/
2022-02-02 09:46:25 +00:00
return nil
}