From a80ccd657e9029b9c00374a2d63e1704f8a3d495 Mon Sep 17 00:00:00 2001 From: jjzcru Date: Sun, 3 May 2020 17:01:33 -0500 Subject: [PATCH 01/10] Add usage to documentation --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ pkg/server/server.go | 11 ++++++----- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7f8f955..7b4b9d9 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ Since it's written in [Go][go], most of the commands runs across multiple operat ## Table of contents * [Getting Started](#getting-started) + [Installation](#installation) + + [Usage](#usage) * [Syntax](#syntax) * [Use Cases](#use-cases) * [Commands](#commands) @@ -59,6 +60,46 @@ are on `windows` you can ignore this step. 3. Add the binary to `$PATH`. 4. Run `hog version` to make sure that the binary is installed. +### Usage + +#### Start Server + +First you need to start the server so the application is able to serve the files. + +``` +hog start -d +``` + +By default the application will run at port `1618` and the endpoint for download is `/download/{BucketID}`. For more information about how the `start` command works go to [start][start] documentation. + +#### Add files + +Now you need to add files to serve, for this go to your terminal and navigate to file or directory that you want to +share and run the `add` command. +``` +hog add file.png ./file.jpg /home/root/file.pdf +``` + +You can add multiple files which are going to be group together as a bucket, you can add relative path, name of files +or directories or absolute paths. This command will return the `BucketID` that was created. + +#### Share your bucket + +To share, after you run the `add` command, you need to use the `BucketID` to generate the link. Lets say that the server +is running on port `1618` and the `BucketID` generated is `2iez0Wa`. Now you just need to create a url that targets +your computer in that port. + +``` +// For localhost +http://localhost:1618/download/2iez0Wa + +// For IP +http://192.168.1.101:1618/download/2iez0Wa + +// For domain +http://my.domain.com:1618/download/2iez0Wa +``` + ## Syntax The syntax consists of: - `domain`: The domain that is use to reference the address of the machine, this value is used by some sharing diff --git a/pkg/server/server.go b/pkg/server/server.go index 49c8651..712c142 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -3,17 +3,18 @@ package server import ( "context" "fmt" - "github.com/gorilla/mux" - "github.com/jjzcru/hog/pkg/hog" - "github.com/jjzcru/hog/pkg/server/handler" - "github.com/jjzcru/hog/pkg/utils" - "github.com/logrusorgru/aurora" "log" "net" "net/http" "os" "os/signal" "strings" + + "github.com/gorilla/mux" + "github.com/jjzcru/hog/pkg/hog" + "github.com/jjzcru/hog/pkg/server/handler" + "github.com/jjzcru/hog/pkg/utils" + "github.com/logrusorgru/aurora" ) func Start(port int, hogPath string, token string) error { From f2cfecfd7b371559090385fc837051383eacf723 Mon Sep 17 00:00:00 2001 From: jjzcru Date: Sun, 3 May 2020 17:44:46 -0500 Subject: [PATCH 02/10] Enable delete multiple files using regex --- internal/command/bucket/cmd.go | 3 +++ internal/command/remove/cmd.go | 48 +++++++++++++++++++++++----------- pkg/utils/utils.go | 35 +++++++++++++++++++++++++ pkg/utils/utils_test.go | 25 ++++++++++++++++++ 4 files changed, 96 insertions(+), 15 deletions(-) create mode 100644 pkg/utils/utils.go create mode 100644 pkg/utils/utils_test.go diff --git a/internal/command/bucket/cmd.go b/internal/command/bucket/cmd.go index b9a2bc0..8e4840f 100644 --- a/internal/command/bucket/cmd.go +++ b/internal/command/bucket/cmd.go @@ -84,6 +84,9 @@ func getHog(hogPath string) (hog.Hog, error) { func transform(h hog.Hog) [][]string { var table [][]string for id, files := range h.Buckets { + if len(files) == 0 { + table = append(table, []string{id, "EMPTY"}) + } for _, file := range files { table = append(table, []string{id, file}) } diff --git a/internal/command/remove/cmd.go b/internal/command/remove/cmd.go index 7d517e5..dd882b6 100644 --- a/internal/command/remove/cmd.go +++ b/internal/command/remove/cmd.go @@ -14,9 +14,9 @@ func Command() *cobra.Command { cmd := &cobra.Command{ Use: "remove", Short: "Remove a bucket by its id", - Args: cobra.ExactArgs(1), + Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - err := run(cmd, args[0]) + err := run(cmd, args) if err != nil { utils.PrintError(err) } @@ -29,7 +29,7 @@ func Command() *cobra.Command { return cmd } -func run(cmd *cobra.Command, id string) error { +func run(cmd *cobra.Command, ids []string) error { isDetached, err := cmd.Flags().GetBool("detached") if err != nil { return err @@ -66,19 +66,40 @@ func run(cmd *cobra.Command, id string) error { return fmt.Errorf("deadline can't be before of current time") } } - - err = validate(h, id) - if err != nil { - return err + var bucketIds []string + for _, id := range ids { + bucketID, err := getBucketIds(h, id) + if err != nil { + return err + } + bucketIds = append(bucketIds, bucketID...) } + bucketIds = utils.RemoveDuplicate(bucketIds) + if isDetached { return Detached() } delayCmd(ttl, deadline) - return remove(hogPath, h, id) + return remove(hogPath, h, bucketIds) +} + +func getBucketIds(h hog.Hog, id string) ([]string, error) { + var ids []string + for k := range h.Buckets { + isSubstring, err := utils.IsSubstring(id, k) + if err != nil { + return ids, err + } + + if isSubstring { + ids = append(ids, k) + } + } + + return ids, nil } func getHog(hogPath string) (hog.Hog, error) { @@ -97,15 +118,12 @@ func getHog(hogPath string) (hog.Hog, error) { } -func validate(h hog.Hog, id string) error { - if _, ok := h.Buckets[id]; !ok { - return fmt.Errorf("bucket with id '%s' do not exist", id) +func remove(hogPath string, h hog.Hog, ids []string) error { + + for _, id := range ids { + delete(h.Buckets, id) } - return nil -} -func remove(hogPath string, h hog.Hog, id string) error { - delete(h.Buckets, id) return hog.SaveToPath(hogPath, h) } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go new file mode 100644 index 0000000..8d974cc --- /dev/null +++ b/pkg/utils/utils.go @@ -0,0 +1,35 @@ +package utils + +import ( + "fmt" + "regexp" +) + +// RemoveDuplicate takes a slice of string as input and remove the duplicates +func RemoveDuplicate(input []string) []string { + var response []string + if input == nil { + input = []string{} + } + + responseMap := map[string]bool{} + + for _, value := range input { + responseMap[value] = true + } + + for k := range responseMap { + response = append(response, k) + } + + return response +} + +// IsSubstring check if a string is a substring of another string +func IsSubstring(substring string, completeString string) (bool, error) { + reg, err := regexp.Compile(fmt.Sprintf("(.)*%s(.)*", substring)) + if err != nil { + return false, err + } + return reg.MatchString(completeString), nil +} diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go new file mode 100644 index 0000000..6735f86 --- /dev/null +++ b/pkg/utils/utils_test.go @@ -0,0 +1,25 @@ +package utils + +import "testing" + +func TestRemoveDuplicate(t *testing.T) { + input := []string{"a", "b", "c", "c"} + response := RemoveDuplicate(input) + if len(response) != (len(input) - 1) { + t.Errorf("the response should have %d items but it has %d instead", len(input)-1, len(response)) + } +} + +func TestIsSubstring(t *testing.T) { + subString := "axc" + completeString := "111axc111" + + isSubstring, err := IsSubstring(subString, completeString) + if err != nil { + t.Error(err) + } + + if !isSubstring { + t.Errorf("the substring belong inside the complete string") + } +} From 022c0703bf992077f6ad250be625e9f48694fcf4 Mon Sep 17 00:00:00 2001 From: jjzcru Date: Sun, 3 May 2020 17:58:02 -0500 Subject: [PATCH 03/10] Enable delete multiple files using regex --- README.md | 2 +- docs/commands/remove.md | 2 +- docs/commands/start.md | 2 +- docs/commands/update.md | 2 +- pkg/hog/hog.go | 4 +++- pkg/hog/hog_test.go | 2 +- 6 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7b4b9d9..eb9d780 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Since it's written in [Go][go], most of the commands runs across multiple operat The main use case for `hog` is that you are able to share your files with other people directly from your machine without any intermediary _(Dropbox, Google Drive, WeTransfer)_. -`hog` uses a file called `hog.yml` to store the references from your files, by default this file is located in your +`hog` uses a file called `.hog.yml` to store the references from your files, by default this file is located in your home directory but you can change the directory used by `hog` by setting the `env` variable `HOG_PEN`. `hog` groups your files in `buckets` which are just a list of paths inside your file system for the files you would diff --git a/docs/commands/remove.md b/docs/commands/remove.md index 8dd8749..8fe52d8 100644 --- a/docs/commands/remove.md +++ b/docs/commands/remove.md @@ -8,7 +8,7 @@ Remove a bucket by its id hog remove {id} [flags] ``` -This command takes only one id as argument. It will check if the bucket exists inside `hog.yml` and will remove the +This command takes only one id as argument. It will check if the bucket exists inside `.hog.yml` and will remove the reference from the file. By default this command runs immediately, if you which to delay the deletion use one of the flags described below. diff --git a/docs/commands/start.md b/docs/commands/start.md index d435b13..04a6755 100644 --- a/docs/commands/start.md +++ b/docs/commands/start.md @@ -8,7 +8,7 @@ Start hog service hog start [flags] ``` -This command use the `hog.yml` file to fetch the bucket ids that are going to be serve to download. +This command use the `.hog.yml` file to fetch the bucket ids that are going to be serve to download. To download any bucket you need to call the endpoint `/download/{BucketID}` depending on the amount of files and the path the download is going to be as following: diff --git a/docs/commands/update.md b/docs/commands/update.md index 3012f57..4096010 100644 --- a/docs/commands/update.md +++ b/docs/commands/update.md @@ -8,7 +8,7 @@ Update the files in a bucket by its id hog update {id} [files] [flags] ``` -This command takes only one id as argument. It will check if the bucket exists inside `hog.yml` and will overwrite +This command takes only one id as argument. It will check if the bucket exists inside `.hog.yml` and will overwrite the files inside the bucket with the files provided. ## Examples diff --git a/pkg/hog/hog.go b/pkg/hog/hog.go index 136549f..1463b46 100644 --- a/pkg/hog/hog.go +++ b/pkg/hog/hog.go @@ -11,6 +11,8 @@ import ( "gopkg.in/yaml.v2" ) +var FILE = ".hog.yml" + var defaultHog = Hog{ Domain: "localhost", Protocol: "http", @@ -80,7 +82,7 @@ func GetPath() (string, error) { return "", err } - return path.Join(baseDir, "hog.yml"), nil + return path.Join(baseDir, FILE), nil } func GetBaseDir() (string, error) { diff --git a/pkg/hog/hog_test.go b/pkg/hog/hog_test.go index fb8876e..8144796 100644 --- a/pkg/hog/hog_test.go +++ b/pkg/hog/hog_test.go @@ -42,7 +42,7 @@ func TestGetPath(t *testing.T) { t.Error(err) } - targetHogPath, err := filepath.Abs(filepath.Join(os.TempDir(), "hog.yml")) + targetHogPath, err := filepath.Abs(filepath.Join(os.TempDir(), FILE)) if err != nil { t.Error(err) } From 6d69f2c1704da6feba02d3534962bd44d3c2461d Mon Sep 17 00:00:00 2001 From: jjzcru Date: Sun, 3 May 2020 18:08:08 -0500 Subject: [PATCH 04/10] Enable update file with regex --- internal/command/update/cmd.go | 41 +++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/internal/command/update/cmd.go b/internal/command/update/cmd.go index 4f4ca3c..86dba95 100644 --- a/internal/command/update/cmd.go +++ b/internal/command/update/cmd.go @@ -14,7 +14,7 @@ func Command() *cobra.Command { cmd := &cobra.Command{ Use: "update", Short: "Update the files in a bucket by its id", - Args: cobra.MinimumNArgs(2), + Args: cobra.ExactArgs(2), Run: func(cmd *cobra.Command, args []string) { id := args[0] files := args[1:] @@ -34,8 +34,9 @@ func run(id string, files []string) error { return err } - if _, ok := h.Buckets[id]; !ok { - return fmt.Errorf("bucket with id '%s' do not exist", id) + bucketID, err := getBucketID(h, id) + if err != nil { + return err } for i, file := range files { @@ -45,13 +46,43 @@ func run(id string, files []string) error { } if !utils.IsPathExist(filePath) { - return fmt.Errorf("path %s is not valid or do not exist", filePath) + return fmt.Errorf("path '%s' is not valid or do not exist", filePath) } files[i] = filePath } - h.Buckets[id] = files + h.Buckets[bucketID] = files return hog.Save(h) } + +func getBucketID(h hog.Hog, id string) (string, error) { + var ids []string + response := "" + + for k := range h.Buckets { + isSubstring, err := utils.IsSubstring(id, k) + if err != nil { + return response, err + } + + if isSubstring { + ids = append(ids, k) + } + } + + ids = utils.RemoveDuplicate(ids) + + if len(ids) == 0 { + return response, fmt.Errorf("no bucket was found with id '%s'", id) + } + + if len(ids) > 1 { + return response, fmt.Errorf("more than one bucket exists with the id '%s'", id) + } + + response = ids[0] + + return response, nil +} From 02ebd43513805cd07036a70b491e7b7f37642aae Mon Sep 17 00:00:00 2001 From: jjzcru Date: Sun, 3 May 2020 18:19:23 -0500 Subject: [PATCH 05/10] Add `url` flag to add command --- docs/commands/add.md | 12 ++++++++++++ internal/command/add/cmd.go | 19 +++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/docs/commands/add.md b/docs/commands/add.md index e075417..10b5405 100644 --- a/docs/commands/add.md +++ b/docs/commands/add.md @@ -17,12 +17,15 @@ Then it will return the `id` for the bucket. hog add test.jpg hog add test_1.jpg ./test_2.jpb hog add test.jpg /home/example/download/file.pdf +hog add test.jpg /home/example/download/file.pdf --url +hog add test.jpg /home/example/download/file.pdf -u ``` ## Flags | Flag | Short code | Description | | ------- | ------ | ------- | | [ttl](#ttl) | | Remove a bucket after a period of time | +| [url](#url) | u | Return a share url as response | ### ttl @@ -34,4 +37,13 @@ hog add test.jpg --ttl 10s hog add test.jpg test_1.png --ttl 1m ``` +### url + +This flag if enable will return an url to share, created by using the configuration inside `.hog.yml`. + +``` +hog add test.jpg --url +hog add test.jpg test_1.png -u +``` + [time-to-live]: https://www.cloudflare.com/learning/cdn/glossary/time-to-live-ttl/ \ No newline at end of file diff --git a/internal/command/add/cmd.go b/internal/command/add/cmd.go index 8cef1de..426810d 100644 --- a/internal/command/add/cmd.go +++ b/internal/command/add/cmd.go @@ -18,17 +18,18 @@ func Command() *cobra.Command { Short: "Group files in a bucket", Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - bucketID, err := run(cmd, args) + response, err := run(cmd, args) if err != nil { utils.PrintError(err) return } - fmt.Println(bucketID) + fmt.Println(response) }, } cmd.Flags().Duration("ttl", 0, "Remove a bucket after a period of time") + cmd.Flags().BoolP("url", "u", false, "Return a share url as response") return cmd } @@ -39,6 +40,11 @@ func run(cmd *cobra.Command, args []string) (string, error) { return "", err } + isUrl, err := cmd.Flags().GetBool("url") + if err != nil { + return "", err + } + var files []string for _, file := range args { filePath, err := filepath.Abs(file) @@ -73,5 +79,14 @@ func run(cmd *cobra.Command, args []string) (string, error) { } } + if isUrl { + h, err := hog.Get() + if err != nil { + return "", err + } + + return fmt.Sprintf("%s://%s:%d/download/%s", h.Protocol, h.Domain, h.Port, bucketID), nil + } + return bucketID, nil } From 225ab33db521cea8aaeee7645127662af3934a32 Mon Sep 17 00:00:00 2001 From: jjzcru Date: Sun, 3 May 2020 18:45:59 -0500 Subject: [PATCH 06/10] Add support for `set` command --- internal/command/add/cmd.go | 2 +- internal/command/bucket/cmd.go | 2 +- internal/command/command.go | 2 + internal/command/remove/cmd.go | 2 +- internal/command/set/cmd.go | 125 +++++++++++++++++++++++++++++++++ internal/command/start/cmd.go | 2 +- internal/command/update/cmd.go | 2 +- 7 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 internal/command/set/cmd.go diff --git a/internal/command/add/cmd.go b/internal/command/add/cmd.go index 426810d..d77643c 100644 --- a/internal/command/add/cmd.go +++ b/internal/command/add/cmd.go @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -// Command returns a cobra command for `init` sub command +// Command returns a cobra command for `add` sub command func Command() *cobra.Command { cmd := &cobra.Command{ Use: "add", diff --git a/internal/command/bucket/cmd.go b/internal/command/bucket/cmd.go index 8e4840f..a5186a3 100644 --- a/internal/command/bucket/cmd.go +++ b/internal/command/bucket/cmd.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -// Command returns a cobra command for `init` sub command +// Command returns a cobra command for `bucket` sub command func Command() *cobra.Command { cmd := &cobra.Command{ Use: "bucket", diff --git a/internal/command/command.go b/internal/command/command.go index b5185ae..b60dcdb 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -1,6 +1,7 @@ package command import ( + "github.com/jjzcru/hog/internal/command/set" "os" "github.com/jjzcru/hog/internal/command/add" @@ -31,6 +32,7 @@ func Execute() error { start.Command(), bucket.Command(), version.Command(), + set.Command(), ) return rootCmd.Execute() diff --git a/internal/command/remove/cmd.go b/internal/command/remove/cmd.go index dd882b6..05abc2a 100644 --- a/internal/command/remove/cmd.go +++ b/internal/command/remove/cmd.go @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -// Command returns a cobra command for `init` sub command +// Command returns a cobra command for `remove` sub command func Command() *cobra.Command { cmd := &cobra.Command{ Use: "remove", diff --git a/internal/command/set/cmd.go b/internal/command/set/cmd.go new file mode 100644 index 0000000..f1584cc --- /dev/null +++ b/internal/command/set/cmd.go @@ -0,0 +1,125 @@ +package set + +import ( + "errors" + "fmt" + "github.com/jjzcru/hog/pkg/hog" + "github.com/jjzcru/hog/pkg/utils" + "github.com/spf13/cobra" + "os" + "strconv" +) + +// Command returns a cobra command for `set` sub command +func Command() *cobra.Command { + cmd := &cobra.Command{ + Use: "set", + Short: "Edit hog configuration values", + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + _ = cmd.Help() + os.Exit(0) + } + }, + } + + cmd.AddCommand( + protocol(), + domain(), + port(), + ) + + return cmd +} + +func protocol() *cobra.Command { + return &cobra.Command{ + Use: "protocol", + Short: "Set protocol value", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + proto := args[0] + if proto != "http" && proto != "https" { + utils.PrintError(errors.New("the only valid protocols are http or https")) + return + } + + h, err := hog.Get() + if err != nil { + utils.PrintError(err) + return + } + h.Protocol = proto + + err = hog.Save(h) + if err != nil { + utils.PrintError(err) + return + } + }, + } +} + +func domain() *cobra.Command { + return &cobra.Command{ + Use: "domain", + Short: "Set domain value", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + h, err := hog.Get() + if err != nil { + utils.PrintError(err) + return + } + h.Domain = args[0] + err = hog.Save(h) + if err != nil { + utils.PrintError(err) + return + } + }, + } +} + +func port() *cobra.Command { + return &cobra.Command{ + Use: "port", + Short: "Set port value", + Args: validatePort(), + Run: func(cmd *cobra.Command, args []string) { + h, err := hog.Get() + if err != nil { + utils.PrintError(err) + return + } + + port, err := strconv.Atoi(args[0]) + if err != nil { + utils.PrintError(err) + return + } + + h.Port = port + err = hog.Save(h) + if err != nil { + utils.PrintError(err) + return + } + }, + } +} + +func validatePort() cobra.PositionalArgs { + return func(cmd *cobra.Command, args []string) error { + if len(args) != 1 { + return fmt.Errorf("accepts 1 arg, received %d", len(args)) + } + + _, err := strconv.Atoi(args[0]) + if err != nil { + return fmt.Errorf("'%s' is not an valid port number", args[0]) + } + + return nil + } +} diff --git a/internal/command/start/cmd.go b/internal/command/start/cmd.go index f029c3b..1ebba24 100644 --- a/internal/command/start/cmd.go +++ b/internal/command/start/cmd.go @@ -7,7 +7,7 @@ import ( "github.com/spf13/cobra" ) -// Command returns a cobra command for `init` sub command +// Command returns a cobra command for `start` sub command func Command() *cobra.Command { cmd := &cobra.Command{ Use: "start", diff --git a/internal/command/update/cmd.go b/internal/command/update/cmd.go index 86dba95..f6564a8 100644 --- a/internal/command/update/cmd.go +++ b/internal/command/update/cmd.go @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -// Command returns a cobra command for `init` sub command +// Command returns a cobra command for `update` sub command func Command() *cobra.Command { cmd := &cobra.Command{ Use: "update", From b2ed0a02a81629f716fc43b1ba162cf0faa14eae Mon Sep 17 00:00:00 2001 From: jjzcru Date: Sun, 3 May 2020 18:51:22 -0500 Subject: [PATCH 07/10] Add support for `get` command --- internal/command/command.go | 2 + internal/command/get/cmd.go | 79 +++++++++++++++++++++++++++++++++++++ internal/command/set/cmd.go | 2 +- 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 internal/command/get/cmd.go diff --git a/internal/command/command.go b/internal/command/command.go index b60dcdb..6f704f2 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -1,6 +1,7 @@ package command import ( + "github.com/jjzcru/hog/internal/command/get" "github.com/jjzcru/hog/internal/command/set" "os" @@ -33,6 +34,7 @@ func Execute() error { bucket.Command(), version.Command(), set.Command(), + get.Command(), ) return rootCmd.Execute() diff --git a/internal/command/get/cmd.go b/internal/command/get/cmd.go new file mode 100644 index 0000000..77ea997 --- /dev/null +++ b/internal/command/get/cmd.go @@ -0,0 +1,79 @@ +package get + +import ( + "fmt" + "github.com/jjzcru/hog/pkg/hog" + "github.com/jjzcru/hog/pkg/utils" + "github.com/spf13/cobra" + "os" +) + +// Command returns a cobra command for `get` sub command +func Command() *cobra.Command { + cmd := &cobra.Command{ + Use: "get", + Short: "Get hog configuration values", + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + _ = cmd.Help() + os.Exit(0) + } + }, + } + + cmd.AddCommand( + protocol(), + domain(), + port(), + ) + + return cmd +} + +func protocol() *cobra.Command { + return &cobra.Command{ + Use: "protocol", + Short: "Get protocol value", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + h, err := hog.Get() + if err != nil { + utils.PrintError(err) + return + } + fmt.Println(h.Protocol) + }, + } +} + +func domain() *cobra.Command { + return &cobra.Command{ + Use: "domain", + Short: "Get domain value", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + h, err := hog.Get() + if err != nil { + utils.PrintError(err) + return + } + fmt.Println(h.Domain) + }, + } +} + +func port() *cobra.Command { + return &cobra.Command{ + Use: "port", + Short: "Get port value", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + h, err := hog.Get() + if err != nil { + utils.PrintError(err) + return + } + fmt.Println(h.Port) + }, + } +} diff --git a/internal/command/set/cmd.go b/internal/command/set/cmd.go index f1584cc..5cd74e7 100644 --- a/internal/command/set/cmd.go +++ b/internal/command/set/cmd.go @@ -14,7 +14,7 @@ import ( func Command() *cobra.Command { cmd := &cobra.Command{ Use: "set", - Short: "Edit hog configuration values", + Short: "Set hog configuration values", Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { _ = cmd.Help() From 23fb08f930ad02536daab079ed98dbfe96f8ecdd Mon Sep 17 00:00:00 2001 From: jjzcru Date: Sun, 3 May 2020 19:06:58 -0500 Subject: [PATCH 08/10] Add documentation for add and set command --- README.md | 7 +++++- docs/commands/get.md | 52 +++++++++++++++++++++++++++++++++++++++ docs/commands/set.md | 58 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 docs/commands/get.md create mode 100644 docs/commands/set.md diff --git a/README.md b/README.md index eb9d780..c1b3541 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,8 @@ First you need to start the server so the application is able to serve the files hog start -d ``` -By default the application will run at port `1618` and the endpoint for download is `/download/{BucketID}`. For more information about how the `start` command works go to [start][start] documentation. +By default the application will run at port `1618` and the endpoint for download is `/download/{BucketID}`. For more +information about how the `start` command works go to [start][start] documentation. #### Add files @@ -146,7 +147,9 @@ link and the file will automatically download. | ------- | ------ | ------- | | [add][add] | Group files in a bucket | `hog add [files] [flags]` | | [bucket][bucket] | Display the buckets and their files | `hog bucket [flags]` | +| [get][get] | Get hog configuration values | `hog get [command]` | | [remove][remove] | Remove a bucket by its id | `hog remove {id} [flags]` | +| [set][set] | Set hog configuration values | `hog set [command]` | | [start][start] | Start hog service | `hog start [flags]` | | [update][update] | Update the files in a bucket by its id | `hog update {id} [files] [flags]`| | [version][version]| Display version number | `hog version [flags]` | @@ -167,7 +170,9 @@ To learn more about the progress and what is being planned go to [Projects][proj [add]: docs/commands/add.md [bucket]: docs/commands/bucket.md +[get]: docs/commands/get.md [remove]: docs/commands/remove.md +[set]: docs/commands/set.md [start]: docs/commands/start.md [update]: docs/commands/update.md [version]: docs/commands/version.md diff --git a/docs/commands/get.md b/docs/commands/get.md new file mode 100644 index 0000000..321551a --- /dev/null +++ b/docs/commands/get.md @@ -0,0 +1,52 @@ +get +========== + +Get hog configuration values. + +_This values comes from the file `.hog.yml`_ + +## Syntax +``` +hog get [command] +``` + +## Example +``` +hog get protocol +hog get domain +hog get port +``` + +## Command + +### protocol + +Get protocol value. Should be `http` or `https`. + +#### Syntax +``` +hog get protocol +``` + +### domain + +Get domain value. + +_This is the domain from which the devices are going to reach the server, can be an ip like `192.168.1.101` or a +public domain like `example.com`. This value is used for sharing functionalities_ + +#### Syntax +``` +hog get domain +``` + +### port + +Get port value. + +_This value should be a number_ + +#### Syntax +``` +hog get port +``` \ No newline at end of file diff --git a/docs/commands/set.md b/docs/commands/set.md new file mode 100644 index 0000000..403d104 --- /dev/null +++ b/docs/commands/set.md @@ -0,0 +1,58 @@ +set +========== + +Set hog configuration values. + +_This command will update the values in the file `.hog.yml`_ + +## Syntax +``` +hog set [command] +``` + +## Example +``` +hog set protocol http +hog set protocol https +hog set domain localhost +hog set domain 192.168.1.101 +hog set domain example.com +hog set port 3000 +``` + +## Command + +### protocol + +Set protocol value. This value can only be `http` or `https`. + +#### Syntax +``` +hog set protocol http +hog set protocol https +``` + +### domain + +Set domain value. + +_This is the domain from which the devices are going to reach the server, can be an ip like `192.168.1.101` or a +public domain like `example.com`. This value is used for sharing functionalities_ + +#### Syntax +``` +hog set domain localhost +hog set domain 192.168.1.101 +hog set domain example.com +``` + +### port + +Set port value. + +_This value must be a number_ + +#### Syntax +``` +hog set port 3000 +``` \ No newline at end of file From 2e601f1544caa25d1a721cdb538d8b980febac54 Mon Sep 17 00:00:00 2001 From: jjzcru Date: Sun, 3 May 2020 19:14:37 -0500 Subject: [PATCH 09/10] Add code documentation --- internal/command/bucket/cmd.go | 2 +- internal/command/remove/cmd.go | 2 +- internal/command/start/cmd.go | 2 +- pkg/hog/hog.go | 21 +++++++++++++++------ pkg/hog/hog_test.go | 12 ++++++------ pkg/hog/id.go | 1 + 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/internal/command/bucket/cmd.go b/internal/command/bucket/cmd.go index a5186a3..d7c506f 100644 --- a/internal/command/bucket/cmd.go +++ b/internal/command/bucket/cmd.go @@ -30,7 +30,7 @@ func Command() *cobra.Command { func run() error { - hogPath, err := hog.GetPath() + hogPath, err := hog.Path() if err != nil { return err } diff --git a/internal/command/remove/cmd.go b/internal/command/remove/cmd.go index 05abc2a..93cd624 100644 --- a/internal/command/remove/cmd.go +++ b/internal/command/remove/cmd.go @@ -45,7 +45,7 @@ func run(cmd *cobra.Command, ids []string) error { return err } - hogPath, err := hog.GetPath() + hogPath, err := hog.Path() if err != nil { return err } diff --git a/internal/command/start/cmd.go b/internal/command/start/cmd.go index 1ebba24..5cac9ec 100644 --- a/internal/command/start/cmd.go +++ b/internal/command/start/cmd.go @@ -64,7 +64,7 @@ func run(cmd *cobra.Command) error { return detached(token) } - hogPath, err := hog.GetPath() + hogPath, err := hog.Path() if err != nil { return nil } diff --git a/pkg/hog/hog.go b/pkg/hog/hog.go index 1463b46..2690fe9 100644 --- a/pkg/hog/hog.go +++ b/pkg/hog/hog.go @@ -11,6 +11,7 @@ import ( "gopkg.in/yaml.v2" ) +// FILE name of the hog configuration file var FILE = ".hog.yml" var defaultHog = Hog{ @@ -28,9 +29,10 @@ type Hog struct { Buckets map[string][]string `yaml:"buckets"` } +// AddFiles to the hog file and return the bucket id func AddFiles(files []string) (string, error) { var groupID string - hogPath, err := GetPath() + hogPath, err := Path() if err != nil { return groupID, err } @@ -76,8 +78,9 @@ func AddFiles(files []string) (string, error) { return groupID, err } -func GetPath() (string, error) { - baseDir, err := GetBaseDir() +// Path return the path of the hog file +func Path() (string, error) { + baseDir, err := BaseDir() if err != nil { return "", err } @@ -85,7 +88,8 @@ func GetPath() (string, error) { return path.Join(baseDir, FILE), nil } -func GetBaseDir() (string, error) { +// BaseDir return the path of the directory that contains the hog file +func BaseDir() (string, error) { hogPen := os.Getenv("HOG_PEN") if len(hogPen) == 0 { return getHomeDir() @@ -115,9 +119,10 @@ func getHomeDir() (string, error) { return usr.HomeDir, nil } +// Get the hog object from the hog path func Get() (Hog, error) { var hog Hog - hogPath, err := GetPath() + hogPath, err := Path() if err != nil { return hog, err } @@ -134,8 +139,9 @@ func Get() (Hog, error) { return hog, nil } +// Save a hog object in the default hog path func Save(hog Hog) error { - hogPath, err := GetPath() + hogPath, err := Path() if err != nil { return err } @@ -143,6 +149,7 @@ func Save(hog Hog) error { return SaveToPath(hogPath, hog) } +// SaveToPath saves a hog object in a specific path func SaveToPath(hogPath string, hog Hog) error { content, err := yaml.Marshal(hog) if err != nil { @@ -152,6 +159,7 @@ func SaveToPath(hogPath string, hog Hog) error { return ioutil.WriteFile(hogPath, content, 0777) } +// FromPath load a hog object from an specific path func FromPath(hogPath string) (Hog, error) { hog := Hog{} @@ -179,6 +187,7 @@ func newGroupID(hog Hog) string { return id } +// CreateEmptyHogFile create a default hog object in the default hog path func CreateEmptyHogFile(hogPath string) error { return SaveToPath(hogPath, defaultHog) } diff --git a/pkg/hog/hog_test.go b/pkg/hog/hog_test.go index 8144796..25bd022 100644 --- a/pkg/hog/hog_test.go +++ b/pkg/hog/hog_test.go @@ -15,7 +15,7 @@ func TestGetBaseDir(t *testing.T) { t.Error(err) } - baseDir, err := GetBaseDir() + baseDir, err := BaseDir() if err != nil { t.Error(err) } @@ -25,7 +25,7 @@ func TestGetBaseDir(t *testing.T) { } _ = os.Setenv("HOG_PEN", fmt.Sprintf("%s/%s", os.TempDir(), time.Now().String())) - _, err = GetBaseDir() + _, err = BaseDir() if err == nil { t.Error("it should throw an error because the directory do not exist") } @@ -37,7 +37,7 @@ func TestGetPath(t *testing.T) { t.Error(err) } - hogPath, err := GetPath() + hogPath, err := Path() if err != nil { t.Error(err) } @@ -64,7 +64,7 @@ func TestAddFilesEmpty(t *testing.T) { } defer func() { - hogPath, _ := GetPath() + hogPath, _ := Path() _ = os.Remove(hogPath) }() @@ -95,7 +95,7 @@ func TestFromPath(t *testing.T) { t.Error(err) } - hogPath, err := GetPath() + hogPath, err := Path() if err != nil { t.Error(err) } @@ -147,7 +147,7 @@ func TestSave(t *testing.T) { } defer func() { - hogPath, _ := GetPath() + hogPath, _ := Path() _ = os.Remove(hogPath) }() diff --git a/pkg/hog/id.go b/pkg/hog/id.go index f961065..c8e8bfc 100644 --- a/pkg/hog/id.go +++ b/pkg/hog/id.go @@ -11,6 +11,7 @@ func init() { rand.Seed(time.Now().UnixNano()) } +// GetID returns a random id func GetID() string { return randStringRunes(7) } From 277a3c984f2b2ccca2d26da4eb0e95c048e763d0 Mon Sep 17 00:00:00 2001 From: jjzcru Date: Sun, 3 May 2020 19:22:18 -0500 Subject: [PATCH 10/10] Update changelog --- CHANGELOG.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bacc60..3b5bfe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,25 @@ +## [v0.2.0](https://github.com/jjzcru/hog/tree/v0.2.0) (2020-05-03) +[Release](https://github.com/jjzcru/hog/releases/tag/v0.2.0) + +**Commands 🤖:** +- **[set]** Create command +- **[get]** Create command +- **[remove]** Users are able to remove multiple buckets under the same command + +**Documentation 📖:** +- **[set]** Create documentation +- **[get]** Create documentation +- **[add]** Update documentation for the `url` flag +- Add `Usage` section in the `README.md` + +**Flags 🚩:** +- **--url** Add flag to **[add]** command + +**Misc 👾:** +- Use regex instead of exact match for the **[remove]** command +- Use regex instead of exact match for the **[update]** command +- Rename hog file from `hog.yml` to `.hog.yml` + ## [v0.1.0](https://github.com/jjzcru/hog/tree/v0.1.0) (2020-05-03) [Release](https://github.com/jjzcru/hog/releases/tag/v0.1.0)