feat(stores): adds mvs, and flags to bring store commands on par with key commands
This commit is contained in:
parent
b89db8dc48
commit
4e5064d07a
25 changed files with 247 additions and 9 deletions
|
|
@ -44,8 +44,9 @@ type KeyConfig struct {
|
|||
}
|
||||
|
||||
type StoreConfig struct {
|
||||
DefaultStoreName string `toml:"default_store_name"`
|
||||
AlwaysPromptDelete bool `toml:"always_prompt_delete"`
|
||||
DefaultStoreName string `toml:"default_store_name"`
|
||||
AlwaysPromptDelete bool `toml:"always_prompt_delete"`
|
||||
AlwaysPromptOverwrite bool `toml:"always_prompt_overwrite"`
|
||||
}
|
||||
|
||||
type GitConfig struct {
|
||||
|
|
@ -80,8 +81,9 @@ func defaultConfig() Config {
|
|||
AlwaysPromptOverwrite: false,
|
||||
},
|
||||
Store: StoreConfig{
|
||||
DefaultStoreName: "default",
|
||||
AlwaysPromptDelete: true,
|
||||
DefaultStoreName: "default",
|
||||
AlwaysPromptDelete: true,
|
||||
AlwaysPromptOverwrite: true,
|
||||
},
|
||||
Git: GitConfig{
|
||||
AutoFetch: false,
|
||||
|
|
|
|||
|
|
@ -60,8 +60,12 @@ func delStore(cmd *cobra.Command, args []string) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("cannot delete store '%s': %v", dbName, err)
|
||||
}
|
||||
yes, err := cmd.Flags().GetBool("yes")
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot delete store '%s': %v", dbName, err)
|
||||
}
|
||||
|
||||
if interactive || config.Store.AlwaysPromptDelete {
|
||||
if !yes && (interactive || config.Store.AlwaysPromptDelete) {
|
||||
promptf("delete store '%s'? (y/n)", args[0])
|
||||
|
||||
var confirm string
|
||||
|
|
@ -87,5 +91,6 @@ func executeDeletion(path string) error {
|
|||
|
||||
func init() {
|
||||
delStoreCmd.Flags().BoolP("interactive", "i", false, "Prompt yes/no for each deletion")
|
||||
delStoreCmd.Flags().BoolP("yes", "y", false, "Skip all confirmation prompts")
|
||||
rootCmd.AddCommand(delStoreCmd)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ func stdoutIsTerminal() bool {
|
|||
// FAIL red (stderr)
|
||||
// hint dim (stderr)
|
||||
// WARN yellow (stderr)
|
||||
// info blue (stderr)
|
||||
// ok green (stderr)
|
||||
// ? cyan (stdout)
|
||||
// > dim (stdout)
|
||||
|
|
@ -60,6 +61,11 @@ func warnf(format string, args ...any) {
|
|||
fmt.Fprintf(os.Stderr, "%s %s\n", keyword("33", "WARN", stderrIsTerminal()), msg)
|
||||
}
|
||||
|
||||
func infof(format string, args ...any) {
|
||||
msg := fmt.Sprintf(format, args...)
|
||||
fmt.Fprintf(os.Stderr, "%s %s\n", keyword("34", "info", stderrIsTerminal()), msg)
|
||||
}
|
||||
|
||||
func okf(format string, args ...any) {
|
||||
msg := fmt.Sprintf(format, args...)
|
||||
fmt.Fprintf(os.Stderr, "%s %s\n", keyword("32", "ok", stderrIsTerminal()), msg)
|
||||
|
|
|
|||
129
cmd/mv-db.go
Normal file
129
cmd/mv-db.go
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
/*
|
||||
Copyright © 2025 Lewis Wynne <lew@ily.rs>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// mvStoreCmd represents the move-store command
|
||||
var mvStoreCmd = &cobra.Command{
|
||||
Use: "move-store FROM TO",
|
||||
Short: "Rename a store",
|
||||
Aliases: []string{"mvs"},
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: mvStore,
|
||||
SilenceUsage: true,
|
||||
}
|
||||
|
||||
func mvStore(cmd *cobra.Command, args []string) error {
|
||||
store := &Store{}
|
||||
|
||||
fromName, err := store.parseDB(args[0], false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot rename store '%s': %v", args[0], err)
|
||||
}
|
||||
toName, err := store.parseDB(args[1], false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot rename store '%s': %v", args[1], err)
|
||||
}
|
||||
|
||||
if fromName == toName {
|
||||
return fmt.Errorf("cannot rename store '%s': source and destination are the same", fromName)
|
||||
}
|
||||
|
||||
var notFound errNotFound
|
||||
fromPath, err := store.FindStore(fromName)
|
||||
if errors.As(err, ¬Found) {
|
||||
return fmt.Errorf("cannot rename store '%s': %w", fromName, err)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot rename store '%s': %v", fromName, err)
|
||||
}
|
||||
|
||||
interactive, err := cmd.Flags().GetBool("interactive")
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot rename store '%s': %v", fromName, err)
|
||||
}
|
||||
safe, err := cmd.Flags().GetBool("safe")
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot rename store '%s': %v", fromName, err)
|
||||
}
|
||||
yes, err := cmd.Flags().GetBool("yes")
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot rename store '%s': %v", fromName, err)
|
||||
}
|
||||
promptOverwrite := !yes && (interactive || config.Store.AlwaysPromptOverwrite)
|
||||
|
||||
toPath, err := store.storePath(toName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot rename store '%s': %v", fromName, err)
|
||||
}
|
||||
if _, err := os.Stat(toPath); err == nil {
|
||||
if safe {
|
||||
infof("skipped '@%s': already exists", toName)
|
||||
return nil
|
||||
}
|
||||
if promptOverwrite {
|
||||
promptf("overwrite store '%s'? (y/n)", toName)
|
||||
var confirm string
|
||||
if err := scanln(&confirm); err != nil {
|
||||
return fmt.Errorf("cannot rename store '%s': %v", fromName, err)
|
||||
}
|
||||
if strings.ToLower(confirm) != "y" {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
copy, _ := cmd.Flags().GetBool("copy")
|
||||
if copy {
|
||||
data, err := os.ReadFile(fromPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot copy store '%s': %v", fromName, err)
|
||||
}
|
||||
if err := os.WriteFile(toPath, data, 0o640); err != nil {
|
||||
return fmt.Errorf("cannot copy store '%s': %v", fromName, err)
|
||||
}
|
||||
okf("copied @%s to @%s", fromName, toName)
|
||||
} else {
|
||||
if err := os.Rename(fromPath, toPath); err != nil {
|
||||
return fmt.Errorf("cannot rename store '%s': %v", fromName, err)
|
||||
}
|
||||
okf("renamed @%s to @%s", fromName, toName)
|
||||
}
|
||||
return autoSync()
|
||||
}
|
||||
|
||||
func init() {
|
||||
mvStoreCmd.Flags().Bool("copy", false, "Copy instead of move (keeps source)")
|
||||
mvStoreCmd.Flags().BoolP("interactive", "i", false, "Prompt before overwriting destination")
|
||||
mvStoreCmd.Flags().BoolP("yes", "y", false, "Skip all confirmation prompts")
|
||||
mvStoreCmd.Flags().Bool("safe", false, "Do not overwrite if the destination store already exists")
|
||||
rootCmd.AddCommand(mvStoreCmd)
|
||||
}
|
||||
24
cmd/mv.go
24
cmd/mv.go
|
|
@ -64,7 +64,15 @@ func mvImpl(cmd *cobra.Command, args []string, keepSource bool) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
promptOverwrite := interactive || config.Key.AlwaysPromptOverwrite
|
||||
safe, err := cmd.Flags().GetBool("safe")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
yes, err := cmd.Flags().GetBool("yes")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
promptOverwrite := !yes && (interactive || config.Key.AlwaysPromptOverwrite)
|
||||
|
||||
identity, _ := loadIdentity()
|
||||
var recipient *age.X25519Recipient
|
||||
|
|
@ -114,6 +122,11 @@ func mvImpl(cmd *cobra.Command, args []string, keepSource bool) error {
|
|||
|
||||
dstIdx := findEntry(dstEntries, toSpec.Key)
|
||||
|
||||
if safe && dstIdx >= 0 {
|
||||
infof("skipped '%s': already exists", toSpec.Display())
|
||||
return nil
|
||||
}
|
||||
|
||||
if promptOverwrite && dstIdx >= 0 {
|
||||
var confirm string
|
||||
promptf("overwrite '%s'? (y/n)", toSpec.Display())
|
||||
|
|
@ -169,13 +182,22 @@ func mvImpl(cmd *cobra.Command, args []string, keepSource bool) error {
|
|||
}
|
||||
}
|
||||
|
||||
if keepSource {
|
||||
okf("copied %s to %s", fromSpec.Display(), toSpec.Display())
|
||||
} else {
|
||||
okf("renamed %s to %s", fromSpec.Display(), toSpec.Display())
|
||||
}
|
||||
return autoSync()
|
||||
}
|
||||
|
||||
func init() {
|
||||
mvCmd.Flags().Bool("copy", false, "Copy instead of move (keeps source)")
|
||||
mvCmd.Flags().BoolP("interactive", "i", false, "Prompt before overwriting destination")
|
||||
mvCmd.Flags().BoolP("yes", "y", false, "Skip all confirmation prompts")
|
||||
mvCmd.Flags().Bool("safe", false, "Do not overwrite if the destination already exists")
|
||||
rootCmd.AddCommand(mvCmd)
|
||||
cpCmd.Flags().BoolP("interactive", "i", false, "Prompt before overwriting destination")
|
||||
cpCmd.Flags().BoolP("yes", "y", false, "Skip all confirmation prompts")
|
||||
cpCmd.Flags().Bool("safe", false, "Do not overwrite if the destination already exists")
|
||||
rootCmd.AddCommand(cpCmd)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,6 +65,7 @@ func init() {
|
|||
|
||||
listStoresCmd.GroupID = "stores"
|
||||
delStoreCmd.GroupID = "stores"
|
||||
mvStoreCmd.GroupID = "stores"
|
||||
exportCmd.GroupID = "stores"
|
||||
restoreCmd.GroupID = "stores"
|
||||
|
||||
|
|
|
|||
|
|
@ -135,6 +135,7 @@ func set(cmd *cobra.Command, args []string) error {
|
|||
idx := findEntry(entries, spec.Key)
|
||||
|
||||
if safe && idx >= 0 {
|
||||
infof("skipped '%s': already exists", spec.Display())
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue