Skip to content

Commit

Permalink
Merge pull request #52 from SpatiumPortae/f/prompt-overwrite
Browse files Browse the repository at this point in the history
feat: file overwrite prompt (with configurability)
  • Loading branch information
mellonnen committed Feb 24, 2023
2 parents 88805c3 + 68826ab commit 190b704
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 42 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/charmbracelet/bubbletea v0.23.2
github.com/charmbracelet/lipgloss v0.6.0
github.com/docker/go-connections v0.4.0
github.com/erikgeiser/promptkit v0.8.0
github.com/fatih/structs v1.1.0
github.com/klauspost/pgzip v1.2.5
github.com/mattn/go-runewidth v0.0.14
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po=
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/erikgeiser/promptkit v0.8.0 h1:bvOzPs6RLyfRZDSgVWOghQEiBSRHQ3zmDdxcV8zOc+E=
github.com/erikgeiser/promptkit v0.8.0/go.mod h1:QxyFbCrrj20PyvV5b+ckWPozbgX11s04GeRlmTCIMTo=
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
Expand Down
16 changes: 9 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,27 @@ const CONFIG_FILE_NAME = "config"
const CONFIG_FILE_EXT = "yml"

type Config struct {
Relay string `mapstructure:"relay"`
Verbose bool `mapstructure:"verbose"`
Relay string `mapstructure:"relay"`
Verbose bool `mapstructure:"verbose"`
PromptOverwriteFiles bool `mapstructure:"prompt_overwrite_files"`
}

func GetDefault() Config {
return Config{
Relay: "167.71.65.96:80",
Verbose: false,
Relay: "167.71.65.96:80",
Verbose: false,
PromptOverwriteFiles: true,
}
}

func ToMap(config Config) map[string]any {
p := map[string]any{}
m := map[string]any{}
for _, field := range structs.Fields(config) {
key := field.Tag("mapstructure")
value := field.Value()
p[key] = value
m[key] = value
}
return p
return m
}

func ToYaml(config Config) []byte {
Expand Down
30 changes: 26 additions & 4 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
const SEND_TEMP_FILE_NAME_PREFIX = "portal-send-temp"
const RECEIVE_TEMP_FILE_NAME_PREFIX = "portal-receive-temp"

type OverwriteDecider func(fileName string) (bool, error)

func ReadFiles(fileNames []string) ([]*os.File, error) {
var files []*os.File
for _, fileName := range fileNames {
Expand All @@ -27,9 +29,9 @@ func ReadFiles(fileNames []string) ([]*os.File, error) {
return files, nil
}

// ArchiveAndCompressFiles tars and gzip-compresses files into a temporary file, returning it
// PackFiles tars and gzip-compresses files into a temporary file, returning it
// along with the resulting size
func ArchiveAndCompressFiles(files []*os.File) (*os.File, int64, error) {
func PackFiles(files []*os.File) (*os.File, int64, error) {
// chained writers -> writing to tw writes to gw -> writes to temporary file
tempFile, err := os.CreateTemp(os.TempDir(), SEND_TEMP_FILE_NAME_PREFIX)
if err != nil {
Expand Down Expand Up @@ -60,9 +62,9 @@ func ArchiveAndCompressFiles(files []*os.File) (*os.File, int64, error) {
return tempFile, fileInfo.Size(), nil
}

// DecompressAndUnarchiveBytes gzip-decompresses and un-tars files into the current working directory
// UnpackFiles gzip-decompresses and un-tars files into the current working directory
// and returns the names and decompressed size of the created files
func DecompressAndUnarchiveBytes(reader io.Reader) ([]string, int64, error) {
func UnpackFiles(reader io.Reader, decideOverwrite OverwriteDecider) ([]string, int64, error) {
// chained readers -> gr reads from reader -> tr reads from gr
gr, err := pgzip.NewReader(reader)
if err != nil {
Expand Down Expand Up @@ -94,24 +96,39 @@ func DecompressAndUnarchiveBytes(reader io.Reader) ([]string, int64, error) {
fileTarget := filepath.Join(cwd, header.Name)

switch header.Typeflag {

case tar.TypeDir:
if _, err := os.Stat(fileTarget); err != nil {
if err := os.MkdirAll(fileTarget, 0755); err != nil {
return nil, 0, err
}
}

case tar.TypeReg:
if fileExists(fileTarget) {
shouldOverwrite, err := decideOverwrite(fileTarget)
if err != nil {
return nil, 0, err
}
if !shouldOverwrite {
continue
}
}

f, err := os.OpenFile(fileTarget, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return nil, 0, err
}

if _, err := io.Copy(f, tr); err != nil {
return nil, 0, err
}

fileInfo, err := f.Stat()
if err != nil {
return nil, 0, err
}

decompressedSize += fileInfo.Size()
createdFiles = append(createdFiles, header.Name)
f.Close()
Expand Down Expand Up @@ -195,6 +212,11 @@ func addToTarArchive(tw *tar.Writer, file *os.File) error {
})
}

func fileExists(filename string) bool {
_, err := os.Stat(filename)
return !os.IsNotExist(err)
}

// optimistically remove files created by portal with the specified prefix
func RemoveTemporaryFiles(prefix string) {
tempFiles, err := os.ReadDir(os.TempDir())
Expand Down
31 changes: 26 additions & 5 deletions ui/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ const (
)

type KeyMap struct {
Quit key.Binding
CopyPassword key.Binding
FileListUp key.Binding
FileListDown key.Binding
Quit key.Binding
CopyPassword key.Binding
FileListUp key.Binding
FileListDown key.Binding
OverwritePromptYes key.Binding
OverwritePromptNo key.Binding
}

func (k KeyMap) ShortHelp() []key.Binding {
Expand All @@ -38,12 +40,21 @@ func (k KeyMap) ShortHelp() []key.Binding {
k.CopyPassword,
k.FileListUp,
k.FileListDown,
k.OverwritePromptYes,
k.OverwritePromptNo,
}
}

func (k KeyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Quit, k.CopyPassword, k.FileListUp, k.FileListDown},
{
k.Quit,
k.CopyPassword,
k.FileListUp,
k.FileListDown,
k.OverwritePromptYes,
k.OverwritePromptNo,
},
}
}

Expand Down Expand Up @@ -73,6 +84,16 @@ var Keys = KeyMap{
key.WithHelp("(↓/j)", "file summary down"),
key.WithDisabled(),
),
OverwritePromptYes: key.NewBinding(
key.WithKeys("y", "Y"),
key.WithHelp("(Y/y)", "confirm overwrite"),
key.WithDisabled(),
),
OverwritePromptNo: key.NewBinding(
key.WithKeys("n", "N"),
key.WithHelp("(N/n)", "deny overwrite"),
key.WithDisabled(),
),
}

var PadText = strings.Repeat(" ", MARGIN)
Expand Down
3 changes: 3 additions & 0 deletions ui/filetable/filetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m Model) View() string {
if len(m.rows) == 0 {
return ""
}
return fileTableStyle.Render(m.table.View()) + "\n\n"
}
Loading

0 comments on commit 190b704

Please sign in to comment.