Use placeholder as default if field left empty and is required (#1642)
* Use placeholder as default if field left empty and is required Co-authored-by: mcastorina <[email protected]> * Drop unused func * uncomment azure source * update wording --------- Co-authored-by: mcastorina <[email protected]>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/muesli/reflow/truncate"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/tui/components/textinputs"
|
||||
)
|
||||
|
||||
// TruncateString is a convenient wrapper around truncate.TruncateString.
|
||||
@@ -14,11 +15,11 @@ func TruncateString(s string, max int) string {
|
||||
return truncate.StringWithTail(s, uint(max), "…")
|
||||
}
|
||||
|
||||
func SummarizeSource(keys []string, inputs map[string]string, labels map[string]string) string {
|
||||
func SummarizeSource(keys []string, inputs map[string]textinputs.Input, labels map[string]string) string {
|
||||
summary := strings.Builder{}
|
||||
for _, key := range keys {
|
||||
if inputs[key] != "" {
|
||||
summary.WriteString("\t" + labels[key] + ": " + inputs[key] + "\n")
|
||||
if inputs[key].Value != "" {
|
||||
summary.WriteString("\t" + labels[key] + ": " + inputs[key].Value + "\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,11 +47,22 @@ type InputConfig struct {
|
||||
Placeholder string
|
||||
}
|
||||
|
||||
func (m Model) GetInputs() map[string]string {
|
||||
inputs := make(map[string]string)
|
||||
type Input struct {
|
||||
Value string
|
||||
IsDefault bool
|
||||
}
|
||||
|
||||
func (m Model) GetInputs() map[string]Input {
|
||||
inputs := make(map[string]Input)
|
||||
|
||||
for i, input := range m.inputs {
|
||||
inputs[m.configs[i].Key] = input.Value()
|
||||
isDefault := false
|
||||
value := input.Value()
|
||||
if value == "" && m.configs[i].Required {
|
||||
isDefault = true
|
||||
value = input.Placeholder
|
||||
}
|
||||
inputs[m.configs[i].Key] = Input{Value: value, IsDefault: isDefault}
|
||||
}
|
||||
|
||||
return inputs
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package source_configure
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -49,7 +50,7 @@ func GetTrufflehogConfiguration() truffleCmdModel {
|
||||
Key: "concurrency",
|
||||
Required: false,
|
||||
Help: "Number of concurrent workers.",
|
||||
Placeholder: "1",
|
||||
Placeholder: strconv.Itoa(runtime.NumCPU()),
|
||||
}
|
||||
|
||||
return truffleCmdModel{textinputs.New([]textinputs.InputConfig{jsonOutput, verification, verifiedResults, excludeDetectors, concurrency}).SetSkip(true)}
|
||||
@@ -59,25 +60,25 @@ func (m truffleCmdModel) Cmd() string {
|
||||
var command []string
|
||||
inputs := m.GetInputs()
|
||||
|
||||
if isTrue(inputs["json"]) {
|
||||
if isTrue(inputs["json"].Value) {
|
||||
command = append(command, "--json")
|
||||
}
|
||||
|
||||
if isTrue(inputs["no-verification"]) {
|
||||
if isTrue(inputs["no-verification"].Value) {
|
||||
command = append(command, "--no-verification")
|
||||
}
|
||||
|
||||
if isTrue(inputs["only-verified"]) {
|
||||
if isTrue(inputs["only-verified"].Value) {
|
||||
command = append(command, "--only-verified")
|
||||
}
|
||||
|
||||
if inputs["exclude_detectors"] != "" {
|
||||
cmd := "--exclude-detectors=" + strings.ReplaceAll(inputs["exclude_detectors"], " ", "")
|
||||
if inputs["exclude_detectors"].Value != "" {
|
||||
cmd := "--exclude-detectors=" + strings.ReplaceAll(inputs["exclude_detectors"].Value, " ", "")
|
||||
command = append(command, cmd)
|
||||
}
|
||||
|
||||
if inputs["concurrency"] != "" {
|
||||
command = append(command, "--concurrency="+inputs["concurrency"])
|
||||
if inputs["concurrency"].Value != "" {
|
||||
command = append(command, "--concurrency="+inputs["concurrency"].Value)
|
||||
}
|
||||
|
||||
return strings.Join(command, " ")
|
||||
@@ -90,8 +91,8 @@ func (m truffleCmdModel) Summary() string {
|
||||
inputs := m.GetInputs()
|
||||
labels := m.GetLabels()
|
||||
for _, key := range keys {
|
||||
if inputs[key] != "" {
|
||||
summary.WriteString("\t" + labels[key] + ": " + inputs[key] + "\n")
|
||||
if inputs[key].Value != "" {
|
||||
summary.WriteString("\t" + labels[key] + ": " + inputs[key].Value + "\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,7 @@ func New(c common.Common) *SourceSelect {
|
||||
OssItem("GCS (Google Cloud Storage)", "Scan a Google Cloud Storage instance."),
|
||||
// Enterprise sources.
|
||||
EnterpriseItem("Artifactory", "Scan JFrog Artifactory packages."),
|
||||
EnterpriseItem("Azure Repos", "Scan Microsoft Azure repositories."),
|
||||
EnterpriseItem("BitBucket", "Scan Atlassian's Git-based source code repository hosting service."),
|
||||
EnterpriseItem("Buildkite", "Scan Buildkite, a CI/CD platform."),
|
||||
EnterpriseItem("Confluence", "Scan Atlassian's web-based wiki and knowledge base."),
|
||||
|
||||
@@ -27,10 +27,7 @@ func (m circleCiCmdModel) Cmd() string {
|
||||
command = append(command, "trufflehog", "circleci")
|
||||
|
||||
inputs := m.GetInputs()
|
||||
|
||||
if inputs["token"] != "" {
|
||||
command = append(command, "--token="+inputs["token"])
|
||||
}
|
||||
command = append(command, "--token="+inputs["token"].Value)
|
||||
|
||||
return strings.Join(command, " ")
|
||||
}
|
||||
|
||||
@@ -29,7 +29,8 @@ func (m dockerCmdModel) Cmd() string {
|
||||
command = append(command, "trufflehog", "docker")
|
||||
|
||||
inputs := m.GetInputs()
|
||||
vals := inputs["images"]
|
||||
vals := inputs["images"].Value
|
||||
|
||||
if vals != "" {
|
||||
images := strings.Fields(vals)
|
||||
for _, image := range images {
|
||||
|
||||
@@ -28,10 +28,7 @@ func (m fsModel) Cmd() string {
|
||||
command = append(command, "trufflehog", "filesystem")
|
||||
|
||||
inputs := m.GetInputs()
|
||||
|
||||
if inputs["path"] != "" {
|
||||
command = append(command, inputs["path"])
|
||||
}
|
||||
command = append(command, inputs["path"].Value)
|
||||
|
||||
return strings.Join(command, " ")
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@ type gcsCmdModel struct {
|
||||
func GetFields() gcsCmdModel {
|
||||
projectId := textinputs.InputConfig{
|
||||
Label: "Project ID",
|
||||
Key: "project_id",
|
||||
Key: "project-id",
|
||||
Required: true,
|
||||
Placeholder: "my-project",
|
||||
Placeholder: "trufflehog-testing",
|
||||
}
|
||||
|
||||
return gcsCmdModel{textinputs.New([]textinputs.InputConfig{projectId})}
|
||||
@@ -27,9 +27,8 @@ func (m gcsCmdModel) Cmd() string {
|
||||
command = append(command, "trufflehog", "gcs")
|
||||
|
||||
inputs := m.GetInputs()
|
||||
if inputs["project_id"] != "" {
|
||||
command = append(command, "--project_id="+inputs["project_id"])
|
||||
}
|
||||
|
||||
command = append(command, "--project-id="+inputs["project-id"].Value)
|
||||
|
||||
command = append(command, "--cloud-environment")
|
||||
return strings.Join(command, " ")
|
||||
@@ -39,6 +38,6 @@ func (m gcsCmdModel) Summary() string {
|
||||
inputs := m.GetInputs()
|
||||
labels := m.GetLabels()
|
||||
|
||||
keys := []string{"project_id"}
|
||||
keys := []string{"project-id"}
|
||||
return common.SummarizeSource(keys, inputs, labels)
|
||||
}
|
||||
|
||||
@@ -15,8 +15,9 @@ func GetFields() gitCmdModel {
|
||||
uri := textinputs.InputConfig{
|
||||
Label: "Git URI",
|
||||
Key: "uri",
|
||||
Help: "file:// for local git repos",
|
||||
Required: true,
|
||||
Placeholder: "[email protected]:trufflesecurity/trufflehog.git.",
|
||||
Placeholder: "[email protected]:trufflesecurity/trufflehog.git",
|
||||
}
|
||||
|
||||
return gitCmdModel{textinputs.New([]textinputs.InputConfig{uri})}
|
||||
@@ -28,9 +29,7 @@ func (m gitCmdModel) Cmd() string {
|
||||
|
||||
inputs := m.GetInputs()
|
||||
|
||||
if inputs["uri"] != "" {
|
||||
command = append(command, inputs["uri"])
|
||||
}
|
||||
command = append(command, inputs["uri"].Value)
|
||||
|
||||
return strings.Join(command, " ")
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ func GetFields() githubCmdModel {
|
||||
org := textinputs.InputConfig{
|
||||
Label: "Organization",
|
||||
Key: "org",
|
||||
Required: false,
|
||||
Required: true,
|
||||
Help: "GitHub organization to scan.",
|
||||
Placeholder: "trufflesecurity",
|
||||
}
|
||||
@@ -27,7 +27,7 @@ func GetFields() githubCmdModel {
|
||||
repo := textinputs.InputConfig{
|
||||
Label: "Repository",
|
||||
Key: "repo",
|
||||
Required: false,
|
||||
Required: true,
|
||||
Help: "GitHub repo to scan.",
|
||||
Placeholder: "https://github.com/trufflesecurity/test_keys",
|
||||
}
|
||||
@@ -35,25 +35,39 @@ func GetFields() githubCmdModel {
|
||||
return githubCmdModel{textinputs.New([]textinputs.InputConfig{org, repo})}
|
||||
}
|
||||
|
||||
// Handle default values since GitHub flags are OR operations
|
||||
func (m githubCmdModel) GetSpecialInputs() map[string]textinputs.Input {
|
||||
inputs := m.GetInputs()
|
||||
if inputs["org"].IsDefault != inputs["repo"].IsDefault {
|
||||
if inputs["org"].IsDefault {
|
||||
delete(inputs, "org")
|
||||
}
|
||||
if inputs["repo"].IsDefault {
|
||||
delete(inputs, "repo")
|
||||
}
|
||||
}
|
||||
|
||||
return inputs
|
||||
}
|
||||
|
||||
func (m githubCmdModel) Cmd() string {
|
||||
var command []string
|
||||
command = append(command, "trufflehog", "github")
|
||||
inputs := m.GetSpecialInputs()
|
||||
|
||||
inputs := m.GetInputs()
|
||||
|
||||
if inputs["org"] != "" {
|
||||
command = append(command, "--org="+inputs["org"])
|
||||
if inputs["org"].Value != "" {
|
||||
command = append(command, "--org="+inputs["org"].Value)
|
||||
}
|
||||
|
||||
if inputs["repo"] != "" {
|
||||
command = append(command, "--repo="+inputs["repo"])
|
||||
if inputs["repo"].Value != "" {
|
||||
command = append(command, "--repo="+inputs["repo"].Value)
|
||||
}
|
||||
|
||||
return strings.Join(command, " ")
|
||||
}
|
||||
|
||||
func (m githubCmdModel) Summary() string {
|
||||
inputs := m.GetInputs()
|
||||
inputs := m.GetSpecialInputs()
|
||||
labels := m.GetLabels()
|
||||
|
||||
keys := []string{"org", "repo"}
|
||||
|
||||
@@ -29,9 +29,7 @@ func (m gitlabCmdModel) Cmd() string {
|
||||
|
||||
inputs := m.GetInputs()
|
||||
|
||||
if inputs["token"] != "" {
|
||||
command = append(command, "--token="+inputs["token"])
|
||||
}
|
||||
command = append(command, "--token="+inputs["token"].Value)
|
||||
|
||||
return strings.Join(command, " ")
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ func GetFields() s3CmdModel {
|
||||
Label: "S3 bucket name(s)",
|
||||
Key: "buckets",
|
||||
Required: true,
|
||||
Placeholder: "my-bucket-name",
|
||||
Placeholder: "truffletestbucket",
|
||||
Help: "Buckets to scan. Separate by space if multiple.",
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ func (m s3CmdModel) Cmd() string {
|
||||
command = append(command, "trufflehog", "s3")
|
||||
|
||||
inputs := m.GetInputs()
|
||||
vals := inputs["buckets"]
|
||||
vals := inputs["buckets"].Value
|
||||
if vals != "" {
|
||||
buckets := strings.Fields(vals)
|
||||
for _, bucket := range buckets {
|
||||
|
||||
@@ -64,10 +64,8 @@ func (m syslogCmdModel) Cmd() string {
|
||||
syslogKeys := [5]string{"address", "protocol", "cert", "key", "format"}
|
||||
|
||||
for _, key := range syslogKeys {
|
||||
if inputs[key] != "" {
|
||||
flag := "--" + key + "=" + inputs[key]
|
||||
command = append(command, flag)
|
||||
}
|
||||
flag := "--" + key + "=" + inputs[key].Value
|
||||
command = append(command, flag)
|
||||
}
|
||||
|
||||
return strings.Join(command, " ")
|
||||
|
||||
Reference in New Issue
Block a user