func Sync(w io.Writer) error { var m gandalfManager users, err := auth.ListUsers() if err != nil { return err } for _, user := range users { fmt.Fprintf(w, "Syncing user %q... ", user.Email) err = m.CreateUser(user.Email) switch err { case repository.ErrUserAlreadyExists: fmt.Fprintln(w, "already present in Gandalf") case nil: fmt.Fprintln(w, "OK") default: return err } } apps, err := app.List(nil, nil) if err != nil { return err } for _, app := range apps { var users []string for _, team := range app.GetTeams() { users = append(users, team.Users...) } fmt.Fprintf(w, "Syncing app %q... ", app.Name) err = m.CreateRepository(app.Name, users) switch err { case repository.ErrRepositoryAlreadExists: fmt.Fprintln(w, "already present in Gandalf") case nil: fmt.Fprintln(w, "OK") default: return err } for _, user := range users { m.GrantAccess(app.Name, user) } } return nil }
func Sync(w io.Writer) error { var m gandalfManager users, err := auth.ListUsers() if err != nil { return err } for _, user := range users { fmt.Fprintf(w, "Syncing user %q... ", user.Email) err = m.CreateUser(user.Email) switch err { case repository.ErrUserAlreadyExists: fmt.Fprintln(w, "already present in Gandalf") case nil: fmt.Fprintln(w, "OK") default: return err } } apps, err := app.List(nil) if err != nil { return err } for _, app := range apps { allowedPerms := []permission.Permission{ { Scheme: permission.PermAppDeploy, Context: permission.Context(permission.CtxGlobal, ""), }, { Scheme: permission.PermAppDeploy, Context: permission.Context(permission.CtxPool, app.Pool), }, } for _, t := range app.GetTeams() { allowedPerms = append(allowedPerms, permission.Permission{ Scheme: permission.PermAppDeploy, Context: permission.Context(permission.CtxTeam, t.Name), }) } users, err := auth.ListUsersWithPermissions(allowedPerms...) if err != nil { return err } userNames := make([]string, len(users)) for i := range users { userNames[i] = users[i].Email } fmt.Fprintf(w, "Syncing app %q... ", app.Name) err = m.CreateRepository(app.Name, userNames) switch err { case repository.ErrRepositoryAlreadExists: fmt.Fprintln(w, "already present in Gandalf") case nil: fmt.Fprintln(w, "OK") default: return err } for _, user := range userNames { m.GrantAccess(app.Name, user) } } return nil }