From 89e0374cffad4beb58c29b763514452b7e758c0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=96zelo=C4=9Flu?= Date: Sun, 19 Sep 2021 16:36:56 +0300 Subject: [PATCH 1/3] cli command structure changed * created template for cli part * cli cmd and network jobs are separated * .gitignore updated for GoLand IDE * module name changed --- .gitignore | 3 +++ cmd/commands.go | 18 ++++++++++++++++++ cmd/root.go | 25 +++++++++++++++++++++++++ go.mod | 2 +- main.go | 48 ++++-------------------------------------------- 5 files changed, 51 insertions(+), 45 deletions(-) create mode 100644 cmd/commands.go create mode 100644 cmd/root.go diff --git a/.gitignore b/.gitignore index a3d4cf7..e745981 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ # Dependency directories (remove the comment below to include it) # vendor/ gurl + +# IDE directories and files +.idea/ \ No newline at end of file diff --git a/cmd/commands.go b/cmd/commands.go new file mode 100644 index 0000000..83e8ffa --- /dev/null +++ b/cmd/commands.go @@ -0,0 +1,18 @@ +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" + "strings" +) + +var cmdGet = &cobra.Command{ + Use: "GET [ to send GET request]", + Short: "Fetches data from given url", + Long: `Fetches data from given url. + It should be valid URL.`, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("URL: " + strings.Join(args, " ")) + }, +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..3610e11 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" + "os" +) + +var rootCmd = &cobra.Command{ + Use: "gURL [options...] ", + Short: "gURL is open source CLI tool written in Go.", + Args: cobra.MaximumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("URL: ", args[0]) + }, +} + +func Execute() { + rootCmd.AddCommand(cmdGet) + + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/go.mod b/go.mod index 371ac35..6b8c028 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module gurl +module github.com/academic/gURL go 1.15 diff --git a/main.go b/main.go index 535e6b7..629319f 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "errors" "flag" "fmt" + "github.com/academic/gURL/cmd" "io" "mime/multipart" "net/url" @@ -15,8 +16,9 @@ import ( "sync" "time" - "github.com/spf13/cobra" + //"github.com/spf13/cobra" + //"github.com/academic/gURL" jsoniter "github.com/json-iterator/go" "github.com/valyala/fasthttp" "github.com/valyala/fasthttp/fasthttpproxy" @@ -405,47 +407,5 @@ func main2() { } func main() { - var echoTimes int - - var cmdPrint = &cobra.Command{ - Use: "print [string to print]", - Short: "Print anything to the screen", - Long: `print is for printing anything back to the screen. - For many years people have printed back to the screen.`, - Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Print: " + strings.Join(args, " ")) - }, - } - - var cmdEcho = &cobra.Command{ - Use: "echo [string to echo]", - Short: "Echo anything to the screen", - Long: `echo is for echoing anything back. - Echo works a lot like print, except it has a child command.`, - Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Echo: " + strings.Join(args, " ")) - }, - } - - var cmdTimes = &cobra.Command{ - Use: "times [string to echo]", - Short: "Echo anything to the screen more times", - Long: `echo things multiple times back to the user by providing - a count and a string.`, - Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - for i := 0; i < echoTimes; i++ { - fmt.Println("Echo: " + strings.Join(args, " ")) - } - }, - } - - cmdTimes.Flags().IntVarP(&echoTimes, "times", "t", 1, "times to echo the input") - - var rootCmd = &cobra.Command{Use: "gurl"} - rootCmd.AddCommand(cmdPrint, cmdEcho) - cmdEcho.AddCommand(cmdTimes) - rootCmd.Execute() + cmd.Execute() } From 8b3579370c222001e459c6e796a26fe507677c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=96zelo=C4=9Flu?= Date: Sun, 19 Sep 2021 16:42:06 +0300 Subject: [PATCH 2/3] meaningless comment lines removed --- main.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/main.go b/main.go index 629319f..19592d2 100644 --- a/main.go +++ b/main.go @@ -16,9 +16,6 @@ import ( "sync" "time" - //"github.com/spf13/cobra" - - //"github.com/academic/gURL" jsoniter "github.com/json-iterator/go" "github.com/valyala/fasthttp" "github.com/valyala/fasthttp/fasthttpproxy" From 32057aa53554a0d81107b5969810919fdb76dc27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=96zelo=C4=9Flu?= Date: Sun, 19 Sep 2021 16:52:37 +0300 Subject: [PATCH 3/3] renamed: cmd/commands.go -> cmd/requests.go --- cmd/{commands.go => requests.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename cmd/{commands.go => requests.go} (100%) diff --git a/cmd/commands.go b/cmd/requests.go similarity index 100% rename from cmd/commands.go rename to cmd/requests.go