M9: init --merge to add new devices without overwriting
Add --merge flag to init that loads existing config, skips devices whose UUID is already configured, and appends only new discoveries. --force and --merge are mutually exclusive. Uses Config.Save() from M8. Error message now suggests both --force and --merge. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
48
cmd/init.go
48
cmd/init.go
@@ -3,16 +3,17 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"git.wntrmute.dev/kyle/arca/internal/config"
|
"git.wntrmute.dev/kyle/arca/internal/config"
|
||||||
"git.wntrmute.dev/kyle/arca/internal/udisks"
|
"git.wntrmute.dev/kyle/arca/internal/udisks"
|
||||||
"github.com/godbus/dbus/v5"
|
"github.com/godbus/dbus/v5"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var forceInit bool
|
var (
|
||||||
|
forceInit bool
|
||||||
|
mergeInit bool
|
||||||
|
)
|
||||||
|
|
||||||
var initCmd = &cobra.Command{
|
var initCmd = &cobra.Command{
|
||||||
Use: "init",
|
Use: "init",
|
||||||
@@ -23,17 +24,26 @@ var initCmd = &cobra.Command{
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
initCmd.Flags().BoolVarP(&forceInit, "force", "f", false, "overwrite existing config file")
|
initCmd.Flags().BoolVarP(&forceInit, "force", "f", false, "overwrite existing config file")
|
||||||
|
initCmd.Flags().BoolVar(&mergeInit, "merge", false, "add new devices to existing config without overwriting")
|
||||||
|
initCmd.MarkFlagsMutuallyExclusive("force", "merge")
|
||||||
rootCmd.AddCommand(initCmd)
|
rootCmd.AddCommand(initCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runInit(cmd *cobra.Command, args []string) error {
|
func runInit(cmd *cobra.Command, args []string) error {
|
||||||
cfgPath := config.Path()
|
cfgPath := config.Path()
|
||||||
|
|
||||||
|
// Load existing config for merge, or start fresh.
|
||||||
|
var cfg *config.Config
|
||||||
|
if mergeInit {
|
||||||
|
cfg = config.Load()
|
||||||
|
} else {
|
||||||
if !forceInit {
|
if !forceInit {
|
||||||
if _, err := os.Stat(cfgPath); err == nil {
|
if _, err := os.Stat(cfgPath); err == nil {
|
||||||
return fmt.Errorf("config already exists at %s (use --force to overwrite)", cfgPath)
|
return fmt.Errorf("config already exists at %s (use --force to overwrite or --merge to add new devices)", cfgPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
cfg = &config.Config{Devices: make(map[string]config.DeviceConfig)}
|
||||||
|
}
|
||||||
|
|
||||||
client, err := udisks.NewClient()
|
client, err := udisks.NewClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -51,43 +61,42 @@ func runInit(cmd *cobra.Command, args []string) error {
|
|||||||
return fmt.Errorf("detecting root device: %w", err)
|
return fmt.Errorf("detecting root device: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := config.Config{
|
added := 0
|
||||||
Devices: make(map[string]config.DeviceConfig),
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, dev := range encrypted {
|
for _, dev := range encrypted {
|
||||||
if isRootBacking(dev.ObjectPath, rootBacking) {
|
if isRootBacking(dev.ObjectPath, rootBacking) {
|
||||||
fmt.Fprintf(os.Stderr, "Skipping %s (root filesystem)\n", dev.DevicePath)
|
fmt.Fprintf(os.Stderr, "Skipping %s (root filesystem)\n", dev.DevicePath)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cfg.HasUUID(dev.UUID) {
|
||||||
|
fmt.Fprintf(os.Stderr, "Skipping %s (already configured)\n", dev.DevicePath)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
alias := aliasFromUUID(dev.UUID)
|
alias := aliasFromUUID(dev.UUID)
|
||||||
cfg.Devices[alias] = config.DeviceConfig{
|
cfg.Devices[alias] = config.DeviceConfig{
|
||||||
UUID: dev.UUID,
|
UUID: dev.UUID,
|
||||||
Methods: []string{"passphrase"},
|
Methods: []string{"passphrase"},
|
||||||
}
|
}
|
||||||
fmt.Fprintf(os.Stderr, "Found %s (UUID %s) -> alias %q\n", dev.DevicePath, dev.UUID, alias)
|
fmt.Fprintf(os.Stderr, "Found %s (UUID %s) -> alias %q\n", dev.DevicePath, dev.UUID, alias)
|
||||||
|
added++
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cfg.Devices) == 0 {
|
if added == 0 && !mergeInit {
|
||||||
fmt.Println("No non-root LUKS devices found.")
|
fmt.Println("No non-root LUKS devices found.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := yaml.Marshal(&cfg)
|
if added == 0 && mergeInit {
|
||||||
if err != nil {
|
fmt.Println("No new devices to add.")
|
||||||
return fmt.Errorf("marshaling config: %w", err)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.MkdirAll(filepath.Dir(cfgPath), 0o755); err != nil {
|
if err := cfg.Save(); err != nil {
|
||||||
return fmt.Errorf("creating config directory: %w", err)
|
return fmt.Errorf("saving config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.WriteFile(cfgPath, data, 0o644); err != nil {
|
fmt.Printf("Config written to %s (%d device(s) added)\n", cfgPath, added)
|
||||||
return fmt.Errorf("writing config: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("Config written to %s\n", cfgPath)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,4 +108,3 @@ func isRootBacking(path dbus.ObjectPath, rootDevices []dbus.ObjectPath) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user