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:
Hon Kwok
2023-10-02 17:21:40 -07:00
committed by GitHub
co-authored by mcastorina
parent de535071e1
commit 4598244167
13 changed files with 70 additions and 53 deletions
+4 -3
View File
@@ -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")
}
}
+14 -3
View File
@@ -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."),
+1 -4
View File
@@ -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, " ")
}
+2 -1
View File
@@ -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 {
+1 -4
View File
@@ -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, " ")
}
+5 -6
View File
@@ -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)
}
+3 -4
View File
@@ -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, " ")
}
+23 -9
View File
@@ -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"}
+1 -3
View File
@@ -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, " ")
}
+2 -2
View File
@@ -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 {
+2 -4
View File
@@ -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, " ")