Skip to content

Commit

Permalink
Refactored the whole uri command #4
Browse files Browse the repository at this point in the history
  • Loading branch information
chclaus committed May 10, 2018
1 parent 7a18048 commit f88b3a7
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 30 deletions.
59 changes: 59 additions & 0 deletions cmd/uri/decode.go
@@ -0,0 +1,59 @@
// Copyright © 2018 Christian Claus <ch.claus@me.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package uri

import (
"fmt"

"github.com/chclaus/dt/utils"
"github.com/spf13/cobra"
"errors"
"os"
)

// decodeCmd represents the uri decode command
var decodeCmd = &cobra.Command{
Use: "decode",
Short: "Decodes an URI-encoded string",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("you have to specify a string which should be decoded")
}

return nil
},
Run: func(cmd *cobra.Command, args []string) {
result, err := utils.DecodeUri(args[0])

if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Println(result)
return
},
Example: "dt uri decode http%3A%2F%2Fwww.github.com",
}

func init() {
uriCmd.AddCommand(decodeCmd)
}
50 changes: 50 additions & 0 deletions cmd/uri/encode.go
@@ -0,0 +1,50 @@
// Copyright © 2018 Christian Claus <ch.claus@me.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package uri

import (
"fmt"

"github.com/chclaus/dt/utils"
"github.com/spf13/cobra"
"errors"
)

// encodeCmd represents the std encode command
var encodeCmd = &cobra.Command{
Use: "encode",
Short: "Encodes an URI-encoded string",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("you have to specify a string which should be encoded")
}

return nil
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(utils.EncodeUri(args[0]))
},
Example: "dt uri encode http://www.github.com",
}

func init() {
uriCmd.AddCommand(encodeCmd)
}
32 changes: 2 additions & 30 deletions cmd/uri/uri.go
Expand Up @@ -21,47 +21,19 @@
package uri

import (
"fmt"

"github.com/chclaus/dt/cmd"
"github.com/chclaus/dt/utils"
"github.com/spf13/cobra"
"log"
)

// uriCmd represents the uri command
var uriCmd = &cobra.Command{
Use: "uri",
Short: "Encodes or decodes an URI",
Long: "Encodes or decodes an URI",
Run: func(cmd *cobra.Command, args []string) {
encode := cmd.Flag("encode").Value.String()
if encode != "" {
fmt.Println(utils.EncodeUri(encode))
return
}

decode := cmd.Flag("decode").Value.String()
if decode != "" {
result, err := utils.DecodeUri(decode)

if err != nil {
log.Fatal(err)
}

fmt.Println(result)
return
}

cmd.Help()
},
Example: `dt uri -e http://www.github.com'
dt uri -d http%3A%2F%2Fwww.github.com`,
Example: `dt uri encode http://www.github.com
dt uri decode http%3A%2F%2Fwww.github.com`,
}

func init() {
cmd.RootCmd.AddCommand(uriCmd)

uriCmd.Flags().StringP("encode", "e", "", "encodes an URI to a safe representation")
uriCmd.Flags().StringP("decode", "d", "", "decodes an already encoded URI")
}

0 comments on commit f88b3a7

Please sign in to comment.