From c515f3cf62b2450eb36dc659974fd8875cf56f48 Mon Sep 17 00:00:00 2001 From: Ce Gao Date: Tue, 2 Aug 2022 16:22:02 +0800 Subject: [PATCH] feat(CLI): Add category and refine help text (#707) * feat(CLI): Add category and refine help text Signed-off-by: Ce Gao * fix: Add license Signed-off-by: Ce Gao --- pkg/app/app.go | 34 ++++++++++++++++++++++++++++++---- pkg/app/bootstrap.go | 5 +++-- pkg/app/build.go | 18 ++++++++---------- pkg/app/const.go | 22 ++++++++++++++++++++++ pkg/app/context.go | 5 +++-- pkg/app/destroy.go | 7 ++++--- pkg/app/env.go | 7 ++++--- pkg/app/image.go | 7 ++++--- pkg/app/init.go | 7 ++++--- pkg/app/pause.go | 7 ++++--- pkg/app/prune.go | 5 +++-- pkg/app/resume.go | 7 ++++--- pkg/app/run.go | 5 +++-- pkg/app/up.go | 7 ++++--- pkg/app/version.go | 9 +++++---- 15 files changed, 105 insertions(+), 47 deletions(-) create mode 100644 pkg/app/const.go diff --git a/pkg/app/app.go b/pkg/app/app.go index 2d02da8ef..55d2dad7a 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -37,6 +37,8 @@ func New() EnvdApp { internalApp.EnableBashCompletion = true internalApp.Name = "envd" internalApp.Usage = "Development environment for data science and AI/ML teams" + internalApp.HideHelpCommand = true + internalApp.HideVersion = true internalApp.Version = version.GetVersion().String() internalApp.Flags = []cli.Flag{ &cli.BoolFlag{ @@ -44,16 +46,17 @@ func New() EnvdApp { Usage: "enable debug output in logs", }, &cli.StringFlag{ - Name: flag.FlagBuildkitdImage, - Usage: "docker image to use for buildkitd", - Value: "docker.io/moby/buildkit:v0.10.3", + Name: flag.FlagBuildkitdImage, + Usage: "docker image to use for buildkitd", + Value: "docker.io/moby/buildkit:v0.10.3", + Hidden: true, }, } internalApp.Commands = []*cli.Command{ CommandBootstrap, - CommandBuild, CommandContext, + CommandBuild, CommandDestroy, CommandEnvironment, CommandImage, @@ -66,6 +69,29 @@ func New() EnvdApp { CommandVersion, } + internalApp.CustomAppHelpTemplate = ` envd - Development environment for data science and AI/ML teams + + Usage: + envd up --path + + envd run --name --command "pip list"{{if .VisibleCommands}} + + Build and launch envd environments. Get more information at: https://envd.tensorchord.ai/. + To get started with using envd, check out the getting started guide: https://envd.tensorchord.ai/guide/getting-started.html. + {{range .VisibleCategories}}{{if .Name}} + {{.Name}}:{{range .VisibleCommands}} + {{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}} + {{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{end}}{{if .VisibleFlagCategories}} + + Global Options:{{range .VisibleFlagCategories}} + {{if .Name}}{{.Name}} + {{end}}{{range .Flags}}{{.}} + {{end}}{{end}}{{else}}{{if .VisibleFlags}} + + Global Options: + {{range $index, $option := .VisibleFlags}}{{if $index}} + {{end}}{{wrap $option.String 6}}{{end}}{{end}}{{end}}` + // Deal with debug flag. var debugEnabled bool diff --git a/pkg/app/bootstrap.go b/pkg/app/bootstrap.go index 5252e26a0..df6150574 100644 --- a/pkg/app/bootstrap.go +++ b/pkg/app/bootstrap.go @@ -32,8 +32,9 @@ import ( ) var CommandBootstrap = &cli.Command{ - Name: "bootstrap", - Usage: "Bootstrap the envd installation including shell autocompletion and buildkit image download", + Name: "bootstrap", + Category: CategoryManagement, + Usage: "Bootstrap the envd installation", Flags: []cli.Flag{ &cli.BoolFlag{ Name: "buildkit", diff --git a/pkg/app/build.go b/pkg/app/build.go index 9164603c1..895224bce 100644 --- a/pkg/app/build.go +++ b/pkg/app/build.go @@ -20,21 +20,20 @@ import ( "github.com/cockroachdb/errors" "github.com/sirupsen/logrus" - "github.com/spf13/viper" "github.com/urfave/cli/v2" "github.com/tensorchord/envd/pkg/builder" "github.com/tensorchord/envd/pkg/docker" - "github.com/tensorchord/envd/pkg/flag" "github.com/tensorchord/envd/pkg/home" sshconfig "github.com/tensorchord/envd/pkg/ssh/config" "github.com/tensorchord/envd/pkg/util/fileutil" ) var CommandBuild = &cli.Command{ - Name: "build", - Aliases: []string{"b"}, - Usage: "Build the envd environment", + Name: "build", + Category: CategoryBasic, + Aliases: []string{"b"}, + Usage: "Build the envd environment", Description: ` To build an image using build.envd: $ envd build @@ -123,11 +122,10 @@ func build(clicontext *cli.Context) error { } logger := logrus.WithFields(logrus.Fields{ - "build-context": buildContext, - "build-file": manifest, - "config": cfg, - "tag": tag, - flag.FlagBuildkitdImage: viper.GetString(flag.FlagBuildkitdImage), + "build-context": buildContext, + "build-file": manifest, + "config": cfg, + "tag": tag, }) debug := clicontext.Bool("debug") output := clicontext.String("output") diff --git a/pkg/app/const.go b/pkg/app/const.go new file mode 100644 index 000000000..07cc04981 --- /dev/null +++ b/pkg/app/const.go @@ -0,0 +1,22 @@ +// Copyright 2022 The envd Authors +// +// 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 app + +const ( + CategoryBasic = "Basic Commands" + CategoryManagement = "Management Commands" + CategoryAdvanced = "Advanced Commands" + CategoryOther = "Other Commands" +) diff --git a/pkg/app/context.go b/pkg/app/context.go index 5dc40b6ee..87fa3feb2 100644 --- a/pkg/app/context.go +++ b/pkg/app/context.go @@ -19,8 +19,9 @@ import ( ) var CommandContext = &cli.Command{ - Name: "context", - Usage: "Manage envd contexts", + Name: "context", + Category: CategoryManagement, + Usage: "Manage envd contexts", Subcommands: []*cli.Command{ CommandContextCreate, CommandContextList, diff --git a/pkg/app/destroy.go b/pkg/app/destroy.go index 75e915979..06b0fc284 100644 --- a/pkg/app/destroy.go +++ b/pkg/app/destroy.go @@ -27,9 +27,10 @@ import ( ) var CommandDestroy = &cli.Command{ - Name: "destroy", - Aliases: []string{"d"}, - Usage: "Destroy the envd environment", + Name: "destroy", + Category: CategoryBasic, + Aliases: []string{"d"}, + Usage: "Destroy the envd environment", Flags: []cli.Flag{ &cli.PathFlag{ Name: "path", diff --git a/pkg/app/env.go b/pkg/app/env.go index 8c3119196..14ded632b 100644 --- a/pkg/app/env.go +++ b/pkg/app/env.go @@ -30,9 +30,10 @@ import ( ) var CommandEnvironment = &cli.Command{ - Name: "envs", - Aliases: []string{"env", "e"}, - Usage: "Manage envd environments", + Name: "envs", + Category: CategoryBasic, + Aliases: []string{"env", "e"}, + Usage: "Manage envd environments", Subcommands: []*cli.Command{ CommandDescribeEnvironment, diff --git a/pkg/app/image.go b/pkg/app/image.go index 13389f519..107377fa4 100644 --- a/pkg/app/image.go +++ b/pkg/app/image.go @@ -30,9 +30,10 @@ import ( ) var CommandImage = &cli.Command{ - Name: "images", - Aliases: []string{"image", "i"}, - Usage: "Manage envd images", + Name: "images", + Category: CategoryBasic, + Aliases: []string{"image", "i"}, + Usage: "Manage envd images", Subcommands: []*cli.Command{ CommandDescribeImage, diff --git a/pkg/app/init.go b/pkg/app/init.go index 874796bbd..1fc607dfb 100644 --- a/pkg/app/init.go +++ b/pkg/app/init.go @@ -29,9 +29,10 @@ import ( var templatef embed.FS var CommandInit = &cli.Command{ - Name: "init", - Aliases: []string{"i"}, - Usage: "Initializes the current directory with the build.envd file", + Name: "init", + Category: CategoryManagement, + Aliases: []string{"i"}, + Usage: "Initializes the current directory with the build.envd file", Flags: []cli.Flag{ &cli.StringFlag{ Name: "lang", diff --git a/pkg/app/pause.go b/pkg/app/pause.go index 4f2d81a0b..f774e5f83 100644 --- a/pkg/app/pause.go +++ b/pkg/app/pause.go @@ -23,9 +23,10 @@ import ( ) var CommandPause = &cli.Command{ - Name: "pause", - Aliases: []string{"p"}, - Usage: "Pause the envd environment", + Name: "pause", + Category: CategoryAdvanced, + Aliases: []string{"p"}, + Usage: "Pause the envd environment", Flags: []cli.Flag{ &cli.StringFlag{ Name: "env", diff --git a/pkg/app/prune.go b/pkg/app/prune.go index 07ee6793c..5f3c07658 100644 --- a/pkg/app/prune.go +++ b/pkg/app/prune.go @@ -23,8 +23,9 @@ import ( ) var CommandPrune = &cli.Command{ - Name: "prune", - Usage: "Clean up the build cache", + Name: "prune", + Category: CategoryManagement, + Usage: "Clean up the build cache", Flags: []cli.Flag{ &cli.DurationFlag{ Name: "keep-duration", diff --git a/pkg/app/resume.go b/pkg/app/resume.go index 10ae7fe1f..adb3133fd 100644 --- a/pkg/app/resume.go +++ b/pkg/app/resume.go @@ -23,9 +23,10 @@ import ( ) var CommandResume = &cli.Command{ - Name: "resume", - Aliases: []string{"r"}, - Usage: "Resume the envd environment", + Name: "resume", + Category: CategoryAdvanced, + Aliases: []string{"r"}, + Usage: "Resume the envd environment", Flags: []cli.Flag{ &cli.StringFlag{ Name: "env", diff --git a/pkg/app/run.go b/pkg/app/run.go index caab8e78f..08b705f34 100644 --- a/pkg/app/run.go +++ b/pkg/app/run.go @@ -25,8 +25,9 @@ import ( ) var CommandRun = &cli.Command{ - Name: "run", - Usage: "Spawns a command installed into the environment.", + Name: "run", + Category: CategoryBasic, + Usage: "Spawns a command installed into the environment.", Flags: []cli.Flag{ &cli.PathFlag{ Name: "name", diff --git a/pkg/app/up.go b/pkg/app/up.go index d48ae1026..367853936 100644 --- a/pkg/app/up.go +++ b/pkg/app/up.go @@ -38,9 +38,10 @@ const ( ) var CommandUp = &cli.Command{ - Name: "up", - Aliases: []string{"u"}, - Usage: "Build and run the envd environment", + Name: "up", + Category: CategoryBasic, + Aliases: []string{"u"}, + Usage: "Build and run the envd environment", Flags: []cli.Flag{ &cli.StringFlag{ Name: "tag", diff --git a/pkg/app/version.go b/pkg/app/version.go index 551c603a6..53ad4a729 100644 --- a/pkg/app/version.go +++ b/pkg/app/version.go @@ -23,10 +23,11 @@ import ( ) var CommandVersion = &cli.Command{ - Name: "version", - Aliases: []string{"v"}, - Usage: "Print envd version information", - Action: printVersion, + Name: "version", + Category: CategoryOther, + Aliases: []string{"v"}, + Usage: "Print envd version information", + Action: printVersion, Flags: []cli.Flag{ &cli.BoolFlag{ Name: "short",