Skip to content

Commit

Permalink
GoLic initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kuritka committed Mar 9, 2021
1 parent 05efaf6 commit c1776ba
Show file tree
Hide file tree
Showing 19 changed files with 1,131 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/inspect.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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/kuritka/golic
name: Inspect [linters, tests]

on:
[workflow_dispatch, push]
jobs:
go-inspect:
name: Inspect packages
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# see: https://golangci-lint.run/usage/configuration/#config-file
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.32
- name: go test
run: go test ./...
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Ignore everything
*

# But not these files...
!CODEOWNERS
!LICENSE
!README.md
!/.gitignore
!.licignore
!.golangci
!*.yaml
!Makefile
!*.go
!go.mod
!go.sum

# ...even if they are in subdirectories
!*/

22 changes: 22 additions & 0 deletions .golangci
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
linters:
disable-all: true
enable:
- bodyclose
- deadcode
- errcheck
- gocritic
- gocyclo
- goimports
- golint
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- lll
run:
deadline: 3m
linters-settings:
lll:
line-length: 150
16 changes: 16 additions & 0 deletions .licignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# GoLic, for more details see: https://github.com/AbsaOSS/golic
# Ignore everything
*

# But not these files...

!*.yaml
!*.go
!Makefile

# ...even if they are in subdirectories
!*/

# except these in subdirs
/**/.gitignore
/.idea/**/*.xml
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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/kuritka/golic
.PHONY: lint
lint:
golangci-lint run

.PHONY: license
license:
GO111MODULE=on go get github.com/AbsaOSS/[email protected]
$(GOBIN)/golic inject -c="2021 ABSA Group Limited" -l=.licignore
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# golic
license generator
```
golic inject -c="2021 SuperPower Group Limited" -l=.licignore --dry
```
![Screenshot 2021-03-08 at 11 42 52](https://user-images.githubusercontent.com/7195836/110310942-6d2f3680-8003-11eb-9540-b2e21b4f2b87.png)


## Running from commandline

create `.licignore`
```shell
# Ignore everything
*

# But not these files...
!Makefile
!*.go

# ...even if they are in subdirectories
!*/
````
And run **GOLIC**
```shell
GO111MODULE=on go get github.com/AbsaOSS/[email protected]
$(GOBIN)/golic inject -c="2021 MyCompany Group Limited" -l=.licignore
```


## Usage
```shell
Usage:
inject [flags]
Flags:
-u, --config-url string config URL (default "https://raw.githubusercontent.com/AbsaOSS/golic/main/config.yaml")
-c, --copyright string company initials entered into license (default "2021 MyCompany")
-d, --dry dry run
-h, --help help for inject
-l, --licignore string .licignore path
-t, --template string license key (default "apache2")
Global Flags:
-v, --verbose verbose output
```

## Configuration
For more details see: [default configuration](https://raw.githubusercontent.com/AbsaOSS/golic/main/config.yaml).
Use `-u` flag to run against custom configuration or create PR.

59 changes: 59 additions & 0 deletions cmd/inject.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
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/kuritka/golic
*/
package cmd

import (
"net/url"
"os"

"github.com/AbsaOSS/golic/impl/inject"
"github.com/spf13/cobra"
)

var injectOptions inject.Options

var injectCmd = &cobra.Command{
Use: "inject",
Short: "",
Long: ``,

Run: func(cmd *cobra.Command, args []string) {
if _, err := os.Stat(injectOptions.LicIgnore); os.IsNotExist(err) {
logger.Error().Msgf("invalid license path '%s'",injectOptions.LicIgnore)
_ = cmd.Help()
os.Exit(0)
}
if _,err := url.Parse(injectOptions.ConfigURL); err != nil {
logger.Error().Msgf("invalid config.yaml url '%s'",injectOptions.ConfigURL)
_ = cmd.Help()
os.Exit(0)
}
i := inject.New(ctx, injectOptions)
Command(i).MustRun()
},
}

func init() {
injectCmd.Flags().StringVarP(&injectOptions.LicIgnore, "licignore", "l", "", ".licignore path")
injectCmd.Flags().StringVarP(&injectOptions.Template, "template", "t", "apache2", "license key")
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/config.yaml", "config URL")
rootCmd.AddCommand(injectCmd)
}
62 changes: 62 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
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/kuritka/golic
*/
//package provides cobra commands
package cmd

import (
"context"
"fmt"
"github.com/enescakir/emoji"
"os"

"github.com/AbsaOSS/golic/utils/log"

"github.com/spf13/cobra"
)

var Verbose bool

var ctx = context.Background()

var logger = log.Log

var rootCmd = &cobra.Command{
Short: "golic license injector",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
logger.Error().Msg("no parameters included")
_ = cmd.Help()
os.Exit(0)
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
logger.Info().Msgf("done %3s%s%s", emoji.Rocket,emoji.Rocket,emoji.Rocket)
},
}

func init() {
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
}

func Execute() {
fmt.Println()
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
45 changes: 45 additions & 0 deletions cmd/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
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/kuritka/golic
*/
package cmd

import (
"github.com/AbsaOSS/golic/utils/guard"
"github.com/enescakir/emoji"
)

type Service interface {
Run() error
String() string
}

type ServiceRunner struct {
service Service
}

func Command(service Service) *ServiceRunner {
return &ServiceRunner{
service,
}
}

//Run service once and panics if service is broken
func (r *ServiceRunner) MustRun() {
logger.Info().Msgf("%s command %s started",emoji.Tractor, r.service)
err := r.service.Run()
guard.FailOnError(err, "command %s failed", r.service)
}
Loading

0 comments on commit c1776ba

Please sign in to comment.