Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
b7b2ee34
Commit
b7b2ee34
authored
Jan 02, 2015
by
Daniel Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add flag helper func
parent
6c6cdac0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
0 deletions
+44
-0
quantity.go
pkg/api/resource/quantity.go
+32
-0
quantity_test.go
pkg/api/resource/quantity_test.go
+12
-0
No files found.
pkg/api/resource/quantity.go
View file @
b7b2ee34
...
...
@@ -18,6 +18,8 @@ package resource
import
(
"errors"
"flag"
"fmt"
"math/big"
"regexp"
"strings"
...
...
@@ -372,3 +374,33 @@ func (q *Quantity) Copy() *Quantity {
Format
:
q
.
Format
,
}
}
// qFlag is a helper type for the Flag function
type
qFlag
struct
{
dest
*
Quantity
}
func
(
qf
qFlag
)
Set
(
val
string
)
error
{
q
,
err
:=
ParseQuantity
(
val
)
if
err
!=
nil
{
return
err
}
// This copy is OK because q will not be referenced again.
*
qf
.
dest
=
*
q
return
nil
}
func
(
qf
qFlag
)
String
()
string
{
return
qf
.
dest
.
String
()
}
// QuantityFlag is a helper that makes a quantity flag (using standard flag package).
// Will panic if defaultValue is not a valid quantity.
func
QuantityFlag
(
flagName
,
defaultValue
,
description
string
)
*
Quantity
{
q
,
err
:=
ParseQuantity
(
defaultValue
)
if
err
!=
nil
{
panic
(
fmt
.
Errorf
(
"can't use %v as a quantity: %v"
,
defaultValue
,
err
))
}
flag
.
Var
(
qFlag
{
q
},
flagName
,
description
)
return
q
}
pkg/api/resource/quantity_test.go
View file @
b7b2ee34
...
...
@@ -25,6 +25,10 @@ import (
"speter.net/go/exp/math/dec/inf"
)
var
(
testQuantityFlag
=
QuantityFlag
(
"quantityFlag"
,
"1M"
,
"dummy flag for testing the quantity flag mechanism"
)
)
func
dec
(
i
int64
,
exponent
int
)
*
inf
.
Dec
{
// See the below test-- scale is the negative of an exponent.
return
inf
.
NewDec
(
i
,
inf
.
Scale
(
-
exponent
))
...
...
@@ -425,3 +429,11 @@ func TestCopy(t *testing.T) {
t
.
Errorf
(
"Copy didn't"
)
}
}
func
TestQFlagSet
(
t
*
testing
.
T
)
{
qf
:=
qFlag
{
&
Quantity
{}}
qf
.
Set
(
"1Ki"
)
if
e
,
a
:=
"1Ki"
,
qf
.
String
();
e
!=
a
{
t
.
Errorf
(
"Unexpected result %v != %v"
,
e
,
a
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment