github.com/chenbh/concourse/v6@v6.4.2/fly/commands/users.go (about) 1 package commands 2 3 import ( 4 "errors" 5 "os" 6 "time" 7 8 "github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers" 9 "github.com/chenbh/concourse/v6/fly/rc" 10 "github.com/chenbh/concourse/v6/fly/ui" 11 "github.com/fatih/color" 12 ) 13 14 const inputDateLayout = "2006-01-02" 15 16 type ActiveUsersCommand struct { 17 Since string `long:"since" description:"Start date range of returned users' last login, defaults to 2 months from today'"` 18 Json bool `long:"json" description:"Print command result as JSON"` 19 } 20 21 func (command *ActiveUsersCommand) Execute([]string) error { 22 target, err := rc.LoadTarget(Fly.Target, Fly.Verbose) 23 if err != nil { 24 return err 25 } 26 27 err = target.Validate() 28 if err != nil { 29 return err 30 } 31 32 dateSince := time.Now().AddDate(0, -2, 0) 33 34 if command.Since != "" { 35 dateSince, err = time.ParseInLocation(inputDateLayout, command.Since, time.Now().Location()) 36 if err != nil { 37 return errors.New("since time should be in the format: yyyy-mm-dd") 38 } 39 } 40 41 if dateSince.After(time.Now()) { 42 return errors.New("since time can't be in the future") 43 } 44 45 users, err := target.Client().ListActiveUsersSince(dateSince) 46 if err != nil { 47 return err 48 } 49 50 if command.Json { 51 err = displayhelpers.JsonPrint(users) 52 if err != nil { 53 return err 54 } 55 return nil 56 } 57 58 headers := ui.TableRow{ 59 {Contents: "username", Color: color.New(color.Bold)}, 60 {Contents: "connector", Color: color.New(color.Bold)}, 61 {Contents: "last login", Color: color.New(color.Bold)}, 62 } 63 64 table := ui.Table{Headers: headers} 65 66 for _, user := range users { 67 row := ui.TableRow{ 68 {Contents: user.Username}, 69 {Contents: user.Connector}, 70 {Contents: time.Unix(user.LastLogin, 0).Format(inputDateLayout)}, 71 } 72 table.Data = append(table.Data, row) 73 } 74 return table.Render(os.Stdout, Fly.PrintTableHeaders) 75 }