-
Notifications
You must be signed in to change notification settings - Fork 15
/
show.go
92 lines (76 loc) · 2.22 KB
/
show.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
// Copyright 2020 by Red Hat, Inc. All rights reserved.
// Use of this source is goverend by the Apache License
// that can be found in the LICENSE file.
package blueprints
import (
"fmt"
"github.com/spf13/cobra"
"github.com/osbuild/weldr-client/v2/cmd/composer-cli/root"
)
var (
showCmd = &cobra.Command{
Use: "show BLUEPRINT,...",
Short: "Show the blueprints in TOML format",
Example: ` composer-cli blueprints show tmux-image
composer-cli blueprints show --commit 73da334ba39116cf2af86a6ed5a19598bb9bfdc8 tmux-image`,
RunE: show,
Args: cobra.MinimumNArgs(1),
}
commit string
)
func init() {
showCmd.Flags().StringVarP(&commit, "commit", "", "", "blueprint commit to retrieve instead of the latest.")
blueprintsCmd.AddCommand(showCmd)
}
func show(cmd *cobra.Command, args []string) (rcErr error) {
if len(commit) > 0 && len(args) > 1 {
return root.ExecutionError(cmd, "--commit only supports one blueprint name at a time")
}
if len(commit) > 0 {
return showCommit(cmd, args[0], commit)
}
// Show one or more blueprints, retrieving the latest version
names := root.GetCommaArgs(args)
if root.JSONOutput {
_, errors, err := root.Client.GetBlueprintsJSON(names)
if err != nil {
return root.ExecutionError(cmd, "Show Error: %s", err)
}
if errors != nil {
return root.ExecutionErrors(cmd, errors)
}
return nil
}
blueprints, resp, err := root.Client.GetBlueprintsTOML(names)
if err != nil {
return root.ExecutionError(cmd, "Show Error: %s", err)
}
if resp != nil && !resp.Status {
rcErr = root.ExecutionErrors(cmd, resp.Errors)
}
for _, bp := range blueprints {
fmt.Println(bp)
}
return rcErr
}
func showCommit(cmd *cobra.Command, name, commit string) error {
if root.JSONOutput {
_, resp, err := root.Client.GetBlueprintChangeJSON(name, commit)
if err != nil {
return root.ExecutionError(cmd, "Show Error: %s", err)
}
if resp != nil {
return root.ExecutionErrors(cmd, resp.Errors)
}
return nil
}
blueprint, resp, err := root.Client.GetBlueprintChangeTOML(name, commit)
if err != nil {
return root.ExecutionError(cmd, "Show Error: %s", err)
}
if resp != nil && !resp.Status {
return root.ExecutionErrors(cmd, resp.Errors)
}
fmt.Println(blueprint)
return nil
}