-
Notifications
You must be signed in to change notification settings - Fork 31
/
attribute.go
85 lines (67 loc) · 1.89 KB
/
attribute.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
package cmd
import (
"fmt"
"github.com/minamijoyo/hcledit/editor"
"github.com/spf13/cobra"
)
func init() {
RootCmd.AddCommand(newAttributeCmd())
}
func newAttributeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "attribute",
Short: "Edit attribute",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
cmd.AddCommand(
newAttributeGetCmd(),
newAttributeSetCmd(),
)
return cmd
}
func newAttributeGetCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get <ADDRESS>",
Short: "Get attribute",
Long: `Get matched attribute at a given address
Arguments:
ADDRESS An address of attribute to get.
`,
RunE: runAttributeGetCmd,
}
return cmd
}
func runAttributeGetCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("expected 1 argument, but got %d arguments", len(args))
}
address := args[0]
return editor.GetAttribute(cmd.InOrStdin(), cmd.OutOrStdout(), "-", address)
}
func newAttributeSetCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "set <ADDRESS> <VALUE>",
Short: "Set attribute",
Long: `Set a value of matched attribute at a given address
Arguments:
ADDRESS An address of attribute to set.
VALUE A new value of attribute.
The value is set literally, even if references or expressions.
Thus, if you want to set a string literal "hoge", be sure to
escape double quotes so that they are not discarded by your shell.
e.g.) hcledit attribute set aaa.bbb.ccc '"hoge"'
`,
RunE: runAttributeSetCmd,
}
return cmd
}
func runAttributeSetCmd(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("expected 2 argument, but got %d arguments", len(args))
}
address := args[0]
value := args[1]
return editor.SetAttribute(cmd.InOrStdin(), cmd.OutOrStdout(), "-", address, value)
}