// New Config creates a new Config object based on the given URI and other data func NewConfig(uri string, authFile string, sshAgent bool, createPty bool, bindIoStreams bool) *Config { // prefix the uri with ssh:// if invalid if !strings.HasPrefix(uri, "ssh://") { uri = "ssh://" + uri } parsed, err := url.Parse(uri) if err != nil { logger.Fatal("Invalid uri provided: " + uri) } var ( user string pass string port string host string ) // load the user and password if provided if parsed.User != nil { user = parsed.User.Username() pass, _ = parsed.User.Password() } // load host and port if available host, port, err = net.SplitHostPort(parsed.Host) if err != nil { host = parsed.Host } // add default values if empty if host == "" { host = "localhost" } if port == "" { port = "22" } iPort, _ := strconv.Atoi(port) // return the filled config object return &Config{ Host: host, User: user, Password: pass, Port: iPort, AuthFile: authFile, SSHAgent: sshAgent, CreatePty: createPty, BindIOStreams: bindIoStreams, } }
package cmd import ( "github.com/Around25/shellbot/logger" "github.com/Around25/shellbot/ops" "github.com/spf13/cobra" ) // copyCmd represents the copy command var copyCmd = &cobra.Command{ Use: "copy", Short: "Copy a file or directory between the local environment and a specified server", Long: `Copy a file or directory between the local environment and a specified server`, Run: func(cmd *cobra.Command, args []string) { err := ops.Copy(args[0], args[1], ops.NewConfig(AppConfig)) if err != nil { logger.Fatal(err) } }, } func init() { RootCmd.AddCommand(copyCmd) }
import ( "github.com/Around25/shellbot/logger" "github.com/Around25/shellbot/ops" "github.com/spf13/cobra" ) // shellCmd represents the shell command var shellCmd = &cobra.Command{ Use: "shell", Short: "Connect to a single server through ssh", Long: `Using shell you can connect open a ssh session to a server.`, Run: func(cmd *cobra.Command, args []string) { var name string if len(args) == 0 { logger.Fatal("Specify the name of the server you want to connect to.") } // get the name of the server as the first argument name = args[0] err := ops.OpenTerminalToServer(name, ops.NewConfig(AppConfig)) if err != nil { logger.Fatal(err) } }, } func init() { RootCmd.AddCommand(shellCmd) }
import ( "github.com/Around25/shellbot/logger" "github.com/Around25/shellbot/ops" "github.com/spf13/cobra" ) // setupCmd represents the setup command var setupCmd = &cobra.Command{ Use: "setup", Short: "Execute all the tasks associated with each group of servers", Long: `Execute all the tasks associated with each group of servers`, Run: func(cmd *cobra.Command, args []string) { var name string if len(args) == 0 { logger.Fatal("No environment specified") } name = args[0] err := ops.ProvisionEnvironment(name, ops.NewConfig(AppConfig)) if err != nil { logger.Fatal(err) } }, } func init() { RootCmd.AddCommand(setupCmd) }