@@ -10,6 +10,9 @@ import (
1010 "github.com/secrethub/secrethub-go/internals/api"
1111)
1212
13+ const accountTypeUser string = "user"
14+ const accountTypeService string = "service"
15+
1316// AccountInspectCommand is a command to inspect account details.
1417type AccountInspectCommand struct {
1518 io ui.IO
@@ -40,38 +43,81 @@ func (cmd *AccountInspectCommand) Run() error {
4043 return err
4144 }
4245
43- user , err := client .Users ().Me ()
46+ account , err := client .Accounts ().Me ()
4447 if err != nil {
4548 return err
4649 }
47-
48- output , err := cli .PrettyJSON (newOutputUser (user , cmd .timeFormatter ))
49- if err != nil {
50- return err
50+ var output string
51+ if account .AccountType == accountTypeUser {
52+ user , err := client .Users ().Me ()
53+ if err != nil {
54+ return err
55+ }
56+ output , err = cli .PrettyJSON (newOutputUser (user , cmd .timeFormatter ))
57+ if err != nil {
58+ return err
59+ }
60+ } else if account .AccountType == accountTypeService {
61+ service , err := client .Services ().Get (account .Name .String ())
62+ if err != nil {
63+ return err
64+ }
65+ output , err = cli .PrettyJSON (newOutputService (service , account , cmd .timeFormatter ))
66+ if err != nil {
67+ return err
68+ }
5169 }
52-
5370 fmt .Fprintln (cmd .io .Output (), output )
5471
5572 return nil
5673}
5774
58- // outputUser is a user friendly JSON representation of a user account.
59- type outputUser struct {
60- Username string
61- FullName string
62- Email string `json:",omitempty"`
63- EmailVerified bool `json:",omitempty"`
75+ // outputAccount contains the fields common in both outputUser and outputService
76+ type outputAccount struct {
77+ AccountType string
78+ AccountName string
6479 CreatedAt string `json:",omitempty"`
6580 PublicAccountKey []byte `json:",omitempty"`
6681}
6782
83+ // outputUser is a user friendly JSON representation of a user account.
84+ type outputUser struct {
85+ Username string
86+ FullName string
87+ Email string `json:",omitempty"`
88+ EmailVerified bool `json:",omitempty"`
89+ outputAccount
90+ }
91+
6892func newOutputUser (user * api.User , timeFormatter TimeFormatter ) * outputUser {
6993 return & outputUser {
70- Username : user .Username ,
71- FullName : user .FullName ,
72- Email : user .Email ,
73- EmailVerified : user .EmailVerified ,
74- CreatedAt : timeFormatter .Format (user .CreatedAt .Local ()),
75- PublicAccountKey : user .PublicKey ,
94+ Username : user .Username ,
95+ FullName : user .FullName ,
96+ Email : user .Email ,
97+ EmailVerified : user .EmailVerified ,
98+ outputAccount : outputAccount {
99+ AccountType : accountTypeUser ,
100+ AccountName : user .Username ,
101+ CreatedAt : timeFormatter .Format (user .CreatedAt .Local ()),
102+ PublicAccountKey : user .PublicKey ,
103+ },
104+ }
105+ }
106+
107+ // outputService is a user friendly JSON representation of a service account.
108+ type outputService struct {
109+ Description string
110+ outputAccount
111+ }
112+
113+ func newOutputService (service * api.Service , account * api.Account , timeFormatter TimeFormatter ) * outputService {
114+ return & outputService {
115+ Description : service .Description ,
116+ outputAccount : outputAccount {
117+ AccountType : accountTypeService ,
118+ AccountName : service .ServiceID ,
119+ CreatedAt : timeFormatter .Format (service .CreatedAt .Local ()),
120+ PublicAccountKey : account .PublicKey ,
121+ },
76122 }
77123}
0 commit comments