Skip to content

Commit 27e280f

Browse files
committed
Improve(hosts): add default label for host.
Signed-off-by: Wine93 <[email protected]>
1 parent 1425127 commit 27e280f

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

cli/command/hosts/list.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
package hosts
2424

2525
import (
26+
"strings"
27+
2628
"github.com/opencurve/curveadm/cli/cli"
2729
"github.com/opencurve/curveadm/internal/configure/hosts"
2830
"github.com/opencurve/curveadm/internal/tui"
@@ -32,7 +34,7 @@ import (
3234

3335
type listOptions struct {
3436
verbose bool
35-
labels []string
37+
labels string
3638
}
3739

3840
func NewListCommand(curveadm *cli.CurveAdm) *cobra.Command {
@@ -51,7 +53,7 @@ func NewListCommand(curveadm *cli.CurveAdm) *cobra.Command {
5153

5254
flags := cmd.Flags()
5355
flags.BoolVarP(&options.verbose, "verbose", "v", false, "Verbose output for hosts")
54-
flags.StringSliceVarP(&options.labels, "labels", "l", []string{}, "Specify the host labels")
56+
flags.StringVarP(&options.labels, "labels", "l", "", "Specify the host labels")
5557

5658
return cmd
5759
}
@@ -156,7 +158,8 @@ func runList(curveadm *cli.CurveAdm, options listOptions) error {
156158
var err error
157159
data := curveadm.Hosts()
158160
if len(data) > 0 {
159-
hcs, err = filter(data, options.labels) // filter hosts
161+
labels := strings.Split(options.labels, ":")
162+
hcs, err = filter(data, labels) // filter hosts
160163
if err != nil {
161164
return err
162165
}

internal/configure/hosts/hc_get.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,15 @@ func (hc *HostConfig) GetSSHPort() int { return hc.getInt(CONFIG_SSH_P
7474
func (hc *HostConfig) GetPrivateKeyFile() string { return hc.getString(CONFIG_PRIVATE_CONFIG_FILE) }
7575
func (hc *HostConfig) GetForwardAgent() bool { return hc.getBool(CONFIG_FORWARD_AGENT) }
7676
func (hc *HostConfig) GetBecomeUser() string { return hc.getString(CONFIG_BECOME_USER) }
77-
func (hc *HostConfig) GetLabels() []string { return hc.labels }
7877
func (hc *HostConfig) GetEnvs() []string { return hc.envs }
7978

79+
func (hc *HostConfig) GetLabels() []string {
80+
if len(hc.labels) == 0 {
81+
return []string{hc.GetHost()}
82+
}
83+
return hc.labels
84+
}
85+
8086
func (hc *HostConfig) GetUser() string {
8187
user := hc.getString(CONFIG_USER)
8288
if user == "${user}" {

0 commit comments

Comments
 (0)