From 737d503f375b491182cbdbc45d4577e307cd1aca Mon Sep 17 00:00:00 2001 From: Vladislav Sidorin Date: Wed, 29 Jun 2022 11:48:52 +0300 Subject: [PATCH] v1.2.0 --- README.md | 36 ++++++++++++++++++++++++------------ app/ModAliases.go | 35 ++++++++++++++++++++++++++++++++--- cmd/root.go | 4 ++-- 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 26c3c60..990a4a0 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,32 @@ Require go 1.16.0 or later, to install run: `-s` - soft launch `--no-color` - disable colored output + +## Rewrite Aliases +You can export env `GOST_ALIASES` to rewrite build-in aliases +and export `GOST_ADD_ALIASES` to add and replace build-in aliases. For example: `GOST_ALIASES="/home/user/gost.aliases.yaml" ./gost mod -s webserver` + +You can rewrite default aliases and bundles of aliases with your own - gost try to read +file `gost.aliases.yaml` in a current directory to rewrite build-in aliases. + +Also two flags present: + +`--aliases` - file to full rewrite build-in aliases (include loaded from `gost.aliases.yaml`) + +`--aliases-add` - file to add and replace build-in aliases + +Reading and replacing order: +1. From env +2. From file in current directory +3. From flags + +## Multi-threads (experimental) +You can run `ghost mod` with` --threads = N` for multithreading, but then the order in which +the `go get` is executed is not guaranteed, there may be side effects. + ## Supported commands ### mod -Do `go get -u` commands with use of aliases and bundles of aliases. +Do `go get -u` commands with use of aliases and bundles of aliases. Usage: `gost mod module1 module2 ...` @@ -61,17 +84,6 @@ Use soft Launch /usr/local/go/bin/go get -u gopkg.in/Graylog2/go-gelf.v1 /usr/local/go/bin/go mod download ``` -However, you can rewrite default aliases and bundles of aliases with your own - gost try to read -file `gost.aliases.yaml` in a current directory to rewrite build-in aliases. Also two flags present: - -`--aliases` - file to full rewrite build-in aliases (include loaded from `gost.aliases.yaml`) - -`--aliases-add` - file to add and replace build-in aliases - -Or you can export env `GOST_ALIASES` to rewrite build-in aliases (equivalent to `--aliases`) - -You can run `ghost mod` with` --threads = N` for multithreading, but then the order in which -the `go get` is executed is not guaranteed, there may be side effects. ### start Starts new project in dir `--package-name` in current dir, do `mod init` and all, what do `gost mod` command. diff --git a/app/ModAliases.go b/app/ModAliases.go index 9aceb2c..a02a882 100644 --- a/app/ModAliases.go +++ b/app/ModAliases.go @@ -17,6 +17,8 @@ const ( ) const defaultFileName = "gost.aliases.yaml" +const envAliasesName = "GOST_ALIASES" +const envAliasesAddName = "GOST_ADD_ALIASES" //go:embed gost.aliases.yaml var defAliases []byte @@ -50,9 +52,10 @@ type ModAliases struct { } func init() { + loadAliasesFromEnv() f, err := os.ReadFile(defaultFileName) if err == nil { - fmt.Println("Load aliases from " + defaultFileName) + fmt.Println("Rewrite aliases from " + defaultFileName) defAliases = f } ma, err := getAliasesByBytes(defAliases) @@ -66,6 +69,32 @@ func init() { fmt.Println("[ERROR]: " + err.Error()) os.Exit(1) } + loadAddAliasesFromEnv() +} + +func loadAliasesFromEnv() { + if fn, ok := os.LookupEnv(envAliasesName); ok { + f, err := os.ReadFile(fn) + if err == nil { + fmt.Println("Rewrite aliases by env " + envAliasesName + " from file " + fn) + defAliases = f + } + } +} + +func loadAddAliasesFromEnv() { + if fn, ok := os.LookupEnv(envAliasesAddName); ok { + ma, err := getAliasesByFile(fn, false) + if err != nil { + return + } + newMa, err := defModAliases.Glue(ma) + if err != nil { + return + } + defModAliases = newMa + fmt.Println("Add aliases from env " + envAliasesAddName + " from file " + fn) + } } func getAliasesByBytes(str []byte) (ModAliases, error) { @@ -79,7 +108,7 @@ func GetAliases() ModAliases { return defModAliases } -//GetDefaultAliasesH - return string of default aliases and bundles set for description of commands +//GetDefaultAliasesHelp - return string of default aliases and bundles set for description of commands func GetDefaultAliasesHelp() string { return string(defAliases) } @@ -111,7 +140,7 @@ func LoadAliasesFromFlags(outIo, errIo io.Writer, aliasesFile, addAliasesFile st return false } defModAliases = ma - _, _ = outIo.Write([]byte("Load aliases from " + aliasesFile)) + _, _ = outIo.Write([]byte("Load aliases from " + aliasesFile + "\n")) } if addAliasesFile != "" { ma, err := getAliasesByFile(addAliasesFile, false) diff --git a/cmd/root.go b/cmd/root.go index eb1ecc8..dad20df 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,6 +7,7 @@ import ( ) const ( + Version = "v1.2.0" //no color flag name NoColor = "no-color" //verbose flag full name @@ -27,7 +28,7 @@ var cfg *envviper.EnvViper var rootCmd = &cobra.Command{ Use: "gost", Short: "Go Start project - helper for start go projects", - Long: "Go Start (gost) - utility for help you to start go project without type some `go get ...` commands." + + Long: "Go Start (gost) " + Version + " - utility for help you to start go project without type some `go get ...` commands." + "\nCommand has some aliases for popular and useful go modules, and has some aliases for bundles of modules", PersistentPreRun: func(cmd *cobra.Command, args []string) { if cfg.GetBool("no-color") { @@ -58,7 +59,6 @@ func init() { // initConfig reads in config file and ENV variables if set. func initConfig() { cfg = envviper.NewEnvViper() - cfg.SetEnvParamsSimple("GOST") _ = cfg.BindPFlag("v", rootCmd.PersistentFlags().Lookup(Verbose)) _ = cfg.BindPFlag("s", rootCmd.PersistentFlags().Lookup(SoftLaunch)) _ = cfg.BindPFlag(NoColor, rootCmd.PersistentFlags().Lookup(NoColor))