From fcf3df5a916e6f3c52784beda7bb1afc66835953 Mon Sep 17 00:00:00 2001 From: kuritka Date: Wed, 17 Mar 2021 16:14:11 +0100 Subject: [PATCH] Exist status 1 i anything is modified and --modified-exit indent ```yaml Makefile: prefix: "#" indent: "***" # generates #***text .yml: prefix: "#" # generates # text Dockerfile*: prefix: "#" indent: "NO_INDENT" # generates #text ``` exit code modified status --- README.md | 12 +++++++++++- cmd/inject.go | 4 +++- cmd/root.go | 9 ++++++++- cmd/runner.go | 4 +++- cmd/version.go | 17 +++++++++++++++++ impl/inject/config.go | 7 ++++--- impl/inject/inject.go | 30 +++++++++++++++++++++++------- impl/inject/opts.go | 1 + 8 files changed, 70 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 6863abb..1234187 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,16 @@ golic inject -c="2021 MyCompany ltd." ## Usage ``` -Usage: +Available Commands: + help Help about any command + inject Injects license + version Print the version number of Golic + +Flags: + -h, --help help for this command + -v, --verbose verbose output + +Usage inject: inject [flags] Flags: @@ -40,6 +49,7 @@ Flags: -d, --dry dry run -h, --help help for inject -l, --licignore string .licignore path (default ".licignore") + -x, --modified-exit If enabled, exits with status 1 when any file is modified. The settings is used by CI -t, --template string license key (default "apache2") diff --git a/cmd/inject.go b/cmd/inject.go index 11116ec..b77f752 100644 --- a/cmd/inject.go +++ b/cmd/inject.go @@ -44,11 +44,13 @@ var injectCmd = &cobra.Command{ os.Exit(0) } i := inject.New(ctx, injectOptions) - Command(i).MustRun() + exitCode = Command(i).MustRun() }, } func init() { + injectCmd.Flags().BoolVarP(&injectOptions.ModifiedExitStatus, "modified-exit", "x", false, + "If enabled, exits with status 1 when any file is modified. The settings is used by CI") injectCmd.Flags().StringVarP(&injectOptions.LicIgnore, "licignore", "l", ".licignore", ".licignore path") injectCmd.Flags().StringVarP(&injectOptions.Template, "template", "t", "apache2", "license key") injectCmd.Flags().StringVarP(&injectOptions.Copyright, "copyright", "c", "2021 MyCompany", diff --git a/cmd/root.go b/cmd/root.go index 6b41e4d..d98134c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -35,6 +35,8 @@ var ctx = context.Background() var logger = log.Log +var exitCode = 0 + var rootCmd = &cobra.Command{ Short: "golic license injector", Long: ``, @@ -46,7 +48,12 @@ var rootCmd = &cobra.Command{ } }, PersistentPostRun: func(cmd *cobra.Command, args []string) { - logger.Info().Msgf("done %3s%s%s", emoji.Rocket,emoji.Rocket,emoji.Rocket) + if exitCode == 0 { + logger.Info().Msgf("done %s%s%s", emoji.Rocket, emoji.Rocket, emoji.Rocket) + } else { + logger.Info().Msgf("done with changes %s", emoji.FaceScreamingInFear) + } + os.Exit(exitCode) }, } diff --git a/cmd/runner.go b/cmd/runner.go index 74f85c5..a038214 100644 --- a/cmd/runner.go +++ b/cmd/runner.go @@ -25,6 +25,7 @@ import ( type Service interface { Run() error String() string + ExitCode() int } type ServiceRunner struct { @@ -38,8 +39,9 @@ func Command(service Service) *ServiceRunner { } //Run service once and panics if service is broken -func (r *ServiceRunner) MustRun() { +func (r *ServiceRunner) MustRun() int { logger.Info().Msgf("%s command %s started",emoji.Tractor, r.service) err := r.service.Run() guard.FailOnError(err, "command %s failed", r.service) + return r.service.ExitCode() } diff --git a/cmd/version.go b/cmd/version.go index 1b7efb7..b4df3fd 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,3 +1,20 @@ +/* +Copyright 2021 Absa Group Limited + +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. + +Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic +*/ package cmd import ( diff --git a/impl/inject/config.go b/impl/inject/config.go index a7cc90e..0e06bc0 100644 --- a/impl/inject/config.go +++ b/impl/inject/config.go @@ -21,9 +21,10 @@ type Config struct { Golic struct{ Licenses map[string]string `yaml:"licenses"` Rules map[string]struct { - Prefix string `yaml:"prefix"` - Suffix string `yaml:"suffix"` - Under []string `yaml:"under"` + Prefix string `yaml:"prefix"` + Suffix string `yaml:"suffix"` + Under []string `yaml:"under"` + Indent string `yaml:"indent"` // if NO_INDENT value, than indent is "" } `yaml:"rules"` } `yaml:"golic"` } diff --git a/impl/inject/inject.go b/impl/inject/inject.go index 33037dc..fea942b 100644 --- a/impl/inject/inject.go +++ b/impl/inject/inject.go @@ -35,10 +35,11 @@ import ( ) type Inject struct { - opts Options - ctx context.Context - ignore gitignore.GitIgnore - cfg *Config + opts Options + ctx context.Context + ignore gitignore.GitIgnore + cfg *Config + modified int } var logger = log.Log @@ -79,6 +80,13 @@ func (i *Inject) String() string { return aurora.BrightCyan("inject").String() } +func (i *Inject) ExitCode() int { + if i.opts.ModifiedExitStatus && i.modified != 0 { + return 1 + } + return 0 +} + func read(f string) (s string, err error) { content, err := ioutil.ReadFile(f) if err != nil { @@ -118,6 +126,7 @@ func (i *Inject) traverse() { if err != nil { logger.Err(err).Msg("") } + i.modified = visited - skipped summary(skipped,visited) } @@ -205,9 +214,16 @@ func getCommentedLicense(config *Config, o Options, file string) (string, error) nil } // `\r\n` -> `\r\n #`, `\n` -> `\n #` - content := strings.ReplaceAll(template,"\n",fmt.Sprintf("\n%s ", config.Golic.Rules[rule].Prefix)) - content = strings.TrimSuffix(content, config.Golic.Rules[rule].Prefix+" ") - content = config.Golic.Rules[rule].Prefix + " " + content + var indent string + switch config.Golic.Rules[rule].Indent { + case "NO_INDENT": indent = "" + case "": indent = " " + default: indent = config.Golic.Rules[rule].Indent + } + + content := strings.ReplaceAll(template,"\n",fmt.Sprintf("\n%s%s", config.Golic.Rules[rule].Prefix,indent)) + content = strings.TrimSuffix(content, config.Golic.Rules[rule].Prefix+indent) + content = config.Golic.Rules[rule].Prefix + indent + content // "# \n" -> "#\n" // "# \r\n" -> "#\r\n"; some environments automatically remove spaces in empty lines. This makes problems in license PR's content = strings.ReplaceAll(content,fmt.Sprintf("%s \n",config.Golic.Rules[rule].Prefix),fmt.Sprintf("%s\n",config.Golic.Rules[rule].Prefix)) content = strings.ReplaceAll(content,fmt.Sprintf("%s \r\n",config.Golic.Rules[rule].Prefix),fmt.Sprintf("%s\r\n",config.Golic.Rules[rule].Prefix)) diff --git a/impl/inject/opts.go b/impl/inject/opts.go index d2b6dac..d21d4f5 100644 --- a/impl/inject/opts.go +++ b/impl/inject/opts.go @@ -24,4 +24,5 @@ type Options struct { ConfigURL string ConfigPath string Template string + ModifiedExitStatus bool }