cmd

package
v0.0.0-...-e3315c2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "pretty",
	Short: "Parallel remote execution tty",
	Long: `Parallel remote execution tty - (Yet another parallel ssh/shell)

usage:
	pretty <host1> <host2> <host3>...
`,

	Args: func(cmd *cobra.Command, args []string) error {
		if len(args) < 1 && hostGroup == "" && hostsFile == "" {
			return errors.New("requires at least one host, hostGroup ou hostsFile")
		}
		return nil
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		argsLen := len(args)
		hostSpecs, err := parseArgsHosts(args)
		if err != nil {
			return err
		}

		if hostGroup != "" {
			groupSpecs, err := parseGroupSpecs(viper.Get(fmt.Sprintf("groups.%s", hostGroup)), hostGroup)
			if err != nil {
				return err
			}
			if argsLen > 1 {
				hostSpecs = append(hostSpecs, groupSpecs...)
			} else if argsLen < 1 {
				hostSpecs = groupSpecs
			}
		}

		if hostsFile != "" {
			data, err := ioutil.ReadFile(hostsFile)
			if err != nil {
				return fmt.Errorf("unable to read hostsFile: %w", err)
			}
			fileSpecs, err := parseHostsFile(data)
			if err != nil {
				return err
			}
			hostSpecs = append(hostSpecs, fileSpecs...)
		}

		var colors = []color.Attribute{
			color.FgRed,
			color.FgGreen,
			color.FgYellow,
			color.FgBlue,
			color.FgMagenta,
			color.FgCyan,
			color.FgWhite,
			color.FgHiRed,
			color.FgHiGreen,
			color.FgHiYellow,
			color.FgHiBlue,
			color.FgHiMagenta,
			color.FgHiCyan,
			color.FgHiWhite,
		}

		for len(colors) <= len(hostSpecs) {
			colors = append(colors, colors...)
		}

		userConfigPath := ""
		if home, err := os.UserHomeDir(); err == nil {
			userConfigPath = filepath.Join(home, ".ssh", "config")
		}
		resolver, err := loadSSHConfigFunc(sshConn.SSHConfigPaths{
			User:   userConfigPath,
			System: "/etc/ssh/ssh_config",
		})
		if err != nil {
			return fmt.Errorf("unable to load ssh config: %w", err)
		}

		globalUser := strings.TrimSpace(viper.GetString("username"))

		hostList := sshConn.NewHostList()
		for pos, spec := range hostSpecs {
			resolveSpec := sshConn.HostSpec{
				Alias:   spec.Host,
				Host:    spec.Host,
				Port:    spec.Port,
				User:    spec.User,
				PortSet: spec.PortSet,
				UserSet: spec.UserSet,
			}
			if !resolveSpec.UserSet && globalUser != "" {
				resolveSpec.User = globalUser
				resolveSpec.UserSet = true
			}
			resolved, err := resolveHostFunc(resolver, resolveSpec, "")
			if err != nil {
				return fmt.Errorf("unable to resolve host %q: %w", spec.Host, err)
			}
			jumps := make([]sshConn.ResolvedHost, 0, len(resolved.ProxyJump))
			for _, jumpAlias := range resolved.ProxyJump {
				jumpSpec := sshConn.HostSpec{Alias: jumpAlias, Host: jumpAlias}
				if globalUser != "" {
					jumpSpec.User = globalUser
					jumpSpec.UserSet = true
				}
				jumpResolved, err := resolveHostFunc(resolver, jumpSpec, "")
				if err != nil {
					return fmt.Errorf("unable to resolve jump host %q: %w", jumpAlias, err)
				}
				jumps = append(jumps, jumpResolved)
			}
			displayName := hostDisplayName(HostSpec{Host: resolved.Host, Port: resolved.Port})
			host := &sshConn.Host{
				Hostname:      displayName,
				Alias:         resolved.Alias,
				Host:          resolved.Host,
				Port:          resolved.Port,
				User:          resolved.User,
				IdentityFiles: resolved.IdentityFiles,
				ProxyJump:     jumps,
				Color:         color.New(colors[pos%len(colors)]),
			}
			hostList.AddHost(host)
		}
		spawnShellFunc(hostList)
		return nil
	},
}

RootCmd represents the base command when called without any subcommands

Functions

func Execute

func Execute() error

Execute adds all child commands to the root command and sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.

Types

type HostSpec

type HostSpec struct {
	Host    string
	Port    int
	User    string
	PortSet bool
	UserSet bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL