[analyze] Add environment variable support to text inputs
The values will only be replaced if the input begins with a dollar sign,
and allows for multiple expansions like "${A} and ${B}". The requirement
for the first character to be a dollar sign prevents any issues with a
dollar sign embedded in a key itself.
This commit is contained in:
@@ -25,14 +25,18 @@ func NewFormPage(c *common.Common, keyType string) FormPage {
|
||||
switch strings.ToLower(keyType) {
|
||||
case "twilio":
|
||||
inputs = []textinputs.InputConfig{{
|
||||
Label: "SID",
|
||||
Key: "sid",
|
||||
Required: true,
|
||||
Label: "SID",
|
||||
Key: "sid",
|
||||
Required: true,
|
||||
AllowEnv: true,
|
||||
Placeholder: "$TWILIO_SID",
|
||||
}, {
|
||||
Label: "Token",
|
||||
Key: "key",
|
||||
Required: true,
|
||||
RedactInput: true,
|
||||
AllowEnv: true,
|
||||
Placeholder: "$TWILIO_TOKEN",
|
||||
}}
|
||||
case "shopify":
|
||||
inputs = []textinputs.InputConfig{{
|
||||
@@ -40,25 +44,33 @@ func NewFormPage(c *common.Common, keyType string) FormPage {
|
||||
Key: "key",
|
||||
Required: true,
|
||||
RedactInput: true,
|
||||
AllowEnv: true,
|
||||
Placeholder: "$SHOPIFY_TOKEN",
|
||||
}, {
|
||||
Label: "Shopify URL",
|
||||
Key: "url",
|
||||
Required: true,
|
||||
Label: "Shopify URL",
|
||||
Key: "url",
|
||||
Required: true,
|
||||
AllowEnv: true,
|
||||
Placeholder: "$SHOPIFY_URL",
|
||||
}}
|
||||
default:
|
||||
kt := strings.ToUpper(keyType)
|
||||
inputs = []textinputs.InputConfig{{
|
||||
Label: "Secret",
|
||||
Key: "key",
|
||||
Required: true,
|
||||
RedactInput: true,
|
||||
AllowEnv: true,
|
||||
Placeholder: fmt.Sprintf("$%s_TOKEN", kt),
|
||||
}}
|
||||
}
|
||||
|
||||
// Always append a log file option.
|
||||
inputs = append(inputs, textinputs.InputConfig{
|
||||
Label: "Log file",
|
||||
Help: "Log HTTP requests that analysis performs to this file",
|
||||
Key: "log_file",
|
||||
Label: "Log file",
|
||||
Help: "Log HTTP requests that analysis performs to this file",
|
||||
Key: "log_file",
|
||||
AllowEnv: true,
|
||||
})
|
||||
|
||||
form := textinputs.New(inputs).
|
||||
|
||||
@@ -4,6 +4,7 @@ package textinputs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
@@ -47,6 +48,7 @@ type InputConfig struct {
|
||||
Required bool
|
||||
Placeholder string
|
||||
RedactInput bool
|
||||
AllowEnv bool
|
||||
}
|
||||
|
||||
type Input struct {
|
||||
@@ -64,6 +66,9 @@ func (m Model) GetInputs() map[string]Input {
|
||||
isDefault = true
|
||||
value = input.Placeholder
|
||||
}
|
||||
if m.configs[i].AllowEnv && strings.HasPrefix(value, "$") {
|
||||
value = os.ExpandEnv(value)
|
||||
}
|
||||
inputs[m.configs[i].Key] = Input{Value: value, IsDefault: isDefault}
|
||||
}
|
||||
|
||||
@@ -191,15 +196,10 @@ func (m Model) View() string {
|
||||
b.WriteString(m.GetLabel(m.configs[i]))
|
||||
}
|
||||
|
||||
// Make a copy of the input so we don't update the display.
|
||||
input := m.inputs[i]
|
||||
if val := input.Value(); len(val) > 4 && m.configs[i].RedactInput {
|
||||
if len(val) > 10 {
|
||||
// start***end
|
||||
input.SetValue(val[:4] + strings.Repeat("*", len(val)-8) + val[len(val)-4:])
|
||||
} else {
|
||||
// start***
|
||||
input.SetValue(val[:4] + strings.Repeat("*", len(val)-4))
|
||||
}
|
||||
if redacted, ok := redactFilter(input.Value(), m.configs[i]); ok {
|
||||
input.SetValue(redacted)
|
||||
}
|
||||
b.WriteString(input.View())
|
||||
b.WriteRune('\n')
|
||||
@@ -221,6 +221,21 @@ func (m Model) View() string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func redactFilter(input string, config InputConfig) (string, bool) {
|
||||
if !config.RedactInput || len(input) <= 4 {
|
||||
return input, false
|
||||
}
|
||||
if config.AllowEnv && strings.HasPrefix(input, "$") {
|
||||
return input, false
|
||||
}
|
||||
if len(input) <= 10 {
|
||||
// start***
|
||||
return input[:4] + strings.Repeat("*", len(input)-4), true
|
||||
}
|
||||
// start***end
|
||||
return input[:4] + strings.Repeat("*", len(input)-8) + input[len(input)-4:], true
|
||||
}
|
||||
|
||||
func (m Model) GetLabel(c InputConfig) string {
|
||||
var label strings.Builder
|
||||
|
||||
|
||||
Reference in New Issue
Block a user