diff options
author | Igor <idrozdov@gitlab.com> | 2019-10-21 16:25:53 +0000 |
---|---|---|
committer | Igor <idrozdov@gitlab.com> | 2019-10-21 16:25:53 +0000 |
commit | 629e3bf9c31687f7b824cf29ba07ad2ce402e280 (patch) | |
tree | 0f80f7394231d39970f23a08ba9ba2ce7051e22c /internal/command/commandargs/authorized_principals.go | |
parent | 7d5229db263a62661653431881bef8b46984d0de (diff) | |
parent | ede41ee451dd0aa6d0ecd958c7fadbdb3b63f3e4 (diff) | |
download | gitlab-shell-629e3bf9c31687f7b824cf29ba07ad2ce402e280.tar.gz |
Merge branch '173-move-go-code-up-one-level' into 'master'
Move Go code up one level
See merge request gitlab-org/gitlab-shell!350
Diffstat (limited to 'internal/command/commandargs/authorized_principals.go')
-rw-r--r-- | internal/command/commandargs/authorized_principals.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/internal/command/commandargs/authorized_principals.go b/internal/command/commandargs/authorized_principals.go new file mode 100644 index 0000000..746ae3f --- /dev/null +++ b/internal/command/commandargs/authorized_principals.go @@ -0,0 +1,50 @@ +package commandargs + +import ( + "errors" + "fmt" +) + +type AuthorizedPrincipals struct { + Arguments []string + KeyId string + Principals []string +} + +func (ap *AuthorizedPrincipals) Parse() error { + if err := ap.validate(); err != nil { + return err + } + + ap.KeyId = ap.Arguments[0] + ap.Principals = ap.Arguments[1:] + + return nil +} + +func (ap *AuthorizedPrincipals) GetArguments() []string { + return ap.Arguments +} + +func (ap *AuthorizedPrincipals) validate() error { + argsSize := len(ap.Arguments) + + if argsSize < 2 { + return errors.New(fmt.Sprintf("# Insufficient arguments. %d. Usage\n#\tgitlab-shell-authorized-principals-check <key-id> <principal1> [<principal2>...]", argsSize)) + } + + keyId := ap.Arguments[0] + principals := ap.Arguments[1:] + + if keyId == "" { + return errors.New("# No key_id provided") + } + + for _, principal := range principals { + if principal == "" { + return errors.New("# An invalid principal was provided") + } + } + + return nil +} |