-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
redact: allow users to redact sensitive info locally
- Loading branch information
Showing
5 changed files
with
128 additions
and
15 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
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,61 @@ | ||
package redact | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/charmbracelet/huh" | ||
"github.com/getsavvyinc/savvy-cli/server" | ||
"github.com/getsavvyinc/savvy-cli/slice" | ||
"github.com/getsavvyinc/savvy-cli/theme" | ||
) | ||
|
||
// Commands allows users to redact one or more commands. | ||
// Commands returns a new slice of commands with the sensitive data redacted or removed. | ||
// | ||
// NOTE: It is possible for users to completely remove some commands. | ||
func Commands(cmds []*server.RecordedCommand) ([]*server.RecordedCommand, error) { | ||
var fs []huh.Field | ||
description := "Replace sensitive data with <placeholders>. To remove a command, simply delete the text." | ||
note := huh.NewNote().Title("Redact Secrets and PII").Description(description) | ||
fs = append(fs, note) | ||
|
||
for i, cmd := range cmds { | ||
fs = append(fs, RedactCommand(cmd.Command, strconv.Itoa(i))) | ||
} | ||
|
||
customTheme := theme.New() | ||
|
||
group := huh.NewGroup(fs...).Title("Redact Commands").WithTheme(customTheme) | ||
|
||
if err := huh.NewForm(group).WithTheme(customTheme).Run(); err != nil { | ||
err := fmt.Errorf("failed to run redaction form: %w", err) | ||
return nil, err | ||
} | ||
|
||
for _, f := range fs { | ||
in, ok := f.(*huh.Input) | ||
if !ok { | ||
continue | ||
} | ||
strVal, ok := in.GetValue().(string) | ||
if !ok { | ||
continue | ||
} | ||
|
||
idx, err := strconv.Atoi(in.GetKey()) | ||
if err != nil { | ||
continue | ||
} | ||
cmds[idx].Command = strVal | ||
} | ||
|
||
redacted := slice.Filter(cmds, func(cmd *server.RecordedCommand) bool { | ||
return cmd.Command != "" || cmd.FileInfo != nil | ||
}) | ||
return redacted, nil | ||
} | ||
|
||
func RedactCommand(cmd string, key string) huh.Field { | ||
return huh.NewInput().Value(&cmd).Key(key).WithTheme(theme.New()) | ||
} |
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,36 @@ | ||
package theme | ||
|
||
import ( | ||
catppuccin "github.com/catppuccin/go" | ||
"github.com/charmbracelet/huh" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
func New() *huh.Theme { | ||
t := huh.ThemeDracula() | ||
|
||
light := catppuccin.Latte | ||
dark := catppuccin.Mocha | ||
var ( | ||
subtext0 = lipgloss.AdaptiveColor{Light: light.Subtext0().Hex, Dark: dark.Subtext0().Hex} | ||
overlay1 = lipgloss.AdaptiveColor{Light: light.Overlay1().Hex, Dark: dark.Overlay1().Hex} | ||
) | ||
|
||
f := &t.Focused | ||
f.SelectedPrefix = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#02CF92", Dark: "#02A877"}).SetString("✓ ") | ||
f.UnselectedPrefix = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "", Dark: "243"}).SetString("• ") | ||
|
||
// light := catppuccin.Latte | ||
// dark := catppuccin.Mocha | ||
// green = lipgloss.AdaptiveColor{Light: light.Green().Hex, Dark: dark.Green().Hex} | ||
|
||
t.Help.Ellipsis.Foreground(subtext0) | ||
t.Help.ShortKey.Foreground(subtext0) | ||
t.Help.ShortDesc.Foreground(overlay1) | ||
t.Help.ShortSeparator.Foreground(subtext0) | ||
t.Help.FullKey.Foreground(subtext0) | ||
t.Help.FullDesc.Foreground(overlay1) | ||
t.Help.FullSeparator.Foreground(subtext0) | ||
|
||
return t | ||
} |