eternity/cmd/main.go

116 lines
3.6 KiB
Go

package main
import (
"eternity/common"
"eternity/lib"
"fmt"
"math/big"
"os"
"os/signal"
"path/filepath"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: eternity <build/generate/convert/repo/package>")
os.Exit(1)
}
logger := &common.DefaultLogger
for i, arg := range os.Args {
if arg == "--help" || arg == "-h" || (i != 1 && arg == "help") {
logger.LogFunc(lib.Log{
Level: "FATAL",
Content: "To ask for help, use 'eternity help <build/generate/convert/repo/package>, not --help, -h, or eternity <command> help.",
})
}
}
switch os.Args[1] {
case "build":
inMemory := true
if len(os.Args) > 2 {
if os.Args[2] == "-d" || os.Args[2] == "--disk" {
inMemory = false
}
}
privateKey, err, vagueError := common.LoadPrivateKey(logger)
if err != nil || vagueError != nil {
common.LoadPrivateKeyHandleError(err, vagueError, logger)
}
config, err, vagueError := lib.ParseConfig("eternity.json", logger)
if err != nil || vagueError != nil {
common.EternityJsonHandleError(err, vagueError, logger)
} else {
var tempDir string
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
fmt.Println("\nCaught", sig, "signal, exiting...")
if tempDir != "" {
common.BuildEPKRemoveTemporaryDirectory(tempDir, logger)
}
os.Exit(1)
}
}()
var size big.Int
size, tempDir, err, vagueError = lib.BuildEPK("./", inMemory, config.Build, logger)
if err != nil || vagueError != nil {
common.BuildEPKHandleError(tempDir, err, vagueError, logger)
} else {
filePath := filepath.Join("./", config.Metadata.Name+"-"+config.Metadata.Version.String())
config.Metadata.DecompressedSize = size
err, vagueError := lib.PackageEPK(config.Metadata, config.Build, tempDir, filePath+".epk", privateKey, logger)
if err != nil || vagueError != nil {
common.PackageEPKHandleError(err, vagueError, logger)
} else {
common.BuildEPKRemoveTemporaryDirectory(tempDir, logger)
logger.LogFunc(lib.Log{
Level: "INFO",
Content: "Successfully built and packaged EPK at " + filePath + ".epk",
})
}
}
}
case "repo":
if len(os.Args) < 4 {
fmt.Println("Usage: eternity repo <build/generate> <directory>")
fmt.Println("See 'eternity help repo' for more information.")
os.Exit(1)
}
privateKey, err, vagueError := common.LoadPrivateKey(logger)
if err != nil || vagueError != nil {
common.LoadPrivateKeyHandleError(err, vagueError, logger)
}
switch os.Args[2] {
case "generate":
err, vagueError := lib.GenerateRepository(os.Args[3], privateKey, logger)
if err != nil || vagueError != nil {
common.GenerateRepositoryHandleError(err, vagueError, logger)
}
}
case "help":
if len(os.Args) > 2 {
switch os.Args[2] {
case "build":
fmt.Println("Usage: eternity build [-d/--disk]")
fmt.Println(" -d, --disk Build EPK on disk instead of in memory. This is useful for large EPKs.")
case "convert":
fmt.Println("Usage: eternity convert </path/to/rpm> [</path/to/output/epk>]")
case "repo":
fmt.Println("Usage: eternity repo <build/generate> <directory>")
fmt.Println(" build Build a repository from a directory containing EPK project directories.")
fmt.Println(" generate Generate a repository from a directory containing EPKs.")
case "package":
fmt.Println("Usage: eternity package <epk>")
default:
fmt.Println("Usage: eternity help <build/generate/convert/repo/package>")
}
} else {
fmt.Println("Usage: eternity help <build/generate/convert/repo/package>")
}
}
}