(opts Opts)
| 41 | } |
| 42 | |
| 43 | func updateFile(opts Opts) error { |
| 44 | sc, err := config.LoadStoresConfig(opts.ConfigPath) |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | store := common.DefaultStoreForPathOrFormat(sc, opts.InputPath, opts.InputType) |
| 49 | log.Printf("Syncing keys for file %s", opts.InputPath) |
| 50 | tree, err := common.LoadEncryptedFile(store, opts.InputPath) |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | conf, err := config.LoadCreationRuleForFile(opts.ConfigPath, opts.InputPath, make(map[string]*string)) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | if conf == nil { |
| 59 | return fmt.Errorf("The config file %s does not contain any creation rule", opts.ConfigPath) |
| 60 | } |
| 61 | |
| 62 | diffs := common.DiffKeyGroups(tree.Metadata.KeyGroups, conf.KeyGroups) |
| 63 | keysWillChange := false |
| 64 | for _, diff := range diffs { |
| 65 | if len(diff.Added) > 0 || len(diff.Removed) > 0 { |
| 66 | keysWillChange = true |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // TODO: use conf.ShamirThreshold instead of tree.Metadata.ShamirThreshold in the next line? |
| 71 | // Or make this configurable? |
| 72 | var shamirThreshold = tree.Metadata.ShamirThreshold |
| 73 | if opts.ShamirThreshold != 0 { |
| 74 | shamirThreshold = opts.ShamirThreshold |
| 75 | } |
| 76 | shamirThreshold = min(shamirThreshold, len(conf.KeyGroups)) |
| 77 | var shamirThresholdWillChange = tree.Metadata.ShamirThreshold != shamirThreshold |
| 78 | |
| 79 | if !keysWillChange && !shamirThresholdWillChange { |
| 80 | log.Printf("File %s already up to date", opts.InputPath) |
| 81 | return nil |
| 82 | } |
| 83 | fmt.Printf("The following changes will be made to the file's groups:\n") |
| 84 | common.PrettyPrintShamirDiff(tree.Metadata.ShamirThreshold, shamirThreshold) |
| 85 | common.PrettyPrintDiffs(diffs) |
| 86 | |
| 87 | if opts.Interactive { |
| 88 | var response string |
| 89 | for response != "y" && response != "n" { |
| 90 | fmt.Printf("Is this okay? (y/n):") |
| 91 | _, err = fmt.Scanln(&response) |
| 92 | if err != nil { |
| 93 | return err |
| 94 | } |
| 95 | } |
| 96 | if response == "n" { |
| 97 | log.Printf("File %s left unchanged", opts.InputPath) |
| 98 | return nil |
| 99 | } |
| 100 | } |
no test coverage detected