github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/shell/shell.go (about) 1 package shell 2 3 import ( 4 "context" 5 "errors" 6 "strings" 7 8 "github.com/henvic/wedeploycli/cmdflagsfromhost" 9 "github.com/henvic/wedeploycli/command/internal/we" 10 "github.com/henvic/wedeploycli/isterm" 11 "github.com/henvic/wedeploycli/shell" 12 "github.com/spf13/cobra" 13 ) 14 15 var setupHost = cmdflagsfromhost.SetupHost{ 16 Pattern: cmdflagsfromhost.FullHostPattern | cmdflagsfromhost.InstancePattern, 17 18 Requires: cmdflagsfromhost.Requires{ 19 Project: true, 20 Service: true, 21 Instance: true, 22 }, 23 24 AutoSelectSingleInstance: true, 25 26 PromptMissingService: true, 27 PromptMissingInstance: true, 28 } 29 30 // ShellCmd opens a shell remotely 31 var ShellCmd = &cobra.Command{ 32 Use: "shell", 33 Aliases: []string{"ssh"}, 34 Short: "Opens a shell on a container of your service\n\t\t", 35 PreRunE: shellPreRun, 36 RunE: shellRun, 37 Args: cobra.NoArgs, 38 } 39 40 func init() { 41 setupHost.Init(ShellCmd) 42 } 43 44 func shellPreRun(cmd *cobra.Command, args []string) error { 45 if !isterm.Stdin() { 46 return errors.New("can't open terminal: tty wasn't found") 47 } 48 49 return setupHost.Process(context.Background(), we.Context()) 50 } 51 52 func shellRun(cmd *cobra.Command, args []string) error { 53 var wectx = we.Context() 54 var host = wectx.Infrastructure() 55 56 host = strings.Replace(host, "http://", "", 1) 57 host = strings.Replace(host, "https://", "", 1) 58 59 var params = shell.Params{ 60 Host: host, 61 Token: wectx.Token(), 62 63 ProjectID: setupHost.Project(), 64 ServiceID: setupHost.Service(), 65 Instance: setupHost.Instance(), 66 67 AttachStdin: true, 68 TTY: true, 69 } 70 71 return shell.Run(context.Background(), params, "") 72 }