exec

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: MIT Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExecCmd = &cobra.Command{
	Use:   "exec [USER@]SERVER COMMAND...",
	Short: "Execute a command on a remote server",
	Long: `Execute a command on a remote server.
	
	This command executes a specified command on a remote server and returns the output.
	It supports SSH-like syntax for specifying the user and server.
	
	Examples:
	  alpacon exec prod-docker docker ps
	  alpacon exec root@prod-docker docker ps
	  alpacon exec admin@web-server ls -la /var/log
	  alpacon exec -u root prod-docker systemctl status nginx
	  alpacon exec -g docker user@server docker images
	`,
	Args: cobra.MinimumNArgs(2),
	Run: func(cmd *cobra.Command, args []string) {
		username, _ := cmd.Flags().GetString("username")
		groupname, _ := cmd.Flags().GetString("groupname")

		if len(args) < 2 {
			utils.CliErrorWithExit("You must specify at least a server name and a command.")
			return
		}

		serverName := args[0]
		commandArgs := args[1:]

		if strings.Contains(serverName, "@") && !strings.Contains(serverName, ":") {
			sshTarget := utils.ParseSSHTarget(serverName)
			if username == "" && sshTarget.User != "" {
				username = sshTarget.User
			}
			serverName = sshTarget.Host
		}

		alpaconClient, err := client.NewAlpaconAPIClient()
		if err != nil {
			utils.CliErrorWithExit("Connection to Alpacon API failed: %s. Consider re-logging.", err)
			return
		}

		command := strings.Join(commandArgs, " ")
		env := make(map[string]string)

		result, err := event.RunCommand(alpaconClient, serverName, command, username, groupname, env)
		if err != nil {
			err = utils.HandleCommonErrors(err, serverName, utils.ErrorHandlerCallbacks{
				OnMFARequired: func(srv string) error {
					return mfa.HandleMFAError(alpaconClient, srv)
				},
				OnUsernameRequired: func() error {
					_, err := iam.HandleUsernameRequired()
					return err
				},
				RetryOperation: func() error {
					result, err = event.RunCommand(alpaconClient, serverName, command, username, groupname, env)
					return err
				},
			})

			if err != nil {
				utils.CliErrorWithExit("Failed to execute command on '%s' server: %s.", serverName, err)
				return
			}
		}
		fmt.Println(result)
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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