-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to convert file size with unit to bytes (#112)
* Add support to convert file size with unit to bytes (#111) * move file size to byte len logic to utils pkg * go version update * version bump --------- Co-authored-by: sandeep <8293321+ehsandeep@users.noreply.github.com> Co-authored-by: Mzack9999 <mzack9999@protonmail.com>
- Loading branch information
1 parent
262b466
commit cfd5bff
Showing
8 changed files
with
153 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package goflags | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
fileutil "github.com/projectdiscovery/utils/file" | ||
) | ||
|
||
type Size int | ||
|
||
func (s *Size) Set(size string) error { | ||
sizeInBytes, err := fileutil.FileSizeToByteLen(size) | ||
if err != nil { | ||
return err | ||
} | ||
*s = Size(sizeInBytes) | ||
return nil | ||
} | ||
|
||
func (s *Size) String() string { | ||
return strconv.Itoa(int(*s)) | ||
} | ||
|
||
// SizeVar converts the given fileSize with a unit (kb, mb, gb, or tb) to bytes. | ||
// For example, '2kb' will be converted to 2048. | ||
// If no unit is provided, it will fallback to mb. e.g: '2' will be converted to 2097152. | ||
func (flagSet *FlagSet) SizeVar(field *Size, long string, defaultValue string, usage string) *FlagData { | ||
return flagSet.SizeVarP(field, long, "", defaultValue, usage) | ||
} | ||
|
||
// SizeVarP converts the given fileSize with a unit (kb, mb, gb, or tb) to bytes. | ||
// For example, '2kb' will be converted to 2048. | ||
// If no unit is provided, it will fallback to mb. e.g: '2' will be converted to 2097152. | ||
func (flagSet *FlagSet) SizeVarP(field *Size, long, short string, defaultValue string, usage string) *FlagData { | ||
if field == nil { | ||
panic(fmt.Errorf("field cannot be nil for flag -%v", long)) | ||
} | ||
if defaultValue != "" { | ||
if err := field.Set(defaultValue); err != nil { | ||
panic(fmt.Errorf("failed to set default value for flag -%v: %v", long, err)) | ||
} | ||
} | ||
flagData := &FlagData{ | ||
usage: usage, | ||
long: long, | ||
defaultValue: defaultValue, | ||
} | ||
if short != "" { | ||
flagData.short = short | ||
flagSet.CommandLine.Var(field, short, usage) | ||
flagSet.flagKeys.Set(short, flagData) | ||
} | ||
flagSet.CommandLine.Var(field, long, usage) | ||
flagSet.flagKeys.Set(long, flagData) | ||
return flagData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package goflags | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSizeVar(t *testing.T) { | ||
t.Run("valid-size", func(t *testing.T) { | ||
var fileSize Size | ||
flagSet := NewFlagSet() | ||
flagSet.CreateGroup("Config", "Config", | ||
flagSet.SizeVarP(&fileSize, "max-size", "ms", "", "max size of the file"), | ||
) | ||
os.Args = []string{ | ||
os.Args[0], | ||
"-max-size", "2kb", | ||
} | ||
err := flagSet.Parse() | ||
assert.Nil(t, err) | ||
assert.Equal(t, Size(2048), fileSize) | ||
tearDown(t.Name()) | ||
}) | ||
|
||
t.Run("default-value", func(t *testing.T) { | ||
var fileSize Size | ||
flagSet := NewFlagSet() | ||
flagSet.CreateGroup("Config", "Config", | ||
flagSet.SizeVarP(&fileSize, "max-size", "ms", "2kb", "max size of the file"), | ||
) | ||
os.Args = []string{ | ||
os.Args[0], | ||
} | ||
err := flagSet.Parse() | ||
assert.Nil(t, err) | ||
assert.Equal(t, Size(2048), fileSize) | ||
tearDown(t.Name()) | ||
}) | ||
|
||
t.Run("without-unit", func(t *testing.T) { | ||
var fileSize Size | ||
err := fileSize.Set("2") | ||
assert.Nil(t, err) | ||
assert.Equal(t, Size(2097152), fileSize) | ||
tearDown(t.Name()) | ||
}) | ||
|
||
t.Run("invalid-size-unit", func(t *testing.T) { | ||
var fileSize Size | ||
err := fileSize.Set("2kilobytes") | ||
assert.NotNil(t, err) | ||
assert.ErrorContains(t, err, "parse error") | ||
tearDown(t.Name()) | ||
}) | ||
} |