mfer/internal/cli/mfer.go

143 lines
3.1 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"
"os"
"time"
2022-12-03 23:18:47 +00:00
log "github.com/visionmedia/go-cli-log"
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
2022-12-04 00:33:24 +00:00
gitrev string
2022-02-02 09:46:25 +00:00
startupTime time.Time
exitCode int
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-04 00:33:24 +00:00
func (mfa *CLIApp) VersionString() string {
return fmt.Sprintf("%s (%s)", mfa.version, mfa.gitrev)
}
2022-12-02 00:54:01 +00:00
func (mfa *CLIApp) run() {
2022-12-04 00:33:24 +00:00
mfa.startupTime = time.Now()
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.app = &cli.App{
Name: mfa.appname,
2022-02-02 09:46:25 +00:00
Usage: "Manifest generator",
2022-12-04 00:33:24 +00:00
Version: mfa.VersionString(),
2022-02-02 09:46:25 +00:00
EnableBashCompletion: true,
2022-12-04 00:33:24 +00:00
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Usage: "Verbosity level",
Aliases: []string{"v"},
},
&cli.BoolFlag{
Name: "quiet",
Usage: "don't produce output except on error",
Aliases: []string{"q"},
},
},
2022-02-02 09:46:25 +00:00
Commands: []*cli.Command{
{
Name: "generate",
Aliases: []string{"gen"},
Usage: "Generate manifest file",
Action: func(c *cli.Context) error {
2022-12-04 00:33:24 +00:00
if !c.Bool("quiet") {
mfa.printBanner()
}
2022-12-02 00:54:01 +00:00
return mfa.generateManifestOperation(c)
2022-02-02 09:46:25 +00:00
},
Flags: []cli.Flag{
2022-12-04 00:33:24 +00:00
&cli.BoolFlag{
Name: "FollowSymLinks",
Aliases: []string{"follow-symlinks"},
Usage: "Resolve encountered symlinks",
},
&cli.BoolFlag{
Name: "IgnoreDotfiles",
Aliases: []string{"ignore-dotfiles"},
Usage: "Ignore any dot (hidden) files encountered",
},
2022-12-04 00:48:23 +00:00
// FIXME this should be a positional arg
2022-02-02 09:46:25 +00:00
&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-04 00:33:24 +00:00
if !c.Bool("quiet") {
mfa.printBanner()
}
return mfa.checkManifestOperation(c)
},
},
{
Name: "version",
Usage: "Show version",
Action: func(c *cli.Context) error {
fmt.Printf("%s\n", mfa.VersionString())
return nil
2022-02-02 09:46:25 +00:00
},
},
2022-12-04 00:48:23 +00:00
{
Name: "fetch",
Usage: "fetch manifest and referenced files",
Action: func(c *cli.Context) error {
if !c.Bool("quiet") {
mfa.printBanner()
}
return mfa.fetchManifestOperation(c)
},
},
2022-02-02 09:46:25 +00:00
},
}
2022-12-04 00:33:24 +00:00
mfa.app.HideVersion = true
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-12-03 23:18:47 +00:00
log.Error(err)
2022-02-02 09:46:25 +00:00
}
}