summaryrefslogtreecommitdiff
path: root/go/internal/command/command.go
blob: b3bdcbae30fb9382a43434946e6ae4790cf91a84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package command

import (
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/commandargs"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/discover"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/fallback"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/readwriter"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/twofactorrecover"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/config"
)

type Command interface {
	Execute(*readwriter.ReadWriter) error
}

func New(arguments []string, config *config.Config) (Command, error) {
	args, err := commandargs.Parse(arguments)

	if err != nil {
		return nil, err
	}

	if config.FeatureEnabled(string(args.CommandType)) {
		return buildCommand(args, config), nil
	}

	return &fallback.Command{}, nil
}

func buildCommand(args *commandargs.CommandArgs, config *config.Config) Command {
	switch args.CommandType {
	case commandargs.Discover:
		return &discover.Command{Config: config, Args: args}
	case commandargs.TwoFactorRecover:
		return &twofactorrecover.Command{Config: config, Args: args}
	}

	return nil
}