func (cmd CmdKey) listKeys(s *repository.Repository) error { tab := NewTable() tab.Header = fmt.Sprintf(" %-10s %-10s %-10s %s", "ID", "User", "Host", "Created") tab.RowFormat = "%s%-10s %-10s %-10s %s" plen, err := s.PrefixLength(backend.Key) if err != nil { return err } done := make(chan struct{}) defer close(done) for id := range s.List(backend.Key, done) { k, err := repository.LoadKey(s, id.String()) if err != nil { cmd.global.Warnf("LoadKey() failed: %v\n", err) continue } var current string if id.String() == s.KeyName() { current = "*" } else { current = " " } tab.Rows = append(tab.Rows, []interface{}{current, id.String()[:plen], k.Username, k.Hostname, k.Created.Format(TimeFormat)}) } return tab.Write(cmd.global.stdout) }
func (cmd CmdKey) deleteKey(repo *repository.Repository, name string) error { if name == repo.KeyName() { return errors.New("refusing to remove key currently used to access repository") } err := repo.Backend().Remove(backend.Key, name) if err != nil { return err } cmd.global.Verbosef("removed key %v\n", name) return nil }
func (cmd CmdKey) changePassword(repo *repository.Repository) error { id, err := repository.AddKey(repo, cmd.getNewPassword(), repo.Key()) if err != nil { return fmt.Errorf("creating new key failed: %v\n", err) } err = repo.Backend().Remove(backend.Key, repo.KeyName()) if err != nil { return err } cmd.global.Verbosef("saved new key as %s\n", id) return nil }