From 70dc63a20ec792b349a6079b60fe6e5e671b3ccd Mon Sep 17 00:00:00 2001 From: "m.kindritskiy" Date: Tue, 6 Aug 2024 21:12:37 +0300 Subject: [PATCH] fix [#263] - --init creates config if one does not exist --- cmd/root.go | 42 +-------------------- examples/python/lets.yaml | 6 +-- lets.yaml | 7 ++-- main.go | 79 ++++++++++++++++++++++++++++----------- workdir/workdir.go | 4 +- 5 files changed, 69 insertions(+), 69 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 46b7418a..2885a567 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,13 +2,8 @@ package cmd import ( "fmt" - "os" "strings" - "github.com/lets-cli/lets/upgrade" - "github.com/lets-cli/lets/upgrade/registry" - "github.com/lets-cli/lets/workdir" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -19,7 +14,7 @@ func newRootCmd(version string) *cobra.Command { Short: "A CLI task runner", Args: cobra.ArbitraryArgs, RunE: func(cmd *cobra.Command, args []string) error { - return runRoot(cmd, version) + return PrintHelpMessage(cmd) }, TraverseChildren: true, FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true}, @@ -64,38 +59,3 @@ func PrintVersionMessage(cmd *cobra.Command) error { _, err := fmt.Fprintf(cmd.OutOrStdout(), "lets version %s\n", cmd.Version) return err } - -func runRoot(cmd *cobra.Command, version string) error { - selfUpgrade, err := cmd.Flags().GetBool("upgrade") - if err != nil { - return fmt.Errorf("can not get flag 'upgrade': %w", err) - } - - if selfUpgrade { - upgrader, err := upgrade.NewBinaryUpgrader(registry.NewGithubRegistry(cmd.Context()), version) - if err != nil { - return fmt.Errorf("can not upgrade lets: %w", err) - } - - return upgrader.Upgrade() - } - - init, err := cmd.Flags().GetBool("init") - if err != nil { - return fmt.Errorf("can not get flag 'init': %w", err) - } - - if init { - wd, err := os.Getwd() - if err != nil { - log.Fatal(err) - } - if err := workdir.InitLetsFile(wd, version); err != nil { - log.Fatal(err) - } - - return nil - } - - return PrintHelpMessage(cmd) -} diff --git a/examples/python/lets.yaml b/examples/python/lets.yaml index bc1f077d..1c0a6c63 100644 --- a/examples/python/lets.yaml +++ b/examples/python/lets.yaml @@ -26,17 +26,17 @@ commands: depends: - build-server cmd: | - docker-compose up server + docker compose up server postgres: description: Run postgres - cmd: docker-compose up postgres + cmd: docker compose up postgres ishell: description: Run ipython shell depends: - build-server - cmd: docker-compose run --rm -T ishell + cmd: docker compose run --rm -T ishell init-venv: description: Run to init python virtual env in this repo diff --git a/lets.yaml b/lets.yaml index 7e46398e..f5d1b03f 100644 --- a/lets.yaml +++ b/lets.yaml @@ -33,7 +33,8 @@ commands: description: Run unit tests depends: [build-lets-image] cmd: - - docker-compose + - docker + - compose - run - --rm - test @@ -46,7 +47,7 @@ commands: Example: lets test-bats config_version.bats lets test-bats config_version.bats --opts="-f " - cmd: docker-compose run --rm test-bats + cmd: docker compose run --rm test-bats test-completions: ref: test-bats @@ -78,7 +79,7 @@ commands: description: Run golint-ci depends: [build-lint-image] cmd: - - docker-compose run --rm lint + - docker compose run --rm lint fmt: description: Run sfmt diff --git a/main.go b/main.go index 11c18d45..0571c2f7 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,9 @@ import ( "github.com/lets-cli/lets/executor" "github.com/lets-cli/lets/logging" "github.com/lets-cli/lets/set" + "github.com/lets-cli/lets/upgrade" + "github.com/lets-cli/lets/upgrade/registry" + "github.com/lets-cli/lets/workdir" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -39,13 +42,12 @@ func main() { os.Exit(1) } - rootFlags, err := parseRootFlags(rootCmd, args) + rootFlags, err := parseRootFlags(args) if err != nil { log.Errorf("lets: parse flags error: %s", err) os.Exit(1) } - // TODO add tests if rootFlags.version { if err := cmd.PrintVersionMessage(rootCmd); err != nil { log.Errorf("lets: print version error: %s", err) @@ -77,6 +79,34 @@ func main() { cmd.InitSubCommands(rootCmd, cfg, rootFlags.all, os.Stdout) } + if rootFlags.init { + wd, err := os.Getwd() + if err == nil { + err = workdir.InitLetsFile(wd, version) + } + + if err != nil { + log.Errorf("lets: can not create lets.yaml: %s", err) + os.Exit(1) + } + + os.Exit(0) + } + + if rootFlags.upgrade { + upgrader, err := upgrade.NewBinaryUpgrader(registry.NewGithubRegistry(ctx), version) + if err == nil { + err = upgrader.Upgrade() + } + + if err != nil { + log.Errorf("lets: can not self-upgrade binary: %s", err) + os.Exit(1) + } + + os.Exit(0) + } + showUsage := rootFlags.help || (command.Name() == "help" && len(args) == 0) if showUsage { @@ -120,10 +150,10 @@ func getContext() context.Context { return ctx } -// do not fail on config error in it is help (-h, --help) or completion command +// do not fail on config error in it is help (-h, --help) or --init or completion command func failOnConfigError(root *cobra.Command, current *cobra.Command, rootFlags *flags) bool { rootCommands := set.NewSet("completion", "help") - return (root.Flags().NFlag() == 0 && !rootCommands.Contains(current.Name())) && !rootFlags.help + return (root.Flags().NFlag() == 0 && !rootCommands.Contains(current.Name())) && !rootFlags.help && !rootFlags.init } type flags struct { @@ -132,6 +162,8 @@ type flags struct { help bool version bool all bool + init bool + upgrade bool } // We can not parse --config and --debug flags using cobra.Command.ParseFlags @@ -144,19 +176,21 @@ type flags struct { // cobra will parse all --config flags, but take only latest // // --config=myconfig, and this is wrong. -func parseRootFlags(root *cobra.Command, args []string) (*flags, error) { +func parseRootFlags(args []string) (*flags, error) { f := &flags{} // if first arg is not a flag, then it is subcommand if len(args) > 0 && !strings.HasPrefix(args[0], "-") { return f, nil } - visited := map[string]bool{ - "config": false, - "debug": false, - "help": false, - "version": false, - "all": false, + visited := set.NewSet[string]() + + isFlagVisited := func(name string) bool { + if visited.Contains(name) { + return true + } + visited.Add(name) + return false } idx := 0 @@ -170,8 +204,7 @@ func parseRootFlags(root *cobra.Command, args []string) (*flags, error) { name, value, found := strings.Cut(arg, "=") switch name { case "--config", "-c": - if !visited["config"] { - visited["config"] = true + if !isFlagVisited("config") { if found { if value == "" { return nil, errors.New("--config must be set to value") @@ -184,27 +217,31 @@ func parseRootFlags(root *cobra.Command, args []string) (*flags, error) { } } case "--debug", "-d", "-dd": - if !visited["debug"] { + if !isFlagVisited("debug") { f.debug = 1 if arg == "-dd" { f.debug = 2 } - visited["debug"] = true } case "--help", "-h": - if !visited["help"] { + if !isFlagVisited("help") { f.help = true - visited["help"] = true } case "--version", "-v": - if !visited["version"] { + if !isFlagVisited("version") { f.version = true - visited["version"] = true } case "--all": - if !visited["all"] { + if !isFlagVisited("all") { f.all = true - visited["all"] = true + } + case "--init": + if !isFlagVisited("init") { + f.init = true + } + case "--upgrade": + if !isFlagVisited("upgrade") { + f.upgrade = true } } diff --git a/workdir/workdir.go b/workdir/workdir.go index 3309764a..f09cc44d 100644 --- a/workdir/workdir.go +++ b/workdir/workdir.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/lithammer/dedent" log "github.com/sirupsen/logrus" @@ -12,7 +13,7 @@ import ( const dotLetsDir = ".lets" func getDefaltLetsConfig(version string) string { - return dedent.Dedent(fmt.Sprintf(` + template := dedent.Dedent(fmt.Sprintf(` version: "%s" shell: bash commands: @@ -25,6 +26,7 @@ func getDefaltLetsConfig(version string) string { lets hello Friend cmd: echo Hello, "${LETSOPT_NAME:-world}"! `, version)) + return strings.TrimLeft(template, "\n") } func GetDotLetsDir(workDir string) (string, error) {