-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_option.go
82 lines (69 loc) · 2.25 KB
/
docker_option.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2021 Ke Fan <litesky@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package options
import (
"encoding/json"
"fmt"
"github.com/spf13/pflag"
)
// DockerOption defines options for docker cluster.
type DockerOption struct {
Endpoint string `json:"endpoint" mapstructure:"endpoint"`
Registry Registry `json:"registry" mapstructure:"registry"`
}
// Registry defines options for docker registry.
type Registry struct {
Username string `json:"username" mapstructure:"username"`
Password string `json:"password" mapstructure:"password"`
Email string `json:"email" mapstructure:"email"`
Serveraddress string `json:"server-address" mapstructure:"server-address"`
Project string `json:"project" mapstructure:"project"`
}
// NewDockerOption create a `zero` value instance.
func NewDockerOption() *DockerOption {
return &DockerOption{
Endpoint: "",
Registry: Registry{},
}
}
// Validate validate option value.
func (o *DockerOption) Validate() []error {
errs := []error{}
if o.Endpoint == "" {
errs = append(errs, fmt.Errorf("docker endpoint can not be empty"))
}
if o.Registry.Project == "" {
errs = append(errs, fmt.Errorf("registry project cannot be empty"))
}
return errs
}
// AddFlags bind command flag.
func (o *DockerOption) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&(o.Endpoint), "docker.endpoint", o.Endpoint, "The endpoint for accessing docker server.")
fs.StringVar(
&(o.Registry.Serveraddress),
"docker.registry.server-address",
o.Registry.Serveraddress,
"docker registry server address",
)
fs.StringVar(&(o.Registry.Project), "docker.registry.project", o.Registry.Project, "docker registry project name")
fs.StringVar(&(o.Registry.Email), "docker.registry.email", o.Registry.Email, "docker registry auth email")
fs.StringVar(
&(o.Registry.Username),
"docker.registry.username",
o.Registry.Username,
"docker registry auth username",
)
fs.StringVar(
&(o.Registry.Password),
"docker.registry.password",
o.Registry.Password,
"docker registry auth password",
)
}
// String to json string.
func (o *DockerOption) String() string {
data, _ := json.Marshal(o)
return string(data)
}