-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from projectdiscovery/issue-103-add-callback-var
Add Callback Vars to flagset (#103)
- Loading branch information
Showing
6 changed files
with
156 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package goflags | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
// CallBackFunc | ||
type CallBackFunc func() | ||
|
||
// callBackVar | ||
type callBackVar struct { | ||
Value CallBackFunc | ||
} | ||
|
||
// Set | ||
func (c *callBackVar) Set(s string) error { | ||
v, err := strconv.ParseBool(s) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse callback flag") | ||
} | ||
if v { | ||
// if flag found execute callback | ||
c.Value() | ||
} | ||
return nil | ||
} | ||
|
||
// IsBoolFlag | ||
func (c *callBackVar) IsBoolFlag() bool { | ||
return true | ||
} | ||
|
||
// String | ||
func (c *callBackVar) String() string { | ||
return "false" | ||
} | ||
|
||
// CallbackVar adds a Callback flag with a longname | ||
func (flagSet *FlagSet) CallbackVar(callback CallBackFunc, long string, usage string) *FlagData { | ||
return flagSet.CallbackVarP(callback, long, "", usage) | ||
} | ||
|
||
// CallbackVarP adds a Callback flag with a shortname and longname | ||
func (flagSet *FlagSet) CallbackVarP(callback CallBackFunc, long, short string, usage string) *FlagData { | ||
if callback == nil { | ||
panic(fmt.Errorf("callback cannot be nil for flag -%v", long)) | ||
} | ||
flagData := &FlagData{ | ||
usage: usage, | ||
long: long, | ||
defaultValue: strconv.FormatBool(false), | ||
field: &callBackVar{Value: callback}, | ||
skipMarshal: true, | ||
} | ||
if short != "" { | ||
flagData.short = short | ||
flagSet.CommandLine.Var(flagData.field, short, usage) | ||
flagSet.flagKeys.Set(short, flagData) | ||
} | ||
flagSet.CommandLine.Var(flagData.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,64 @@ | ||
package goflags | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSuccessfulCallback(t *testing.T) { | ||
toolName := "tool_1" | ||
want := `updated successfully!` | ||
got := &bytes.Buffer{} | ||
|
||
flagSet := NewFlagSet() | ||
flagSet.CreateGroup("Update", "Update", | ||
flagSet.CallbackVar(updateCallbackFunc(toolName, got), "update", fmt.Sprintf("update %v to the latest released version", toolName)), | ||
flagSet.CallbackVarP(func() {}, "disable-update-check", "duc", "disable automatic update check"), | ||
) | ||
os.Args = []string{ | ||
os.Args[0], | ||
"-update", | ||
} | ||
err := flagSet.Parse() | ||
assert.Nil(t, err) | ||
assert.Equal(t, want, got.String()) | ||
tearDown(t.Name()) | ||
} | ||
|
||
func TestFailCallback(t *testing.T) { | ||
toolName := "tool_1" | ||
got := &bytes.Buffer{} | ||
|
||
if os.Getenv("IS_SUB_PROCESS") == "1" { | ||
flagSet := NewFlagSet() | ||
flagSet.CommandLine.SetOutput(got) | ||
flagSet.CreateGroup("Update", "Update", | ||
flagSet.CallbackVar(nil, "update", fmt.Sprintf("update %v to the latest released version", toolName)), | ||
) | ||
os.Args = []string{ | ||
os.Args[0], | ||
"-update", | ||
} | ||
_ = flagSet.Parse() | ||
} | ||
cmd := exec.Command(os.Args[0], "-test.run=TestFailCallback") | ||
cmd.Env = append(os.Environ(), "IS_SUB_PROCESS=1") | ||
err := cmd.Run() | ||
if e, ok := err.(*exec.ExitError); ok && !e.Success() { | ||
return | ||
} | ||
t.Fatalf("process ran with err %v, want exit error", err) | ||
tearDown(t.Name()) | ||
} | ||
|
||
func updateCallbackFunc(toolName string, cliOutput io.Writer) func() { | ||
return func() { | ||
fmt.Fprintf(cliOutput, "updated successfully!") | ||
} | ||
} |
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