From e0cc381b14b991898d519a43807e74398b2a2b4f Mon Sep 17 00:00:00 2001 From: hub Date: Sat, 10 Sep 2022 01:15:34 +0200 Subject: [PATCH] flag cleanup --- cmd/ping.go | 2 +- cmd/root.go | 52 +++++++++++++++++++++++++++++++++++--------------- cmd/server.go | 18 ++++++++++------- cmd/version.go | 12 ++---------- 4 files changed, 51 insertions(+), 33 deletions(-) diff --git a/cmd/ping.go b/cmd/ping.go index ae951e8..7fcc964 100644 --- a/cmd/ping.go +++ b/cmd/ping.go @@ -8,7 +8,6 @@ import ( ) func init() { - pingCmd.Flags().String("host", "api.modrinth.com", "Labrinth host") rootCmd.AddCommand(pingCmd) } @@ -22,6 +21,7 @@ var pingCmd = &cobra.Command{ log.Fatalln(err) } + log.Println("Connecting to:", host) client := modrinth.NewClient(host) info, err := client.LabrinthInfo() if err != nil { diff --git a/cmd/root.go b/cmd/root.go index dba92e1..187b52c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/nothub/mrpack-install/buildinfo" modrinth "github.com/nothub/mrpack-install/modrinth/api" "github.com/nothub/mrpack-install/modrinth/mrpack" "github.com/nothub/mrpack-install/requester" @@ -16,14 +17,18 @@ import ( ) func init() { - // TODO: rootCmd.PersistentFlags().BoolP("version", "V", false, "Print version infos") + rootCmd.PersistentFlags().BoolP("version", "V", false, "Print version and exit") // TODO: rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose output") - rootCmd.Flags().String("host", "api.modrinth.com", "Labrinth host") - rootCmd.Flags().String("server-dir", "mc", "Server directory path") - rootCmd.Flags().String("server-file", "", "Server jar file name") - rootCmd.Flags().String("proxy", "", "Use a proxy to download") - rootCmd.Flags().Int("download-threads", 8, "Download threads") - rootCmd.Flags().Int("retry-times", 3, "Number of retries when a download fails") + rootCmd.PersistentFlags().String("host", "api.modrinth.com", "Labrinth host") + rootCmd.PersistentFlags().String("server-dir", "mc", "Server directory path") + rootCmd.PersistentFlags().String("server-file", "", "Server jar file name") + rootCmd.PersistentFlags().String("proxy", "", "Use a proxy to download") + rootCmd.PersistentFlags().Int("download-threads", 8, "Download threads") + rootCmd.PersistentFlags().Int("retry-times", 3, "Number of retries when a download fails") + // TODO: --eula + // TODO: --op ... + // TODO: --whitelist ... + // TODO: --start-server } var rootCmd = &cobra.Command{ @@ -34,9 +39,32 @@ var rootCmd = &cobra.Command{ mrpack-install https://example.org/data/cool-pack.mrpack mrpack-install hexmc-modpack --server-file server.jar mrpack-install yK0ISmKn 1.0.0-1.18 --server-dir mcserver - mrpack-install communitypack9000 --host api.labrinth.example.org`, - Args: cobra.RangeArgs(1, 2), + mrpack-install communitypack9000 --host api.labrinth.example.org + mrpack-install --version`, + Args: cobra.RangeArgs(0, 2), Run: func(cmd *cobra.Command, args []string) { + ver, err := cmd.Flags().GetBool("version") + if err != nil { + log.Fatalln(err) + } + if ver { + fmt.Println("mrpack-install", buildinfo.Version) + return + } + + if len(args) < 1 { + err = cmd.Help() + if err != nil { + fmt.Println(err) + } + os.Exit(1) + } + input := args[0] + version := "" + if len(args) > 1 { + version = args[1] + } + host, err := cmd.Flags().GetString("host") if err != nil { log.Fatalln(err) @@ -70,12 +98,6 @@ var rootCmd = &cobra.Command{ fmt.Println(err) } - input := args[0] - version := "" - if len(args) > 1 { - version = args[1] - } - err = os.MkdirAll(serverDir, 0755) if err != nil { log.Fatalln(err) diff --git a/cmd/server.go b/cmd/server.go index f75a454..a79a1b5 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -2,6 +2,7 @@ package cmd import ( "github.com/nothub/mrpack-install/mojang" + "github.com/nothub/mrpack-install/requester" "github.com/nothub/mrpack-install/server" "github.com/spf13/cobra" "log" @@ -10,13 +11,6 @@ import ( func init() { serverCmd.Flags().String("minecraft-version", "latest", "Minecraft version") serverCmd.Flags().String("flavor-version", "latest", "Flavor version") - serverCmd.Flags().String("server-dir", "mc", "Server directory path") - serverCmd.Flags().String("server-file", "", "Server jar file name") - /* - TODO: eula flag - TODO: ops flag - TODO: whitelist flags - */ rootCmd.AddCommand(serverCmd) } @@ -45,6 +39,16 @@ var serverCmd = &cobra.Command{ if err != nil { log.Fatalln(err) } + proxy, err := cmd.Flags().GetString("proxy") + if err != nil { + log.Fatalln(err) + } + if proxy != "" { + err := requester.DefaultHttpClient.SetProxy(proxy) + if err != nil { + log.Fatalln(err) + } + } if minecraftVersion == "" || minecraftVersion == "latest" { latestMinecraftVersion, err := mojang.LatestRelease() diff --git a/cmd/version.go b/cmd/version.go index 9a826f6..216631c 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -2,10 +2,7 @@ package cmd import ( "fmt" - "log" - "path" - "runtime/debug" - + "github.com/nothub/mrpack-install/buildinfo" "github.com/spf13/cobra" ) @@ -18,11 +15,6 @@ var versionCmd = &cobra.Command{ Short: "Print version infos", Long: `Extract and display the running binaries embedded version information.`, Run: func(cmd *cobra.Command, args []string) { - info, ok := debug.ReadBuildInfo() - if ok { - fmt.Println(path.Base(info.Main.Path), info.Main.Version, info.Main.Sum) - } else { - log.Fatalln("Unable to extract build infos from running binary!") - } + fmt.Println("mrpack-install", buildinfo.Version) }, }