Files
kt-connect/pkg/kt/command/check.go
T
2020-03-12 17:01:58 +08:00

84 lines
1.9 KiB
Go

package command
import (
osexec "os/exec"
"github.com/alibaba/kt-connect/pkg/kt"
"runtime"
"github.com/alibaba/kt-connect/pkg/kt/options"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/cli"
"github.com/alibaba/kt-connect/pkg/kt/exec"
"github.com/alibaba/kt-connect/pkg/kt/exec/kubectl"
"github.com/alibaba/kt-connect/pkg/kt/exec/ssh"
"github.com/alibaba/kt-connect/pkg/kt/exec/sshuttle"
)
// NewCheckCommand return new check command
func NewCheckCommand(ktcli kt.CliInterface, options *options.DaemonOptions, action ActionInterface) cli.Command {
return cli.Command{
Name: "check",
Usage: "check local dependency for ktctl",
Action: func(c *cli.Context) error {
if options.Debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
return action.Check(options)
},
}
}
// Check check local denpendency for kt connect
func (action *Action) Check(options *options.DaemonOptions) (err error) {
log.Info().Msgf("system info %s-%s", runtime.GOOS, runtime.GOARCH)
sshCli := ssh.Cli{}
kubernetesCli := kubectl.Cli{
KubeConfig: options.KubeConfig,
}
uttle := sshuttle.Cli{}
err = runCommandWithMsg(
sshCli.Version(),
"checking ssh version", "ssh is missing, please make sure command ssh is work right at your local first",
)
if err != nil {
return
}
err = runCommandWithMsg(
kubernetesCli.Version(),
"checking kubectl version", "kubectl is missing, please make sure kubectl is working right at your local first",
)
if err != nil {
return
}
err = runCommandWithMsg(
uttle.Version(),
"checking sshuttle version", "sshuttle is missing, you can only use 'ktctl connect --method socks5' with Socks5 proxy mode",
)
if err != nil {
return
}
log.Info().Msg("KT Connect is ready, enjoy it!")
return nil
}
func runCommandWithMsg(cmd *osexec.Cmd, title string, msg string) (err error) {
log.Info().Msg(title)
err = exec.RunAndWait(cmd, title, true)
if err != nil {
log.Warn().Msg(msg)
}
return
}