From 651fba2b7b29234fb190520d896a884e24a2df48 Mon Sep 17 00:00:00 2001 From: kuritka Date: Fri, 19 Mar 2021 13:59:42 +0100 Subject: [PATCH] Masterconfig goes with binary The main reason is to make current master config be dependent on actual golic version - masterconfig as embeded resource - remove `--config-url` or `-u` --- README.md | 6 +++--- cmd/inject.go | 10 +++++----- cmd/root.go | 9 ++++++++- cmd/version.go | 2 +- impl/inject/inject.go | 29 +++++++---------------------- impl/inject/opts.go | 2 +- main.go | 12 ++++++++++-- 7 files changed, 35 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 5943d14..1b65fb9 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ create `.licignore` in project root Install and run **GOLIC** ```shell # GO 1.16 -go install github.com/AbsaOSS/golic@v0.4.7 +go install github.com/AbsaOSS/golic@v0.4.8 golic inject -c="2021 MyCompany ltd." ``` @@ -58,8 +58,8 @@ Global Flags: ``` ## Configuration -Golic uses [master configuration](https://raw.githubusercontent.com/AbsaOSS/golic/main/.golic.yaml) by default. -The master configuration is accessible from the URL, and if you need to change it, use the `-u` flag or create PR. +Golic uses embeded [master configuration](https://raw.githubusercontent.com/AbsaOSS/golic/main/.golic.yaml) by default. +The master configuration is compiled within binary, and if you need to change it, create PR. However, it is much better to create a local configuration that overrides the master configuration settings. All you have to do is create a `.golic.yaml` file in the project root, or use the` -p` flag. Example below overrides master configuration by custom licenses diff --git a/cmd/inject.go b/cmd/inject.go index e7c2d86..9cb8a6f 100644 --- a/cmd/inject.go +++ b/cmd/inject.go @@ -18,11 +18,11 @@ Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic package cmd import ( - "github.com/enescakir/emoji" - "net/url" "os" "github.com/AbsaOSS/golic/impl/inject" + + "github.com/enescakir/emoji" "github.com/spf13/cobra" ) @@ -39,11 +39,12 @@ var injectCmd = &cobra.Command{ _ = cmd.Help() os.Exit(1) } - if _,err := url.Parse(injectOptions.ConfigURL); err != nil { - logger.Error().Msgf("invalid config.yaml url '%s'",injectOptions.ConfigURL) + if masterconfig == "" { + logger.Error().Msgf("invalid master config. ") _ = cmd.Help() os.Exit(1) } + injectOptions.MasterConfig = masterconfig i := inject.New(ctx, injectOptions) exitCode = Command(i).MustRun() if exitCode == 0 { @@ -62,7 +63,6 @@ func init() { injectCmd.Flags().StringVarP(&injectOptions.Copyright, "copyright", "c", "2021 MyCompany", "company initials entered into license") injectCmd.Flags().BoolVarP(&injectOptions.Dry, "dry", "d", false, "dry run") - injectCmd.Flags().StringVarP(&injectOptions.ConfigURL, "config-url", "u", "https://raw.githubusercontent.com/AbsaOSS/golic/main/.golic.yaml", "default config URL") injectCmd.Flags().StringVarP(&injectOptions.ConfigPath, "config-path","p", ".golic.yaml","path to the local configuration overriding config-url" ) rootCmd.AddCommand(injectCmd) } diff --git a/cmd/root.go b/cmd/root.go index 56bdec8..e79f88a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -36,6 +36,8 @@ var logger = log.Log var exitCode = 0 +var masterconfig string + var rootCmd = &cobra.Command{ Short: "golic license injector", Long: ``, @@ -55,8 +57,13 @@ func init() { rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output") } -func Execute() { +func Execute(golic string) { fmt.Println() + if Verbose { + fmt.Println(golic) + fmt.Println() + } + masterconfig = golic if err := rootCmd.Execute(); err != nil { os.Exit(1) } diff --git a/cmd/version.go b/cmd/version.go index b4518f5..47f52b1 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -22,7 +22,7 @@ import ( "github.com/spf13/cobra" ) -const version = "v0.4.7" +const version = "v0.4.8" var versionCmd = &cobra.Command{ Use: "version", diff --git a/impl/inject/inject.go b/impl/inject/inject.go index 48af2f1..bf5bed0 100644 --- a/impl/inject/inject.go +++ b/impl/inject/inject.go @@ -22,7 +22,6 @@ import ( "fmt" "gopkg.in/yaml.v3" "io/ioutil" - "net/http" "os" "path/filepath" "strings" @@ -57,14 +56,14 @@ func (i *Inject) Run() (err error) { if err != nil { return err } - logger.Info().Msgf("%s reading %s",emoji.OpenBook, aurora.BrightCyan(i.opts.ConfigURL)) + logger.Info().Msgf("%s reading %s; use --verbose to see details",emoji.OpenBook, aurora.BrightCyan("master config")) if i.cfg, err = i.readCommonConfig(); err != nil { return } if _, err = os.Stat(i.opts.ConfigPath); !os.IsNotExist(err) { logger.Info().Msgf("%s reading %s",emoji.OpenBook, aurora.BrightCyan(i.opts.ConfigPath)) logger.Info().Msgf("%s overriding %s with %s", - emoji.ConstructionWorker, aurora.BrightCyan(i.opts.ConfigURL),aurora.BrightCyan(i.opts.ConfigPath)) + emoji.ConstructionWorker, aurora.BrightCyan("master config"),aurora.BrightCyan(i.opts.ConfigPath)) if i.cfg, err = i.readLocalConfig(); err != nil { return } @@ -110,7 +109,7 @@ func (i *Inject) traverse() { cp = aurora.Magenta(path) skipped++ } - emoji.Printf(" %s %s %s \n",emoji.Minus, cp, aurora.BrightMagenta(symbol)) + _,_ = emoji.Printf(" %s %s %s \n",emoji.Minus, cp, aurora.BrightMagenta(symbol)) } return } @@ -201,11 +200,11 @@ func getCommentedLicense(config *Config, o Options, file string) (string, error) var template string var rule string if template, ok = config.Golic.Licenses[o.Template]; !ok { - return "",fmt.Errorf("no license found for %s, check configuration (%s)",o.Template,o.ConfigURL) + return "",fmt.Errorf("no license found for %s, check configuration (.golic.yaml)",o.Template) } //if _, ok = config.Golic.Rules[rule]; !ok { if rule, ok = matchRule(config, file); !ok { - return "",fmt.Errorf("no rule found for %s, check configuration (%s)", rule,o.ConfigURL) + return "",fmt.Errorf("no rule found for %s, check configuration (.golic.yaml)", rule) } template = strings.ReplaceAll(template,"{{copyright}}", o.Copyright) if config.IsWrapped(rule) { @@ -272,21 +271,7 @@ func (i *Inject) readLocalConfig() (*Config, error) { } func (i *Inject) readCommonConfig() (c *Config, err error) { - var client http.Client - var resp *http.Response - var b []byte - c = new(Config) - resp, err = client.Get(i.opts.ConfigURL) - if err != nil { - return - } - if resp.StatusCode >= http.StatusBadRequest { - return nil, fmt.Errorf("%s: %s returns %d", http.MethodGet, i.opts.ConfigURL, resp.StatusCode) - } - defer resp.Body.Close() - if b, err = ioutil.ReadAll(resp.Body); err != nil { - return - } - err = yaml.Unmarshal(b, c) + c = &Config{} + err = yaml.Unmarshal([]byte(i.opts.MasterConfig), c) return } \ No newline at end of file diff --git a/impl/inject/opts.go b/impl/inject/opts.go index d21d4f5..d3d1a84 100644 --- a/impl/inject/opts.go +++ b/impl/inject/opts.go @@ -21,8 +21,8 @@ type Options struct { LicIgnore string Copyright string Dry bool - ConfigURL string ConfigPath string Template string ModifiedExitStatus bool + MasterConfig string } diff --git a/main.go b/main.go index 1341fa8..6f80f6e 100644 --- a/main.go +++ b/main.go @@ -17,8 +17,16 @@ Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic */ package main -import "github.com/AbsaOSS/golic/cmd" +import ( + _ "embed" + "fmt" + "github.com/AbsaOSS/golic/cmd" +) + +//go:embed .golic.yaml +var golic string func main() { - cmd.Execute() + fmt.Println() + cmd.Execute(golic) }