latest
This commit is contained in:
84
src/app.go
84
src/app.go
@@ -1,89 +1,25 @@
|
||||
package mfer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func printBanner(Appname, Version, Buildarch string) {
|
||||
s, _ := pterm.DefaultBigText.WithLetters(pterm.NewLettersFromString(Appname)).Srender()
|
||||
pterm.DefaultCenter.Println(s) // Print BigLetters with the default CenterPrinter
|
||||
}
|
||||
|
||||
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 = ""
|
||||
}
|
||||
var NO_COLOR bool
|
||||
|
||||
func init() {
|
||||
NO_COLOR = false
|
||||
if _, exists := os.LookupEnv("NO_COLOR"); exists {
|
||||
disableStyling()
|
||||
NO_COLOR = true
|
||||
}
|
||||
}
|
||||
|
||||
func Run(Appname, Version, Buildarch string) int {
|
||||
printBanner(Appname, Version, Buildarch)
|
||||
app := &cli.App{
|
||||
Name: Appname,
|
||||
Usage: "Manifest generator",
|
||||
Version: Version,
|
||||
EnableBashCompletion: true,
|
||||
Commands: []*cli.Command{
|
||||
{
|
||||
Name: "generate",
|
||||
Aliases: []string{"gen"},
|
||||
Usage: "Generate manifest file",
|
||||
Action: func(c *cli.Context) error {
|
||||
return generateManifest(c.String("input"), c.String("output"))
|
||||
},
|
||||
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",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
m := &mfer{}
|
||||
m.appname = Appname
|
||||
m.version = Version
|
||||
m.buildarch = Buildarch
|
||||
m.exitCode = 0
|
||||
|
||||
err := app.Run(os.Args)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func generateManifest(path, outputFilename string) error {
|
||||
fmt.Println("generateManifest()")
|
||||
walkErr := filepath.Walk(path, func(itemPath string, info os.FileInfo, err error) error {
|
||||
fmt.Println(itemPath)
|
||||
return nil
|
||||
})
|
||||
|
||||
if walkErr != nil {
|
||||
log.Fatal(walkErr)
|
||||
return walkErr
|
||||
}
|
||||
|
||||
return nil
|
||||
m.run()
|
||||
return m.exitCode
|
||||
}
|
||||
|
||||
69
src/manifest.go
Normal file
69
src/manifest.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package mfer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
type MFGenerationJob struct {
|
||||
sourcePath string
|
||||
outputFile string
|
||||
innerpb *MFFileInner
|
||||
outerpb *MFFile
|
||||
fileCount int64
|
||||
totalSize int64
|
||||
afs afero.Fs
|
||||
}
|
||||
|
||||
func NewMFGenerationJobFromFilesystem(sourcePath string) (*MFGenerationJob, error) {
|
||||
afs := afero.NewOsFs()
|
||||
exists, err := afero.DirExists(afs, sourcePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("source directory does not exist")
|
||||
}
|
||||
|
||||
mgj := MFGenerationJob{}
|
||||
mgj.afs = afs
|
||||
mgj.sourcePath = sourcePath
|
||||
|
||||
return &mgj, nil
|
||||
}
|
||||
|
||||
func (m *MFGenerationJob) scanForFiles() error {
|
||||
|
||||
m.innerpb = &MFFileInner{}
|
||||
m.innerpb.Version = MFFileInner_ONE
|
||||
|
||||
walkErr := filepath.Walk(m.sourcePath, func(itemPath string, info os.FileInfo, err error) error {
|
||||
|
||||
// we do not include the manifest file in the manifest
|
||||
if itemPath == "index.mf" {
|
||||
return nil
|
||||
}
|
||||
|
||||
fpi := MFFilePath{}
|
||||
fpi.Path = itemPath
|
||||
fpi.Size = info.Size()
|
||||
m.innerpb.Files = append(m.innerpb.Files, &fpi)
|
||||
m.fileCount++
|
||||
m.totalSize += fpi.Size
|
||||
return nil
|
||||
})
|
||||
|
||||
if walkErr != nil {
|
||||
log.Fatal(walkErr)
|
||||
return walkErr
|
||||
}
|
||||
|
||||
fmt.Printf("%#v\n", m.innerpb)
|
||||
fmt.Printf("filecount = %#v\n", m.fileCount)
|
||||
fmt.Printf("totalsize = %#v\n", m.totalSize)
|
||||
return nil
|
||||
}
|
||||
@@ -18,7 +18,7 @@ message MFFile {
|
||||
bytes innerMessage = 102;
|
||||
// these are used solely to detect corruption/truncation
|
||||
// and not for cryptographic integrity.
|
||||
uint64 size = 103;
|
||||
int64 size = 103;
|
||||
bytes sha256 = 104;
|
||||
|
||||
// 2xx for optional manifest root attributes
|
||||
@@ -36,7 +36,7 @@ message MFFile {
|
||||
message MFFilePath {
|
||||
// required attributes:
|
||||
string path = 101;
|
||||
uint64 size = 102;
|
||||
int64 size = 102;
|
||||
|
||||
// gotta have at least one:
|
||||
repeated MFFileChecksum hashes = 201;
|
||||
@@ -63,7 +63,7 @@ message MFFileInner {
|
||||
Version version = 101;
|
||||
|
||||
// required manifest attributes:
|
||||
uint64 fileCount = 102; //FIXME is this necessary?
|
||||
int64 fileCount = 102; //FIXME is this necessary?
|
||||
repeated MFFilePath files = 103;
|
||||
|
||||
// optional manifest attributes 2xx:
|
||||
|
||||
110
src/mfer.go
Normal file
110
src/mfer.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package mfer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
type mfer struct {
|
||||
appname string
|
||||
version string
|
||||
buildarch string
|
||||
startupTime time.Time
|
||||
exitCode int
|
||||
errorString string
|
||||
app *cli.App
|
||||
}
|
||||
|
||||
func (m *mfer) printBanner() {
|
||||
s, _ := pterm.DefaultBigText.WithLetters(pterm.NewLettersFromString(m.appname)).Srender()
|
||||
pterm.DefaultCenter.Println(s) // Print BigLetters with the default CenterPrinter
|
||||
}
|
||||
|
||||
func (m *mfer) 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 (m *mfer) run() {
|
||||
|
||||
if NO_COLOR {
|
||||
// shoutout to rob pike who thinks it's juvenile
|
||||
m.disableStyling()
|
||||
}
|
||||
|
||||
m.printBanner()
|
||||
|
||||
m.app = &cli.App{
|
||||
Name: m.appname,
|
||||
Usage: "Manifest generator",
|
||||
Version: m.version,
|
||||
EnableBashCompletion: true,
|
||||
Commands: []*cli.Command{
|
||||
{
|
||||
Name: "generate",
|
||||
Aliases: []string{"gen"},
|
||||
Usage: "Generate manifest file",
|
||||
Action: func(c *cli.Context) error {
|
||||
return m.generateManifestOperation(c)
|
||||
},
|
||||
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 {
|
||||
return m.validateManifestOperation(c)
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := m.app.Run(os.Args)
|
||||
|
||||
if err != nil {
|
||||
m.exitCode = 1
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *mfer) validateManifestOperation(c *cli.Context) error {
|
||||
log.Fatal("unimplemented")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mfer) generateManifestOperation(c *cli.Context) error {
|
||||
fmt.Println("generateManifest()")
|
||||
mgj, err := NewMFGenerationJobFromFilesystem(c.String("input"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
mgj.scanForFiles()
|
||||
//mgj.outputFile = c.String("output")
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user