From 420133eb9fda8a56f5b5e0b21490b3796212e164 Mon Sep 17 00:00:00 2001 From: Xudong Guo Date: Mon, 8 Jan 2024 15:03:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20docs=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Closes: #830 --- cmd/add.go | 1 + cmd/alias.go | 1 + cmd/clear.go | 1 + cmd/cloud.go | 1 + cmd/delete.go | 2 +- cmd/docs.go | 39 +++++++++++++++++++++++++++++++++++++++ cmd/export.go | 1 + cmd/list.go | 1 + cmd/merge.go | 1 + cmd/namespace.go | 1 + cmd/rename.go | 1 + cmd/switch.go | 1 + go.mod | 1 + go.sum | 3 +++ 14 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 cmd/docs.go diff --git a/cmd/add.go b/cmd/add.go index 789ff462..b80675ff 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -39,6 +39,7 @@ func (ac *AddCommand) Init() { ac.command.Flags().String("context-name", "", "override context name when add kubeconfig context") ac.command.PersistentFlags().BoolP("cover", "c", false, "Overwrite local kubeconfig files") _ = ac.command.MarkFlagRequired("file") + ac.AddCommands(&DocsCommand{}) } func (ac *AddCommand) runAdd(cmd *cobra.Command, args []string) error { diff --git a/cmd/alias.go b/cmd/alias.go index 05a796bf..2a69f9c2 100644 --- a/cmd/alias.go +++ b/cmd/alias.go @@ -31,6 +31,7 @@ func (al *AliasCommand) Init() { al.command.DisableFlagsInUseLine = true al.command.Flags().StringP("out", "o", "", "output to ~/.zshrc or ~/.bash_profile") _ = al.command.MarkFlagRequired("out") + al.AddCommands(&DocsCommand{}) } // SourceCmd source command diff --git a/cmd/clear.go b/cmd/clear.go index 4857cad2..c29aaeb6 100644 --- a/cmd/clear.go +++ b/cmd/clear.go @@ -26,6 +26,7 @@ func (cl *ClearCommand) Init() { Example: clearExample(), } cl.command.DisableFlagsInUseLine = true + cl.AddCommands(&DocsCommand{}) } func (cl *ClearCommand) runClear(cmd *cobra.Command, args []string) error { diff --git a/cmd/cloud.go b/cmd/cloud.go index 54bcc922..fd4243e3 100644 --- a/cmd/cloud.go +++ b/cmd/cloud.go @@ -70,6 +70,7 @@ func (cc *CloudCommand) Init() { cc.command.PersistentFlags().String("region_id", "", "cloud region id") cc.AddCommands(&CloudAddCommand{}) cc.AddCommands(&CloudListCommand{}) + cc.AddCommands(&DocsCommand{}) } func getClusters(provider, regionID string, num int) ([]cloud.ClusterInfo, error) { diff --git a/cmd/delete.go b/cmd/delete.go index 4dec1611..bb0ac237 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -26,7 +26,7 @@ func (dc *DeleteCommand) Init() { }, Example: deleteExample(), } - + dc.AddCommands(&DocsCommand{}) } func (dc *DeleteCommand) runDelete(command *cobra.Command, args []string) error { diff --git a/cmd/docs.go b/cmd/docs.go new file mode 100644 index 00000000..6011dc6f --- /dev/null +++ b/cmd/docs.go @@ -0,0 +1,39 @@ +package cmd + +import ( + "fmt" + "github.com/pkg/browser" + "github.com/spf13/cobra" +) + +type DocsCommand struct { + BaseCommand +} + +var DOCS = "https://kubecm.cloud/" + +// Init DocsCommand +func (dc *DocsCommand) Init() { + dc.command = &cobra.Command{ + Use: "docs", + Short: "Open document website", + Long: "Open document website in your browser", + RunE: func(cmd *cobra.Command, args []string) error { + return dc.runDocs(cmd, args) + }, + Example: docsExample(), + } +} + +func (dc *DocsCommand) runDocs(cmd *cobra.Command, args []string) error { + url := fmt.Sprintf("%s#/en-us/cli/kubecm_%s", DOCS, cmd.Parent().Use) + fmt.Printf("Opened %s in your browser.\n", url) + return browser.OpenURL(url) +} + +func docsExample() string { + return ` +# Open add command document page +kubecm add docs +` +} diff --git a/cmd/export.go b/cmd/export.go index ee1a738c..13168378 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -27,6 +27,7 @@ func (ec *ExportCommand) Init() { } ec.command.Flags().StringP("file", "f", "", "Path to export kubeconfig files") _ = ec.command.MarkFlagRequired("file") + ec.AddCommands(&DocsCommand{}) } func (ec *ExportCommand) runExport(args []string) error { diff --git a/cmd/list.go b/cmd/list.go index 6aa46211..0da12226 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -29,6 +29,7 @@ func (lc *ListCommand) Init() { Example: listExample(), } lc.command.DisableFlagsInUseLine = true + lc.AddCommands(&DocsCommand{}) } func (lc *ListCommand) runList(command *cobra.Command, args []string) error { diff --git a/cmd/merge.go b/cmd/merge.go index cc48a3ad..b7475aa9 100644 --- a/cmd/merge.go +++ b/cmd/merge.go @@ -31,6 +31,7 @@ func (mc *MergeCommand) Init() { mc.command.Flags().StringP("folder", "f", "", "KubeConfig folder") mc.command.Flags().BoolP("assumeyes", "y", false, "skip interactive file overwrite confirmation") //_ = mc.command.MarkFlagRequired("folder") + mc.AddCommands(&DocsCommand{}) } func (mc MergeCommand) runMerge(command *cobra.Command, args []string) error { diff --git a/cmd/namespace.go b/cmd/namespace.go index 65973609..09bd3c94 100644 --- a/cmd/namespace.go +++ b/cmd/namespace.go @@ -33,6 +33,7 @@ Switch or change namespace interactively }, Example: namespaceExample(), } + nc.AddCommands(&DocsCommand{}) } func (nc *NamespaceCommand) runNamespace(command *cobra.Command, args []string) error { diff --git a/cmd/rename.go b/cmd/rename.go index 5ebe698a..cb561ea0 100644 --- a/cmd/rename.go +++ b/cmd/rename.go @@ -26,6 +26,7 @@ func (rc *RenameCommand) Init() { }, Example: renameExample(), } + rc.AddCommands(&DocsCommand{}) } func (rc *RenameCommand) runRename(command *cobra.Command, args []string) error { diff --git a/cmd/switch.go b/cmd/switch.go index 89b37a15..f0e1a441 100644 --- a/cmd/switch.go +++ b/cmd/switch.go @@ -35,6 +35,7 @@ Switch Kube Context interactively }, Example: switchExample(), } + sc.AddCommands(&DocsCommand{}) } func (sc *SwitchCommand) runSwitch(command *cobra.Command, args []string) error { diff --git a/go.mod b/go.mod index aa21bff9..7669b79d 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/Azure/go-autorest/autorest v0.11.24 github.com/Azure/go-autorest/autorest/azure/auth v0.5.12 github.com/aws/aws-sdk-go v1.49.9 + github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c github.com/stretchr/testify v1.8.4 ) diff --git a/go.sum b/go.sum index e81d992f..b31187bd 100644 --- a/go.sum +++ b/go.sum @@ -481,6 +481,8 @@ github.com/paulmach/orb v0.1.3/go.mod h1:VFlX/8C+IQ1p6FTRRKzKoOPJnvEtA5G0Veuqwbu github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= +github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= +github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -766,6 +768,7 @@ golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= From 5fa4834fee155800dedcc0ec5acebd0bd25eb852 Mon Sep 17 00:00:00 2001 From: Xudong Guo Date: Mon, 8 Jan 2024 15:21:48 +0800 Subject: [PATCH 2/2] update --- cmd/cmd.go | 1 + cmd/docs.go | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index bc1cd3ff..47a0ceba 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -21,6 +21,7 @@ func NewBaseCommand() *BaseCommand { &CreateCommand{}, // create command &CloudCommand{}, // cloud command &ExportCommand{}, // export command + &DocsCommand{}, // docs command ) return baseCmd diff --git a/cmd/docs.go b/cmd/docs.go index 6011dc6f..b2d1e507 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -26,13 +26,20 @@ func (dc *DocsCommand) Init() { } func (dc *DocsCommand) runDocs(cmd *cobra.Command, args []string) error { - url := fmt.Sprintf("%s#/en-us/cli/kubecm_%s", DOCS, cmd.Parent().Use) + var url string + if cmd.Parent().Use == "kubecm" { + url = DOCS + } else { + url = fmt.Sprintf("%s#/en-us/cli/kubecm_%s", DOCS, cmd.Parent().Use) + } fmt.Printf("Opened %s in your browser.\n", url) return browser.OpenURL(url) } func docsExample() string { return ` +# Open kubecm website +kubecm docs # Open add command document page kubecm add docs `