-
Notifications
You must be signed in to change notification settings - Fork 41
/
flags.go
63 lines (50 loc) · 1.75 KB
/
flags.go
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
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2023 Upbound Inc.
// All rights reserved
package space
import (
"io"
"net/url"
"os"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/upbound/up/internal/input"
)
type registryFlags struct {
Repository *url.URL `hidden:"" name:"registry-repository" env:"UPBOUND_REGISTRY" default:"us-west1-docker.pkg.dev/orchestration-build/upbound-environments" help:"Set registry for where to pull OCI artifacts from. This is an OCI registry reference, i.e. a URL without the scheme or protocol prefix."`
Endpoint *url.URL `hidden:"" name:"registry-endpoint" env:"UPBOUND_REGISTRY_ENDPOINT" default:"https://us-west1-docker.pkg.dev" help:"Set registry endpoint, including scheme, for authentication."`
}
type authorizedRegistryFlags struct {
registryFlags
TokenFile *os.File `name:"token-file" help:"File containing authentication token."`
Username string `hidden:"" name:"registry-username" help:"Set the registry username."`
Password string `hidden:"" name:"registry-password" help:"Set the registry password."`
}
func (p *authorizedRegistryFlags) AfterApply() error {
if p.TokenFile == nil && p.Username == "" && p.Password == "" {
if p.Repository.String() == defaultRegistry {
return errors.New("--token-file is required")
}
prompter := input.NewPrompter()
id, err := prompter.Prompt("Username", false)
if err != nil {
return err
}
token, err := prompter.Prompt("Password", true)
if err != nil {
return err
}
p.Username = id
p.Password = token
return nil
}
if p.Username != "" {
return nil
}
b, err := io.ReadAll(p.TokenFile)
defer p.TokenFile.Close() // nolint:errcheck
if err != nil {
return errors.Wrap(err, errReadTokenFile)
}
p.Username = jsonKey
p.Password = string(b)
return nil
}