github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/cmd/perms.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 6 "github.com/drycc/controller-sdk-go/perms" 7 "github.com/drycc/workflow-cli/pkg/git" 8 "github.com/drycc/workflow-cli/settings" 9 ) 10 11 // PermsList prints which users have permissions. 12 func (d *DryccCmd) PermsList(appID string, admin bool, results int) error { 13 s, appID, err := permsLoad(d.ConfigFile, appID, admin) 14 15 if err != nil { 16 return err 17 } 18 19 var users []string 20 21 if admin { 22 if results == defaultLimit { 23 results = s.Limit 24 } 25 users, _, err = perms.ListAdmins(s.Client, results) 26 } else { 27 users, err = perms.List(s.Client, appID) 28 } 29 30 if d.checkAPICompatibility(s.Client, err) != nil { 31 return err 32 } 33 34 table := d.getDefaultFormatTable([]string{"USERNAME", "ADMIN"}) 35 for _, user := range users { 36 table.Append([]string{user, fmt.Sprintf("%v", admin)}) 37 } 38 table.Render() 39 return nil 40 } 41 42 // PermCreate adds a user to an app or makes them an administrator. 43 func (d *DryccCmd) PermCreate(appID string, username string, admin bool) error { 44 45 s, appID, err := permsLoad(d.ConfigFile, appID, admin) 46 47 if err != nil { 48 return err 49 } 50 51 if admin { 52 d.Printf("Adding %s to system administrators... ", username) 53 err = perms.NewAdmin(s.Client, username) 54 } else { 55 d.Printf("Adding %s to %s collaborators... ", username, appID) 56 err = perms.New(s.Client, appID, username) 57 } 58 59 if d.checkAPICompatibility(s.Client, err) != nil { 60 return err 61 } 62 63 d.Println("done") 64 65 return nil 66 } 67 68 // PermDelete removes a user from an app or revokes admin privileges. 69 func (d *DryccCmd) PermDelete(appID, username string, admin bool) error { 70 71 s, appID, err := permsLoad(d.ConfigFile, appID, admin) 72 73 if err != nil { 74 return err 75 } 76 77 if admin { 78 d.Printf("Removing %s from system administrators... ", username) 79 err = perms.DeleteAdmin(s.Client, username) 80 } else { 81 d.Printf("Removing %s from %s collaborators... ", username, appID) 82 err = perms.Delete(s.Client, appID, username) 83 } 84 85 if d.checkAPICompatibility(s.Client, err) != nil { 86 return err 87 } 88 89 d.Println("done") 90 91 return nil 92 } 93 94 func permsLoad(cf, appID string, admin bool) (*settings.Settings, string, error) { 95 s, err := settings.Load(cf) 96 97 if err != nil { 98 return nil, "", err 99 } 100 101 if !admin && appID == "" { 102 appID, err = git.DetectAppName(git.DefaultCmd, s.Client.ControllerURL.Host) 103 104 if err != nil { 105 return nil, "", err 106 } 107 } 108 109 return s, appID, err 110 }