bedrocktool/cmd/bedrocktool/main.go

153 lines
3.5 KiB
Go
Raw Normal View History

2022-02-12 22:27:49 +00:00
package main
import (
"bufio"
"context"
2022-02-12 22:27:49 +00:00
"flag"
"fmt"
"os"
"os/signal"
"runtime/debug"
"syscall"
2022-02-12 22:27:49 +00:00
2022-09-04 14:53:21 +00:00
"github.com/bedrock-tool/bedrocktool/utils"
_ "github.com/bedrock-tool/bedrocktool/subcommands"
_ "github.com/bedrock-tool/bedrocktool/subcommands/skins"
_ "github.com/bedrock-tool/bedrocktool/subcommands/world"
2022-09-04 14:26:32 +00:00
"github.com/google/subcommands"
2022-09-04 00:24:58 +00:00
"github.com/sirupsen/logrus"
2022-02-12 22:27:49 +00:00
)
2022-02-27 10:08:19 +00:00
func main() {
2022-09-06 13:24:08 +00:00
defer func() {
if err := recover(); err != nil {
logrus.Errorf("Fatal Error occurred.")
println("")
println("--COPY FROM HERE--")
logrus.Infof("Version: %s", utils.Version)
logrus.Infof("Cmdline: %s", os.Args)
logrus.Errorf("Error: %s", err)
fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
2022-09-06 13:24:08 +00:00
println("--END COPY HERE--")
println("")
println("if you want to report this error, please open an issue at")
println("https://github.com/bedrock-tool/bedrocktool/issues")
println("And attach the error info, describe what you did to get this error.")
println("Thanks!\n")
2022-09-08 08:39:46 +00:00
if utils.G_interactive {
input := bufio.NewScanner(os.Stdin)
input.Scan()
}
2022-09-06 13:24:08 +00:00
os.Exit(1)
}
}()
2022-09-04 00:24:58 +00:00
logrus.SetLevel(logrus.DebugLevel)
2022-09-05 21:57:11 +00:00
if utils.Version != "" {
logrus.Infof("bedrocktool version: %s", utils.Version)
}
newVersion, err := utils.Updater.UpdateAvailable()
if err != nil {
logrus.Error(err)
}
2022-09-05 22:27:05 +00:00
if newVersion != "" {
2022-09-05 21:57:11 +00:00
logrus.Infof("Update Available: %s", newVersion)
2022-09-04 00:24:58 +00:00
}
2022-08-21 16:28:39 +00:00
ctx, cancel := context.WithCancel(context.Background())
2022-09-04 14:26:32 +00:00
flag.BoolVar(&utils.G_debug, "debug", false, "debug mode")
flag.BoolVar(&utils.G_preload_packs, "preload", false, "preload resourcepacks for proxy")
enable_dns := flag.Bool("dns", false, "enable dns server for consoles")
2022-09-05 21:57:11 +00:00
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.ImportantFlag("debug")
subcommands.ImportantFlag("dns")
subcommands.ImportantFlag("preload")
2022-08-15 00:29:01 +00:00
subcommands.HelpCommand()
{ // interactive input
if len(os.Args) < 2 {
select {
case <-ctx.Done():
return
default:
fmt.Println("Available commands:")
2022-09-04 14:26:32 +00:00
for name, desc := range utils.ValidCMDs {
fmt.Printf("\t%s\t%s\n", name, desc)
}
fmt.Printf("Use '%s <command>' to run a command\n", os.Args[0])
cmd, cancelled := utils.User_input(ctx, "Input Command: ")
if cancelled {
return
}
os.Args = append(os.Args, cmd)
2022-09-08 08:39:46 +00:00
utils.G_interactive = true
}
}
}
flag.Parse()
if *enable_dns {
2022-09-04 14:26:32 +00:00
utils.InitDNS()
}
2022-08-11 20:22:39 +00:00
// exit cleanup
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
println("cancelling")
cancel()
}()
2022-02-20 11:39:02 +00:00
2022-10-14 22:42:43 +00:00
subcommands.Execute(ctx)
2022-09-08 08:39:46 +00:00
if utils.G_interactive {
logrus.Info("Press Enter to exit.")
input := bufio.NewScanner(os.Stdin)
input.Scan()
}
2022-07-29 16:12:06 +00:00
}
type TransCMD struct {
auth bool
}
func (*TransCMD) Name() string { return "trans" }
func (*TransCMD) Synopsis() string { return "" }
func (c *TransCMD) SetFlags(f *flag.FlagSet) {
f.BoolVar(&c.auth, "auth", false, "if it should login to xbox")
}
2022-09-04 13:24:55 +00:00
func (c *TransCMD) Usage() string {
return c.Name() + ": " + c.Synopsis() + "\n"
}
func (c *TransCMD) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
const (
BLACK_FG = "\033[30m"
BOLD = "\033[1m"
BLUE = "\033[46m"
PINK = "\033[45m"
WHITE = "\033[47m"
RESET = "\033[0m"
)
if c.auth {
2022-09-04 14:26:32 +00:00
utils.GetTokenSource()
}
fmt.Println(BLACK_FG + BOLD + BLUE + " Trans " + PINK + " Rights " + WHITE + " Are " + PINK + " Human " + BLUE + " Rights " + RESET)
return 0
}
2022-09-04 13:24:55 +00:00
func init() {
2022-09-04 14:26:32 +00:00
utils.RegisterCommand(&TransCMD{})
}