github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/cmd/users.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/drycc/controller-sdk-go/users"
     7  	"github.com/drycc/workflow-cli/settings"
     8  )
     9  
    10  // UsersList lists users registered with the controller.
    11  func (d *DryccCmd) UsersList(results int) error {
    12  	s, err := settings.Load(d.ConfigFile)
    13  
    14  	if err != nil {
    15  		return err
    16  	}
    17  
    18  	if results == defaultLimit {
    19  		results = s.Limit
    20  	}
    21  
    22  	users, _, err := users.List(s.Client, results)
    23  	if d.checkAPICompatibility(s.Client, err) != nil {
    24  		return err
    25  	}
    26  	table := d.getDefaultFormatTable([]string{"USERNAME", "EMAIL", "ADMIN", "STAFF", "ACTIVE", "DATE-JOIN"})
    27  	for _, user := range users {
    28  		table.Append([]string{
    29  			user.Username,
    30  			user.Email,
    31  			fmt.Sprintf("%v", user.IsSuperuser),
    32  			fmt.Sprintf("%v", user.IsStaff),
    33  			fmt.Sprintf("%v", user.IsActive),
    34  			user.DateJoined,
    35  		})
    36  	}
    37  	table.Render()
    38  	return nil
    39  }
    40  
    41  // UsersEnable enable user with the controller.
    42  func (d *DryccCmd) UsersEnable(username string) error {
    43  	s, err := settings.Load(d.ConfigFile)
    44  
    45  	if err != nil {
    46  		return err
    47  	}
    48  	d.Printf("Enabling user %s... ", username)
    49  	err = users.Enable(s.Client, username)
    50  	if d.checkAPICompatibility(s.Client, err) != nil {
    51  		return err
    52  	}
    53  
    54  	d.Println("done")
    55  	d.Println("This modification is only temporary and will be reverted when the user login again.")
    56  	return nil
    57  }
    58  
    59  // UsersDisable disable user with the controller.
    60  func (d *DryccCmd) UsersDisable(username string) error {
    61  	s, err := settings.Load(d.ConfigFile)
    62  
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	d.Printf("Disabling user %s... ", username)
    68  	err = users.Disable(s.Client, username)
    69  	if d.checkAPICompatibility(s.Client, err) != nil {
    70  		return err
    71  	}
    72  
    73  	d.Println("done")
    74  	d.Println("This modification is only temporary and will be reverted when the user login again.")
    75  	return nil
    76  }