Skip to content

Commit e78ac64

Browse files
authored
Merge pull request #5 from chclaus/feature/refactor-subcommand-and-flags
Feature/refactor subcommand and flags
2 parents 4ea321f + cacaad7 commit e78ac64

34 files changed

+435
-1027
lines changed

.goreleaser.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ archive:
3232
files:
3333
- LICENSE.txt
3434
- README.md
35+
- examples/config-sample.yaml
3536

3637
# Homebrew customization
3738
brew:

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tools.
2626
- [License](#license)
2727

2828
# Example
29-
<img src="demo_v0_2_0.gif?raw=" width="726px"></img>
29+
<img src="demo_v1_0_0.gif?raw=" width="672px"></img>
3030

3131
## Installation
3232
If you've go installed and your `$GOPATH` is set, you can easily install
@@ -111,9 +111,6 @@ Date conversions:
111111
- time nanos to RFC 3339
112112
- RFC 3339 to time millis
113113

114-
### Regex command
115-
TODO
116-
117114
### HTML command
118115
Escapes and unescapes HTML
119116
- transforms a string to an escaped html sequence
@@ -124,18 +121,29 @@ Escapes and unescapes HTML
124121
Starts a simple web server to serve static content. You can specify
125122
hostname and port and must set a folder to serve.
126123

127-
128124
## Configuration
129125
You can configure a some default behaviors of dt to fit your needs.
130126
Just place a file `.dt.yaml` into your home directory, you can configure
131127
the following options:
132128

133129
```yaml
134130
server:
135-
address: 0.0.0.0
136-
port: 3000
131+
port: 3001
132+
address: 127.0.0.1
133+
uuid:
134+
namespace: cacae610-c76a-4736-90ef-0271126b4346
135+
version: 4
136+
base64:
137+
encoding: std
138+
random:
139+
algorithm: complex
140+
length: 20
141+
hash:
142+
algorithm: bcrypt
143+
cost: 12
137144
```
138145
146+
For more informations, please take a look in the examples directory.
139147
140148
## Contributing
141149
Everyone is welcome to create pull requests for this project. If you're
@@ -146,5 +154,7 @@ If you've got an idea of a function that should find it's way into this
146154
project, but you won't implement it by yourself, please create a new
147155
issue.
148156
157+
Please ensure, that all contributions match the MIT license.
158+
149159
## License
150160
dt (dev-toolbelt) is released under the MIT license. See [LICENSE.txt](LICENSE.txt)

cmd/base64/base64.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ var base64Cmd = &cobra.Command{
4343

4444
func init() {
4545
cmd.RootCmd.AddCommand(base64Cmd)
46+
47+
base64Cmd.PersistentFlags().StringP("format", "f", "std",
48+
"the encoding format. Possible values are: std, stdRaw, url, urlRaw")
4649
}

cmd/base64/decode.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright © 2018 Christian Claus <[email protected]>
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package base64
22+
23+
import (
24+
"fmt"
25+
26+
"encoding/base64"
27+
"errors"
28+
"github.com/chclaus/dt/utils"
29+
"github.com/spf13/cobra"
30+
"os"
31+
)
32+
33+
// decodeCmd represents the std decode command
34+
var decodeCmd = &cobra.Command{
35+
Use: "decode",
36+
Short: "Decodes an encoded base64 string",
37+
Long: `Decodes an encoded base64 string. As default, the standard encoding 'std',
38+
defined in RFC 4648 is used. All possible encodings are:
39+
40+
std Uses the standard base64 encoding, as defined in RFC 4648
41+
stdRaw Uses the standard raw, unpadded base64 encoding as defined in RFC 4648 section 3.2
42+
url Uses the alternate base64 encoding defined in RFC 4648. Typically used in URLs and filenames
43+
urlRaw Uses the standard raw, unpadded alternate base64 encoding defined in RFC 4648
44+
`,
45+
Args: func(cmd *cobra.Command, args []string) error {
46+
if len(args) != 1 {
47+
return errors.New("you have to specify a string which should be decoded")
48+
}
49+
50+
return nil
51+
},
52+
Run: func(cmd *cobra.Command, args []string) {
53+
format := cmd.Flag("format").Value.String()
54+
switch format {
55+
case "std":
56+
fmt.Println(utils.DecodeBase64(base64.StdEncoding, args[0]))
57+
break
58+
case "stdRaw":
59+
fmt.Println(utils.DecodeBase64(base64.RawStdEncoding, args[0]))
60+
break
61+
case "url":
62+
fmt.Println(utils.DecodeBase64(base64.URLEncoding, args[0]))
63+
break
64+
case "urlRaw":
65+
fmt.Println(utils.DecodeBase64(base64.RawURLEncoding, args[0]))
66+
break
67+
default:
68+
fmt.Println(fmt.Errorf("the given format '%s' is unknown.", format))
69+
os.Exit(1)
70+
}
71+
},
72+
Example: `dt base64 decode -f std foo
73+
dt base64 decode -f stdRaw foo
74+
dt base64 decode -f url foo
75+
dt base64 decode -f urlRaw foo`,
76+
}
77+
78+
func init() {
79+
base64Cmd.AddCommand(decodeCmd)
80+
}

cmd/base64/encode.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright © 2018 Christian Claus <[email protected]>
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package base64
22+
23+
import (
24+
"fmt"
25+
26+
"encoding/base64"
27+
"errors"
28+
"github.com/chclaus/dt/utils"
29+
"github.com/spf13/cobra"
30+
"os"
31+
)
32+
33+
// encodeCmd represents the std encode command
34+
var encodeCmd = &cobra.Command{
35+
Use: "encode",
36+
Short: "Encodes a decoded base64 string",
37+
Long: `Encodes a decoded base64 string. As default, the standard encoding 'std',
38+
defined in RFC 4648 is used. All possible encodings are:
39+
40+
std Uses the standard base64 encoding, as defined in RFC 4648
41+
stdRaw Uses the standard raw, unpadded base64 encoding as defined in RFC 4648 section 3.2
42+
url Uses the alternate base64 encoding defined in RFC 4648. Typically used in URLs and filenames
43+
urlRaw Uses the standard raw, unpadded alternate base64 encoding defined in RFC 4648
44+
`,
45+
Args: func(cmd *cobra.Command, args []string) error {
46+
if len(args) != 1 {
47+
return errors.New("you have to specify a string which should be encoded")
48+
}
49+
50+
return nil
51+
},
52+
Run: func(cmd *cobra.Command, args []string) {
53+
format := cmd.Flag("format").Value.String()
54+
switch format {
55+
case "std":
56+
fmt.Println(utils.EncodeBase64(base64.StdEncoding, args[0]))
57+
break
58+
case "stdRaw":
59+
fmt.Println(utils.EncodeBase64(base64.RawStdEncoding, args[0]))
60+
break
61+
case "url":
62+
fmt.Println(utils.EncodeBase64(base64.URLEncoding, args[0]))
63+
break
64+
case "urlRaw":
65+
fmt.Println(utils.EncodeBase64(base64.RawURLEncoding, args[0]))
66+
break
67+
default:
68+
fmt.Println(fmt.Errorf("the given format '%s' is unknown.", format))
69+
os.Exit(1)
70+
}
71+
},
72+
Example: `dt base64 encode -f std foo
73+
dt base64 encode -f stdRaw foo
74+
dt base64 encode -f url foo
75+
dt base64 encode -f urlRaw foo`,
76+
}
77+
78+
func init() {
79+
base64Cmd.AddCommand(encodeCmd)
80+
}

cmd/base64/std.go

Lines changed: 0 additions & 60 deletions
This file was deleted.

cmd/base64/stdRaw.go

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)