Skip to content

Commit

Permalink
Exist status 1 i anything is modified and --modified-exit
Browse files Browse the repository at this point in the history
indent
```yaml
    Makefile:
      prefix: "#"
      indent: "***" # generates #***text
    .yml:
      prefix: "#"  # generates # text
    Dockerfile*:
      prefix: "#"
      indent: "NO_INDENT" # generates #text
```

exit code

modified status
  • Loading branch information
kuritka committed Mar 17, 2021
1 parent 5d7856e commit 3e3de25
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 13 deletions.
4 changes: 3 additions & 1 deletion cmd/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 8 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var ctx = context.Background()

var logger = log.Log

var exitCode = 0

var rootCmd = &cobra.Command{
Short: "golic license injector",
Long: ``,
Expand All @@ -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)
},
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
type Service interface {
Run() error
String() string
ExitCode() int
}

type ServiceRunner struct {
Expand All @@ -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()
}
17 changes: 17 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
7 changes: 4 additions & 3 deletions impl/inject/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
30 changes: 23 additions & 7 deletions impl/inject/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -118,6 +126,7 @@ func (i *Inject) traverse() {
if err != nil {
logger.Err(err).Msg("")
}
i.modified = visited - skipped
summary(skipped,visited)
}

Expand Down Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions impl/inject/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ type Options struct {
ConfigURL string
ConfigPath string
Template string
ModifiedExitStatus bool
}

0 comments on commit 3e3de25

Please sign in to comment.