summaryrefslogtreecommitdiff
path: root/go/internal/command/discover/discover.go
blob: ab04cbd403aaa566f9a0a9eb7b0cc0143aa0cea7 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
package discover

import (
	"fmt"
	"io"
	"os"

	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/commandargs"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/config"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/gitlabnet/discover"
)

type Command struct {
	Config *config.Config
	Args   *commandargs.CommandArgs
}

var (
	output io.Writer = os.Stdout
)

func (c *Command) Execute() error {
	response, err := c.getUserInfo()
	if err != nil {
		return fmt.Errorf("Failed to get username: %v", err)
	}

	if response.IsAnonymous() {
		fmt.Fprintf(output, "Welcome to GitLab, Anonymous!\n")
	} else {
		fmt.Fprintf(output, "Welcome to GitLab, @%s!\n", response.Username)
	}

	return nil
}

func (c *Command) getUserInfo() (*discover.Response, error) {
	client, err := discover.NewClient(c.Config)
	if err != nil {
		return nil, err
	}

	if c.Args.GitlabKeyId != "" {
		return client.GetByKeyId(c.Args.GitlabKeyId)
	} else if c.Args.GitlabUsername != "" {
		return client.GetByUsername(c.Args.GitlabUsername)
	} else {
		// There was no 'who' information, this  matches the ruby error
		// message.
		return nil, fmt.Errorf("who='' is invalid")
	}
}