Skip to content

Commit

Permalink
flag cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Sep 9, 2022
1 parent eb0dd63 commit e0cc381
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func init() {
pingCmd.Flags().String("host", "api.modrinth.com", "Labrinth host")
rootCmd.AddCommand(pingCmd)
}

Expand All @@ -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 {
Expand Down
52 changes: 37 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 <uuid>...
// TODO: --whitelist <uuid>...
// TODO: --start-server
}

var rootCmd = &cobra.Command{
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 11 additions & 7 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}

Expand Down Expand Up @@ -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()
Expand Down
12 changes: 2 additions & 10 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package cmd

import (
"fmt"
"log"
"path"
"runtime/debug"

"github.com/nothub/mrpack-install/buildinfo"
"github.com/spf13/cobra"
)

Expand All @@ -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)
},
}

0 comments on commit e0cc381

Please sign in to comment.