2019-02-24 16:17:52 +03:00
package commands
import (
2020-06-21 08:51:48 +03:00
"encoding/json"
2020-06-24 05:22:32 +03:00
"errors"
2019-02-24 16:17:52 +03:00
"fmt"
2020-06-24 05:22:32 +03:00
"reflect"
"github.com/spf13/cobra"
2019-02-24 16:17:52 +03:00
"github.com/MichaelMure/git-bug/cache"
2020-06-24 05:22:32 +03:00
identity2 "github.com/MichaelMure/git-bug/identity"
2019-02-24 16:17:52 +03:00
"github.com/MichaelMure/git-bug/util/colors"
"github.com/MichaelMure/git-bug/util/interrupt"
)
2020-06-21 08:51:48 +03:00
var (
userLsOutputFormat string
)
func runUserLs ( _ * cobra . Command , _ [ ] string ) error {
2019-02-24 16:17:52 +03:00
backend , err := cache . NewRepoCache ( repo )
if err != nil {
return err
}
defer backend . Close ( )
interrupt . RegisterCleaner ( backend . Close )
2020-06-24 05:22:32 +03:00
ids := backend . AllIdentityIds ( )
var users [ ] * cache . IdentityExcerpt
for _ , id := range ids {
user , err := backend . ResolveIdentityExcerpt ( id )
if err != nil {
return err
}
users = append ( users , user )
}
2020-06-21 08:51:48 +03:00
switch userLsOutputFormat {
2020-06-23 12:51:42 +03:00
case "org-mode" :
2020-06-24 05:22:32 +03:00
return userLsOrgmodeFormatter ( users )
2020-06-21 08:51:48 +03:00
case "json" :
2020-06-24 05:22:32 +03:00
return userLsJsonFormatter ( users )
2020-06-21 08:51:48 +03:00
case "plain" :
2020-06-24 05:22:32 +03:00
return userLsPlainFormatter ( users )
2020-06-21 08:51:48 +03:00
case "default" :
2020-06-24 05:22:32 +03:00
return userLsDefaultFormatter ( users )
2020-06-21 08:51:48 +03:00
default :
return fmt . Errorf ( "unknown format %s" , userLsOutputFormat )
}
}
type JSONIdentity struct {
Id string ` json:"id" `
HumanId string ` json:"human_id" `
Name string ` json:"name" `
Login string ` json:"login" `
}
2020-06-24 05:22:32 +03:00
func NewJSONIdentity ( id interface { } ) ( JSONIdentity , error ) {
switch id . ( type ) {
case * cache . IdentityExcerpt :
i := id . ( * cache . IdentityExcerpt )
return JSONIdentity {
i . Id . String ( ) ,
i . Id . Human ( ) ,
i . Name ,
i . Login ,
} , nil
case identity2 . Interface :
i := id . ( identity2 . Interface )
return JSONIdentity {
i . Id ( ) . String ( ) ,
i . Id ( ) . Human ( ) ,
i . Name ( ) ,
i . Login ( ) ,
} , nil
case cache . LegacyAuthorExcerpt :
i := id . ( cache . LegacyAuthorExcerpt )
return JSONIdentity {
2020-06-24 09:34:36 +03:00
"" ,
"" ,
2020-06-24 05:22:32 +03:00
i . Name ,
i . Login ,
} , nil
default :
return JSONIdentity { } , errors . New ( fmt . Sprintf ( "Inconvertible type, attempting to convert type %s to type %s." , reflect . TypeOf ( id ) . String ( ) , reflect . TypeOf ( JSONIdentity { } ) . String ( ) ) )
}
}
2020-06-21 08:51:48 +03:00
2020-06-24 05:22:32 +03:00
func userLsPlainFormatter ( users [ ] * cache . IdentityExcerpt ) error {
for _ , user := range users {
2020-06-21 08:51:48 +03:00
fmt . Printf ( "%s %s\n" ,
2020-06-24 05:22:32 +03:00
user . Id . Human ( ) ,
user . DisplayName ( ) ,
2020-06-21 08:51:48 +03:00
)
}
return nil
}
2020-06-24 05:22:32 +03:00
func userLsDefaultFormatter ( users [ ] * cache . IdentityExcerpt ) error {
for _ , user := range users {
2019-02-24 16:17:52 +03:00
fmt . Printf ( "%s %s\n" ,
2020-06-24 05:22:32 +03:00
colors . Cyan ( user . Id . Human ( ) ) ,
user . DisplayName ( ) ,
2019-02-24 16:17:52 +03:00
)
}
return nil
}
2020-06-24 05:22:32 +03:00
func userLsJsonFormatter ( users [ ] * cache . IdentityExcerpt ) error {
jsonUsers := [ ] JSONIdentity { }
for _ , user := range users {
jsonUser , err := NewJSONIdentity ( user )
2020-06-21 08:51:48 +03:00
if err != nil {
return err
}
2020-06-24 05:22:32 +03:00
jsonUsers = append ( jsonUsers , jsonUser )
2020-06-21 08:51:48 +03:00
}
2020-06-24 05:22:32 +03:00
jsonObject , _ := json . MarshalIndent ( jsonUsers , "" , " " )
2020-06-21 08:51:48 +03:00
fmt . Printf ( "%s\n" , jsonObject )
return nil
}
2020-06-24 05:22:32 +03:00
func userLsOrgmodeFormatter ( users [ ] * cache . IdentityExcerpt ) error {
for _ , user := range users {
2020-06-23 12:51:42 +03:00
fmt . Printf ( "* %s %s\n" ,
2020-06-24 05:22:32 +03:00
user . Id . Human ( ) ,
user . DisplayName ( ) ,
2020-06-23 12:51:42 +03:00
)
}
return nil
}
2019-02-24 16:17:52 +03:00
var userLsCmd = & cobra . Command {
Use : "ls" ,
2019-02-24 16:46:08 +03:00
Short : "List identities." ,
2019-02-24 16:17:52 +03:00
PreRunE : loadRepo ,
RunE : runUserLs ,
}
func init ( ) {
userCmd . AddCommand ( userLsCmd )
userLsCmd . Flags ( ) . SortFlags = false
2020-06-21 08:51:48 +03:00
userLsCmd . Flags ( ) . StringVarP ( & userLsOutputFormat , "format" , "f" , "default" ,
2020-06-23 12:51:42 +03:00
"Select the output formatting style. Valid values are [default,plain,json,org-mode]" )
2019-02-24 16:17:52 +03:00
}