Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ Examples:
// Save the manifest
err = manifest.StoreFileManifest(vaultRoot, filepath.Base(pair.Source), fileManifest)
if err != nil {
if err.Error() == "skipped" {
errorMsg := fmt.Sprintf("✗ '%s': skipped", fileManifest.Destination+filepath.Base(pair.Source))
fmt.Println(errorMsg)
continue
}
errorMsg := fmt.Sprintf("✗ %s: manifest storage failed - %v", filepath.Base(pair.Source), err)
fmt.Println(errorMsg)
failedFiles = append(failedFiles, errorMsg)
Expand Down
4 changes: 3 additions & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ Examples:
}

// Step 1: Remove the manifest file
manifestPath := filepath.Join(vaultRoot, ".sietch", "manifests", fileBaseName+".yaml")
destination := strings.ReplaceAll(targetFile.Destination, "/", ".")
uniqueFileIdentifier := destination + fileBaseName + ".yaml"
manifestPath := filepath.Join(vaultRoot, ".sietch", "manifests", uniqueFileIdentifier)
if err := os.Remove(manifestPath); err != nil {
return fmt.Errorf("failed to remove manifest file: %v", err)
}
Expand Down
18 changes: 16 additions & 2 deletions internal/manifest/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"gopkg.in/yaml.v3"

"github.com/substantialcattle5/sietch/internal/config"
"github.com/substantialcattle5/sietch/util"
)

// WriteManifest writes the vault configuration to vault.yaml
Expand Down Expand Up @@ -54,9 +56,21 @@ func StoreFileManifest(vaultRoot string, fileName string, manifest *config.FileM
}

// Create manifest file path
manifestPath := filepath.Join(manifestsDir, fileName+".yaml")
destination := strings.ReplaceAll(manifest.Destination, "/", ".")
uniqueFileIdentifier := destination + fileName + ".yaml"
manifestPath := filepath.Join(manifestsDir, uniqueFileIdentifier)

// Check if file exists
_, err := os.Stat(manifestPath)
if err == nil {
message := fmt.Sprintf("'%s' exists. Overwrite? ", manifest.Destination+fileName)
response, err := util.ConfirmOverwrite(message, os.Stdin, os.Stdout)
if err != nil || !response {
return fmt.Errorf("skipped")
}
}

// Create the file
// Create/Overwrite the file
file, err := os.Create(manifestPath)
if err != nil {
return fmt.Errorf("failed to create manifest file: %v", err)
Expand Down
19 changes: 19 additions & 0 deletions util/ConfirmOverwrite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package util

import (
"bufio"
"fmt"
"io"
"strings"
)

func ConfirmOverwrite(prompt string, in io.Reader, out io.Writer) (bool, error) {
fmt.Fprintf(out, "%s (y/N): ", prompt)
reader := bufio.NewReader(in)
response, err := reader.ReadString('\n')
if err != nil {
return false, err
}
response = strings.TrimSpace(strings.ToLower(response))
return response == "y" || response == "yes", nil
}
59 changes: 59 additions & 0 deletions util/ConfirmOverwrite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package util

import (
"bytes"
"io"
"strings"
"testing"
)

func TestConfirmOverwrite(t *testing.T) {
tests := []struct {
name string
input string
expected bool
err error
}{
{
name: "yes input",
input: "y\n",
expected: true,
err: nil,
},
{
name: "yes uppercase",
input: "YES\n",
expected: true,
err: nil,
},
{
name: "no input",
input: "n\n",
expected: false,
err: nil,
},
{
name: "empty input",
input: "\n",
expected: false,
err: nil,
},
{
name: "input not terminating with \\n",
input: "yes",
expected: false,
err: io.EOF,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
in := strings.NewReader(tt.input)
out := &bytes.Buffer{}
ok, err := ConfirmOverwrite("testfile", in, out)
if err != tt.err || ok != tt.expected {
t.Errorf("ConfirmOverwrite('testfile', %s, out) = (%t, %s), want = (%t, nil)", tt.input, ok, err.Error(), tt.expected)
}
})
}
}
Loading