Skip to content

Commit

Permalink
rename read to update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Ban committed Aug 27, 2021
1 parent ca42aa7 commit a2803ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ $ sshman register user [email protected] ~/.ssh/user1.pub production-team staging-s
To run auto discovery users on registered servers, or to refresh the configuration if any 3rd party has changed `~/.ssh/authorized_keys` files, run:

```sh
$ sshman read -f
$ sshman update
```

### Adding user to server
Expand Down
47 changes: 20 additions & 27 deletions cmd/readusers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

// readCmd represents the read command
var readCmd = &cobra.Command{
Use: "read",
Short: "Read all into configuration",
Use: "update",
Short: "Read users into configuration",
Long: `Loop through all servers, download all users from autorized_keys into configuration`,
Run: func(cmd *cobra.Command, _ []string) {
readUsers(cmd, readConfig())
Expand All @@ -20,11 +20,9 @@ var readCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(readCmd)
readCmd.Flags().BoolP("force", "f", false, "Force reading configuration")
}

func readUsers(cmd *cobra.Command, C *config) {
force, _ := cmd.Flags().GetBool("force")
func readUsers(_ *cobra.Command, C *config) {
bar := progressbar.Default(int64(len(C.Hosts)))
for alias, host := range C.Hosts {
bar.Add(1)
Expand All @@ -45,30 +43,25 @@ func readUsers(cmd *cobra.Command, C *config) {
client.Close()
sum := checksum(string(b))
userlist := []string{}
if host.Checksum != sum || force {
// if host.Checksum != sum {
// slog.Infof("checksum doesn't match for %s %s", alias, sum)
// }
lines := strings.Split(string(b), "\n")
for _, line := range lines {
if len(line) == 0 {
continue
}
parts := strings.Split(line, " ")
if len(parts) != 3 {
slog.Errorf("Not good line: '%s'", line)
}
lsum := checksum(parts[1])
if _, ok := C.Users[lsum]; !ok {
C.Users[lsum] = user{
KeyType: parts[0],
Key: parts[1],
Name: parts[2],
Email: parts[2] + "@" + alias,
}
lines := strings.Split(string(b), "\n")
for _, line := range lines {
if len(line) == 0 {
continue
}
parts := strings.Split(line, " ")
if len(parts) != 3 {
slog.Errorf("Not good line: '%s'", line)
}
lsum := checksum(parts[1])
if _, ok := C.Users[lsum]; !ok {
C.Users[lsum] = user{
KeyType: parts[0],
Key: parts[1],
Name: parts[2],
Email: parts[2] + "@" + alias,
}
userlist = append(userlist, C.Users[lsum].Email)
}
userlist = append(userlist, C.Users[lsum].Email)
}
host.Checksum = sum
host.Users = userlist
Expand Down

0 comments on commit a2803ad

Please sign in to comment.