go cobra执行命令
/*
Copyright 2022 cuisongliu@qq.com.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"github.com/spf13/cobra"
"github.com/labring/sealos/pkg/clusterfile"
"github.com/labring/sealos/pkg/ssh"
"github.com/labring/sealos/pkg/types/v1beta1"
)
var roles string
var clusterName string
var ips []string
var exampleExec = `
exec to default cluster: default
sealos exec "cat /etc/hosts"
specify the cluster name(If there is only one cluster in the $HOME/.sealos directory, it should be applied. ):
sealos exec -c my-cluster "cat /etc/hosts"
set role label to exec cmd:
sealos exec -c my-cluster -r master,node "cat /etc/hosts"
set ips to exec cmd:
sealos exec -c my-cluster --ips 172.16.1.38 "cat /etc/hosts"
`
func newExecCmd() *cobra.Command {
var cluster *v1beta1.Cluster
var execCmd = &cobra.Command{
Use: "exec",
Short: "Execute shell command or script on specified nodes",
Example: exampleExec,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(ips) > 0 {
execIPCmd, err := ssh.NewExecCmdFromIPs(cluster, ips)
if err != nil {
return err
}
return execIPCmd.RunCmd(args[0])
}
execRoleCmd, err := ssh.NewExecCmdFromRoles(cluster, roles)
if err != nil {
return err
}
return execRoleCmd.RunCmd(args[0])
},
PreRunE: func(cmd *cobra.Command, args []string) error {
cls, err := clusterfile.GetClusterFromName(clusterName)
if err != nil {
return err
}
cluster = cls
return nil
},
}
execCmd.Flags().StringVarP(&clusterName, "cluster", "c", "default", "name of cluster to applied exec action")
execCmd.Flags().StringVarP(&roles, "roles", "r", "", "run command on nodes with role")
execCmd.Flags().StringSliceVar(&ips, "ips", []string{}, "run command on nodes with ip address")
return execCmd
}
分类:
GO
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
2020-07-29 MySQL容器部署及数据持久化(主从复制)
2020-07-29 Tomcat9基于单实例转换多实例配置过程
2020-07-29 python入门图
2020-07-29 java apm工具
2020-07-29 JAVA微服务相关组件
2020-07-29 Kubernetes深入Pod控制器-Deployment控制器
2020-07-29 使用Openresty实现WAF防火墙功能