-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathview.go
108 lines (92 loc) · 2.95 KB
/
view.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// SPDX-License-Identifier: Apache-2.0
package worker
import (
"fmt"
"github.com/go-vela/cli/action"
"github.com/go-vela/cli/action/worker"
"github.com/go-vela/cli/internal"
"github.com/go-vela/cli/internal/client"
"github.com/urfave/cli/v2"
)
// CommandView defines the command for inspecting a worker.
var CommandView = &cli.Command{
Name: "worker",
Description: "Use this command to view a worker.",
Usage: "View details of the provided worker",
Action: view,
Flags: []cli.Flag{
// Worker Flags
&cli.StringFlag{
EnvVars: []string{"VELA_WORKER_HOSTNAME", "WORKER_HOSTNAME"},
Name: internal.FlagWorkerHostname,
Aliases: []string{"wh"},
Usage: "provide the hostname of the worker",
},
&cli.StringFlag{
EnvVars: []string{"VELA_WORKER_REGISTRATION_TOKEN", "WORKER_REGISTRATION_TOKEN"},
Name: internal.FlagWorkerRegistrationToken,
Aliases: []string{"wr"},
Usage: "toggle to show the registration token for the worker",
Value: "false",
},
// Output Flags
&cli.StringFlag{
EnvVars: []string{"VELA_OUTPUT", "WORKER_OUTPUT"},
Name: internal.FlagOutput,
Aliases: []string{"op"},
Usage: "format the output in json, spew or yaml",
Value: "yaml",
},
},
CustomHelpTemplate: fmt.Sprintf(`%s
EXAMPLES:
1. View details of a worker.
$ {{.HelpName}} --worker.hostname MyWorker
2. View registration token for a worker.
$ {{.HelpName}} --worker.hostname MyWorker --worker.registration.token true
3. View details of a worker with json output.
$ {{.HelpName}} --worker.hostname MyWorker --output json
4. View details of a worker when config or environment variables are set.
$ {{.HelpName}}
DOCUMENTATION:
https://go-vela.github.io/docs/reference/cli/worker/view/
`, cli.CommandHelpTemplate),
}
// helper function to capture the provided input
// and create the object used to inspect a worker.
//
//nolint:dupl // ignore similar code with chown, get, remove and repair
func view(c *cli.Context) error {
// load variables from the config file
err := action.Load(c)
if err != nil {
return err
}
// parse the Vela client from the context
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/client?tab=doc#Parse
client, err := client.Parse(c)
if err != nil {
return err
}
// create the worker configuration
//
// https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config
w := &worker.Config{
Action: internal.ActionView,
Hostname: c.String(internal.FlagWorkerHostname),
RegistrationToken: c.Bool(internal.FlagWorkerRegistrationToken),
Output: c.String(internal.FlagOutput),
}
// validate worker configuration
//
// https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.Validate
err = w.Validate()
if err != nil {
return err
}
// execute the view call for the worker configuration
//
// https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.View
return w.View(client)
}