Skip to content

Commit

Permalink
Improve(hosts): add default label for host.
Browse files Browse the repository at this point in the history
Signed-off-by: Wine93 <[email protected]>
  • Loading branch information
Wine93 committed Feb 20, 2024
1 parent 1425127 commit 27e280f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 6 additions & 3 deletions cli/command/hosts/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
package hosts

import (
"strings"

"github.com/opencurve/curveadm/cli/cli"
"github.com/opencurve/curveadm/internal/configure/hosts"
"github.com/opencurve/curveadm/internal/tui"
Expand All @@ -32,7 +34,7 @@ import (

type listOptions struct {
verbose bool
labels []string
labels string
}

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

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

return cmd
}
Expand Down Expand Up @@ -156,7 +158,8 @@ func runList(curveadm *cli.CurveAdm, options listOptions) error {
var err error
data := curveadm.Hosts()
if len(data) > 0 {
hcs, err = filter(data, options.labels) // filter hosts
labels := strings.Split(options.labels, ":")
hcs, err = filter(data, labels) // filter hosts
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion internal/configure/hosts/hc_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ func (hc *HostConfig) GetSSHPort() int { return hc.getInt(CONFIG_SSH_P
func (hc *HostConfig) GetPrivateKeyFile() string { return hc.getString(CONFIG_PRIVATE_CONFIG_FILE) }
func (hc *HostConfig) GetForwardAgent() bool { return hc.getBool(CONFIG_FORWARD_AGENT) }
func (hc *HostConfig) GetBecomeUser() string { return hc.getString(CONFIG_BECOME_USER) }
func (hc *HostConfig) GetLabels() []string { return hc.labels }
func (hc *HostConfig) GetEnvs() []string { return hc.envs }

func (hc *HostConfig) GetLabels() []string {
if len(hc.labels) == 0 {
return []string{hc.GetHost()}
}
return hc.labels
}

func (hc *HostConfig) GetUser() string {
user := hc.getString(CONFIG_USER)
if user == "${user}" {
Expand Down

0 comments on commit 27e280f

Please sign in to comment.