diff --git a/.github/workflows/release-cli.yml b/.github/workflows/release-cli.yml index e6d00ef0f..ab1ea75e2 100644 --- a/.github/workflows/release-cli.yml +++ b/.github/workflows/release-cli.yml @@ -8,7 +8,7 @@ on: env: tagName: ${{ github.ref_name }} GO_VERSION : "1.22" - BinaryName: "obocli" + BinaryName: "okctl" jobs: release-oceanbase-cli: diff --git a/go.mod b/go.mod index c0c95ce79..cd4fedcef 100644 --- a/go.mod +++ b/go.mod @@ -46,6 +46,7 @@ require ( k8s.io/api v0.29.3 k8s.io/apimachinery v0.29.3 k8s.io/client-go v0.29.3 + k8s.io/kubectl v0.0.0 k8s.io/kubernetes v1.27.2 k8s.io/metrics v0.29.3 sigs.k8s.io/controller-runtime v0.15.0 @@ -55,8 +56,10 @@ require ( github.com/Azure/azure-sdk-for-go/sdk/azcore v1.11.1 // indirect github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.2 // indirect github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2 // indirect + github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect github.com/KyleBanks/depth v1.2.1 // indirect + github.com/MakeNowJust/heredoc v1.0.0 // indirect github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect github.com/aws/aws-sdk-go v1.51.25 // indirect @@ -123,9 +126,12 @@ require ( github.com/klauspost/cpuid/v2 v2.2.6 // indirect github.com/kylelemons/godebug v1.1.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect + github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mailru/easyjson v0.7.7 // indirect + github.com/mitchellh/go-wordwrap v1.0.1 // indirect github.com/moby/spdystream v0.2.0 // indirect + github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect @@ -140,6 +146,7 @@ require ( github.com/prometheus/common/sigv4 v0.1.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect github.com/rogpeppe/go-internal v1.13.1 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect @@ -178,6 +185,7 @@ require ( gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/apiextensions-apiserver v0.29.3 // indirect + k8s.io/cli-runtime v0.29.3 // indirect k8s.io/component-base v0.29.3 // indirect k8s.io/klog/v2 v2.120.1 // indirect k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect diff --git a/go.sum b/go.sum index 59b697a74..d95e5e63b 100644 --- a/go.sum +++ b/go.sum @@ -42,6 +42,8 @@ github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.6 github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.6.0/go.mod h1:gZmgV+qBqygoznvqo2J9oKZAFziqhLZ2xE/WVUmzkHA= github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4 v4.3.0 h1:bXwSugBiSbgtz7rOtbfGf+woewp4f06orW9OP5BjHLA= github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4 v4.3.0/go.mod h1:Y/HgrePTmGy9HjdSGTqZNa+apUpTVIEVKXJyARP2lrk= +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 h1:XHOnouVk1mxXfQidrMEnLlPk9UMeRtyBTnEFtxkV0kU= github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= @@ -50,6 +52,8 @@ github.com/Code-Hex/go-generics-cache v1.5.1 h1:6vhZGc5M7Y/YD8cIUcY8kcuQLB4cHR7U github.com/Code-Hex/go-generics-cache v1.5.1/go.mod h1:qxcC9kRVrct9rHeiYpFWSoW1vxyillCVzX13KZG8dl4= github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= +github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= +github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -111,6 +115,8 @@ github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa h1:jQCWAUqqlij9Pgj2i/P github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa/go.mod h1:x/1Gn8zydmfq8dk6e9PdstVsDgu9RuyIIJqAaF//0IM= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= +github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= @@ -420,6 +426,8 @@ github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjS github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.10.3 h1:v9QZf2Sn6AmjXtQeFpdoq/eaNtYP6IN+7lcrygsIAtg= github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0= +github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE= github.com/linode/linodego v1.32.0 h1:OmZzB3iON6uu84VtLFf64uKmAQqJJarvmsVguroioPI= github.com/linode/linodego v1.32.0/go.mod h1:y8GDP9uLVH4jTB9qyrgw79qfKdYJmNCGUOJmfuiOcmI= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= @@ -443,12 +451,16 @@ github.com/miekg/dns v1.1.59 h1:C9EXc/UToRwKLhK5wKU/I4QVsBUc8kE6MkHBkeypWZs= github.com/miekg/dns v1.1.59/go.mod h1:nZpewl5p6IvctfgrckopVx2OlSEHPRO/U4SYkRklrEk= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= +github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8= github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c= +github.com/moby/term v0.0.0-20221205130635-1aeaba878587 h1:HfkjXDfhgVaN5rmueG8cL8KKeFNecRCXFhaJ2qZ5SKA= +github.com/moby/term v0.0.0-20221205130635-1aeaba878587/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -526,6 +538,7 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= @@ -778,6 +791,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -993,6 +1007,8 @@ k8s.io/apiextensions-apiserver v0.29.3 h1:9HF+EtZaVpFjStakF4yVufnXGPRppWFEQ87qnO k8s.io/apiextensions-apiserver v0.29.3/go.mod h1:po0XiY5scnpJfFizNGo6puNU6Fq6D70UJY2Cb2KwAVc= k8s.io/apimachinery v0.29.3 h1:2tbx+5L7RNvqJjn7RIuIKu9XTsIZ9Z5wX2G22XAa5EU= k8s.io/apimachinery v0.29.3/go.mod h1:hx/S4V2PNW4OMg3WizRrHutyB5la0iCUbZym+W0EQIU= +k8s.io/cli-runtime v0.29.3 h1:r68rephmmytoywkw2MyJ+CxjpasJDQY7AGc3XY2iv1k= +k8s.io/cli-runtime v0.29.3/go.mod h1:aqVUsk86/RhaGJwDhHXH0jcdqBrgdF3bZWk4Z9D4mkM= k8s.io/client-go v0.29.3 h1:R/zaZbEAxqComZ9FHeQwOh3Y1ZUs7FaHKZdQtIc2WZg= k8s.io/client-go v0.29.3/go.mod h1:tkDisCvgPfiRpxGnOORfkljmS+UrW+WtXAy2fTvXJB0= k8s.io/component-base v0.29.3 h1:Oq9/nddUxlnrCuuR2K/jp6aflVvc0uDvxMzAWxnGzAo= @@ -1001,6 +1017,8 @@ k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw= k8s.io/klog/v2 v2.120.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 h1:BZqlfIlq5YbRMFko6/PM7FjZpUb45WallggurYhKGag= k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340/go.mod h1:yD4MZYeKMBwQKVht279WycxKyM84kkAx2DPrTXaeb98= +k8s.io/kubectl v0.29.3 h1:RuwyyIU42MAISRIePaa8Q7A3U74Q9P4MoJbDFz9o3us= +k8s.io/kubectl v0.29.3/go.mod h1:yCxfY1dbwgVdEt2zkJ6d5NNLOhhWgTyrqACIoFhpdd4= k8s.io/kubernetes v1.27.2 h1:g4v9oY6u7vBUDEuq4FvC50Bbw2K7GZuvM00IIESWVf4= k8s.io/kubernetes v1.27.2/go.mod h1:U8ZXeKBAPxeb4J4/HOaxjw1A9K6WfSH+fY2SS7CR6IM= k8s.io/metrics v0.29.3 h1:nN+eavbMQ7Kuif2tIdTr2/F2ec2E/SIAWSruTZ+Ye6U= diff --git a/internal/cli/cli.go b/internal/cli/cli.go index b806649d5..7206a3145 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -26,12 +26,26 @@ import ( "github.com/oceanbase/ob-operator/internal/cli/cmd/version" ) +// BinaryName injected by ldflags +var BinaryName = "unknown" + +var rootLongDesc = ` +============================================= + _ _ _ + ___ | | __ ___ | |_ | | + / _ \ | |/ / / __| | __| | | + | (_) | | < | (__ | |_ | | + \___/ |_|\_\ \___| \__| |_| + +============================================= +A Command Line Tool compatible with OceanBase Operator` + // NewCliCmd return ob-operator cli func NewCliCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "obocli", + Use: BinaryName, Short: "OceanBase Operator Cli", - Long: "OceanBase Operator Cli tool to manage OceanBase clusters, tenants, and backup policies.", + Long: rootLongDesc, RunE: func(cmd *cobra.Command, args []string) error { if cmd.Flags().Changed("version") { versionCmd := version.NewCmd() diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go new file mode 100644 index 000000000..cf89aeb7b --- /dev/null +++ b/internal/cli/cli_test.go @@ -0,0 +1,35 @@ +/* +Copyright (c) 2024 OceanBase +ob-operator is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + + http://license.coscl.org.cn/MulanPSL2 + +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. +*/ +package cli_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/oceanbase/ob-operator/internal/cli" +) + +func TestCli(t *testing.T) { + // Test NewCliCmd + cmd := cli.NewCliCmd() + if cmd == nil { + t.Errorf("NewCliCmd() failed") + } else { + t.Logf("NewCliCmd() success") + // Test Runable + err := cmd.RunE(cmd, []string{"help"}) + assert.NoErrorf(t, err, "cli failed to run") + } +} diff --git a/internal/cli/cmd/backup/create.go b/internal/cli/cmd/backup/create.go index b98702acb..5c3f84f20 100644 --- a/internal/cli/cmd/backup/create.go +++ b/internal/cli/cmd/backup/create.go @@ -20,14 +20,6 @@ import ( "github.com/oceanbase/ob-operator/internal/cli/utils" ) -var createExample = ` - # Create a backup policy by OSS - obocli backup create --archive-path=oss:/// --bak-data-path=oss:/// --oss-access-id= --oss-access-key= - - # Create a backup policy by NFS - obocli backup create --archive-path= --bak-data-path= --bak-encryption-password= -` - // NewCreateCmd create an new backup policy func NewCreateCmd() *cobra.Command { o := backup.NewCreateOptions() diff --git a/internal/cli/cmd/backup/examples.go b/internal/cli/cmd/backup/examples.go new file mode 100644 index 000000000..c0f2b185c --- /dev/null +++ b/internal/cli/cmd/backup/examples.go @@ -0,0 +1,26 @@ +/* +Copyright (c) 2024 OceanBase +ob-operator is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + + http://license.coscl.org.cn/MulanPSL2 + +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. +*/ +package backup + +import "k8s.io/kubectl/pkg/util/templates" + +var createExample = templates.Examples(` + # Create a backup policy by OSS + okctl backup create --archive-path=oss:/// --bak-data-path=oss:/// --oss-access-id= --oss-access-key= + + # Create a backup policy by NFS + okctl backup create --archive-path= --bak-data-path= --bak-encryption-password= +`) + +// TODO: add more examples diff --git a/internal/cli/cmd/completion/completion.go b/internal/cli/cmd/completion/completion.go index 5661f1970..a37a39fbd 100644 --- a/internal/cli/cmd/completion/completion.go +++ b/internal/cli/cmd/completion/completion.go @@ -40,13 +40,13 @@ const defaultBoilerPlate = ` var completionLong = `To load completions: Bash: - $ source <(obocli completion bash) + $ source <(okctl completion bash) # To load completions for each session, execute once: # Linux: - $ obocli completion bash > /etc/bash_completion.d/obocli + $ okctl completion bash > /etc/bash_completion.d/okctl # macOS: - $ obocli completion bash > /usr/local/etc/bash_completion.d/obocli + $ okctl completion bash > /usr/local/etc/bash_completion.d/okctl Zsh: @@ -56,26 +56,26 @@ Zsh: $ echo "autoload -U compinit; compinit" >> ~/.zshrc To load completions in your current shell session: - $ source <(obocli completion zsh) + $ source <(okctl completion zsh) # To load completions for each session, execute once: - $ obocli completion zsh > "${fpath[1]}/_obocli" + $ okctl completion zsh > "${fpath[1]}/_okctl" # You will need to start a new shell for this setup to take effect. fish: - $ obocli completion fish | source + $ okctl completion fish | source # To load completions for each session, execute once: - $ obocli completion fish > ~/.config/fish/completions/obocli.fish + $ okctl completion fish > ~/.config/fish/completions/okctl.fish PowerShell: - PS> obocli completion powershell | Out-String | Invoke-Expression + PS> okctl completion powershell | Out-String | Invoke-Expression # To load completions for every new session, run: - PS> obocli completion powershell > obocli.ps1 + PS> okctl completion powershell > okctl.ps1 # and source this file from your PowerShell profile. ` var ( @@ -113,10 +113,10 @@ func NewCmd(out io.Writer, boilerPlate string) *cobra.Command { // RunCompletionE is the entry point for the completion command func RunCompletionE(out io.Writer, boilerPlate string, cmd *cobra.Command, args []string) error { if len(args) == 0 { - return errors.New("shell not specified. See 'obocli completion -h' for help and examples") + return errors.New("shell not specified. See 'okctl completion -h' for help and examples") } if len(args) > 1 { - return errors.New("too many arguments. Expected only the shell type. See 'obocli completion -h' for help and examples") + return errors.New("too many arguments. Expected only the shell type. See 'okctl completion -h' for help and examples") } run, found := completionShells[args[0]] if !found { diff --git a/internal/cli/cmd/completion/completion_test.go b/internal/cli/cmd/completion/completion_test.go index 22441ab8e..80a291e74 100644 --- a/internal/cli/cmd/completion/completion_test.go +++ b/internal/cli/cmd/completion/completion_test.go @@ -46,12 +46,12 @@ func TestCompletions(t *testing.T) { { name: "no args", args: []string{}, - expectedError: "shell not specified. See 'obocli completion -h' for help and examples", + expectedError: "shell not specified. See 'okctl completion -h' for help and examples", }, { name: "too many args", args: []string{"bash", "zsh"}, - expectedError: "too many arguments. Expected only the shell type. See 'obocli completion -h' for help and examples", + expectedError: "too many arguments. Expected only the shell type. See 'okctl completion -h' for help and examples", }, { name: "unsupported shell", @@ -63,7 +63,7 @@ func TestCompletions(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(tt *testing.T) { parentCmd := &cobra.Command{ - Use: "obocli", + Use: "okctl", } out := new(bytes.Buffer) cmd := NewCmd(out, defaultBoilerPlate) diff --git a/internal/cli/cmd/version/version.go b/internal/cli/cmd/version/version.go index 81978c438..927f19e81 100644 --- a/internal/cli/cmd/version/version.go +++ b/internal/cli/cmd/version/version.go @@ -25,11 +25,11 @@ import ( // Injected by build script var ( - OS = "" - Arch = "" - Version = "" - CommitHash = "" - BuildTime = "" + OS = "unknown" + Arch = "unknown" + Version = "unknown" + CommitHash = "unknown" + BuildTime = "unknown" ) // defaultVersionTemplate is the default template for displaying version information. diff --git a/internal/cli/generic/generic.go b/internal/cli/generic/generic.go index 6eb96ee37..88bd5ac36 100644 --- a/internal/cli/generic/generic.go +++ b/internal/cli/generic/generic.go @@ -25,7 +25,7 @@ type ResourceOption struct { Cmd *cobra.Command } -// Parse the args in obocli +// Parse the args in cli func (o *ResourceOption) Parse(cmd *cobra.Command, args []string) error { o.Name = args[0] o.Cmd = cmd diff --git a/internal/cli/utils/logger.go b/internal/cli/utils/logger.go index dfdcdddfc..10f1809d3 100644 --- a/internal/cli/utils/logger.go +++ b/internal/cli/utils/logger.go @@ -14,6 +14,7 @@ See the Mulan PSL v2 for more details. package utils import ( + "fmt" "log" "os" "text/tabwriter" @@ -23,8 +24,12 @@ var fLog *log.Logger var tbLog *log.Logger var tbw = tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0) +// BinaryName injected by ldflags +var BinaryName string + func init() { - fLog = log.New(os.Stdout, "[obocli]: ", 0) + logHead := fmt.Sprintf("[%s]: ", BinaryName) + fLog = log.New(os.Stdout, logHead, 0) tbLog = log.New(tbw, "", 0) } diff --git a/make/cli.mk b/make/cli.mk index e51c7091f..5d9161594 100644 --- a/make/cli.mk +++ b/make/cli.mk @@ -1,10 +1,12 @@ ##@ cli -BINARY_NAME ?= obocli +BINARY_NAME ?= okctl CLI_VERSION ?= 0.1.0 GOARCH ?=$(shell uname -m) GOOS ?= $(shell uname -s | tr LD ld) VERSION_INJECT_PACKAGE=github.com/oceanbase/ob-operator/internal/cli/cmd/version +LOGGER_HEAD_INJECT_PACKAGE=github.com/oceanbase/ob-operator/internal/cli/utils +BINARY_NAME_INJECT_PACKAGE=github.com/oceanbase/ob-operator/internal/cli COMMIT_HASH ?= $(shell git rev-parse --short HEAD) BUILD_TIMESTAMP ?= $(shell date '+%Y%m%d%H%M%S') @@ -15,11 +17,13 @@ else ifeq ($(GOARCH),aarch64) GOARCH = arm64 endif -CLI_BUILD_FLAGS = -p 4 -ldflags="-X '$(VERSION_INJECT_PACKAGE).Version=$(CLI_VERSION)' -X '$(VERSION_INJECT_PACKAGE).CommitHash=$(COMMIT_HASH)' -X '$(VERSION_INJECT_PACKAGE).BuildTime=$(BUILD_TIMESTAMP)' " +# build flags +CLI_BUILD_FLAGS = -p 4 -ldflags="-X '$(VERSION_INJECT_PACKAGE).Version=$(CLI_VERSION)' -X '$(VERSION_INJECT_PACKAGE).CommitHash=$(COMMIT_HASH)' -X '$(VERSION_INJECT_PACKAGE).BuildTime=$(BUILD_TIMESTAMP)' -X '$(BINARY_NAME_INJECT_PACKAGE).BinaryName=$(BINARY_NAME)' -X '$(LOGGER_HEAD_INJECT_PACKAGE).BinaryName=$(BINARY_NAME)'" + # build args CLI_BUILD := GO11MODULE=ON CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build $(CLI_BUILD_FLAGS) -# build dir for obocli +# build dir for cli BUILD_DIR?=bin/ .PHONY: cli-build diff --git a/scripts/create-cli-release.sh b/scripts/create-cli-release.sh index df150de65..0fa4593bc 100755 --- a/scripts/create-cli-release.sh +++ b/scripts/create-cli-release.sh @@ -15,7 +15,7 @@ echo "release tag: $git_tag" # Build the release binaries for every OS/arch combination. # It builds compressed artifacts on $release_dir. function build_binary { - binary_name="obocli" + binary_name="okctl" echo "build $binary_name binaries" version=$1