Skip to content

Commit

Permalink
Masterconfig goes with binary
Browse files Browse the repository at this point in the history
The main reason is to make current master config be dependent on actual golic version

- masterconfig as embeded resource
- remove `--config-url` or `-u`
  • Loading branch information
kuritka committed Mar 19, 2021
1 parent f44c680 commit 651fba2
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 35 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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."
```

Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions cmd/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand All @@ -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)
}
9 changes: 8 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var logger = log.Log

var exitCode = 0

var masterconfig string

var rootCmd = &cobra.Command{
Short: "golic license injector",
Long: ``,
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 7 additions & 22 deletions impl/inject/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion impl/inject/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type Options struct {
LicIgnore string
Copyright string
Dry bool
ConfigURL string
ConfigPath string
Template string
ModifiedExitStatus bool
MasterConfig string
}
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 651fba2

Please sign in to comment.