1// Copyright (c) 2020 Denis Tingajkin 2// 3// SPDX-License-Identifier: Apache-2.0 4// 5// Licensed under the Apache License, Version 2.0 (the "License"); 6// you may not use this file except in compliance with the License. 7// You may obtain a copy of the License at: 8// 9// http://www.apache.org/licenses/LICENSE-2.0 10// 11// Unless required by applicable law or agreed to in writing, software 12// distributed under the License is distributed on an "AS IS" BASIS, 13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14// See the License for the specific language governing permissions and 15// limitations under the License. 16 17package main 18 19import ( 20 "fmt" 21 "go/parser" 22 "go/token" 23 "os" 24 25 goheader "github.com/denis-tingajkin/go-header" 26 "github.com/denis-tingajkin/go-header/version" 27 "github.com/fatih/color" 28 "github.com/sirupsen/logrus" 29) 30 31const configPath = ".go-header.yml" 32 33type issue struct { 34 goheader.Issue 35 filePath string 36} 37 38func main() { 39 paths := os.Args[1:] 40 if len(paths) == 0 { 41 logrus.Fatal("Paths has not passed") 42 } 43 if len(paths) == 1 { 44 if paths[0] == "version" { 45 fmt.Println(version.Value()) 46 return 47 } 48 } 49 c := &goheader.Configuration{} 50 if err := c.Parse(configPath); err != nil { 51 logrus.Fatal(err.Error()) 52 } 53 v, err := c.GetValues() 54 if err != nil { 55 logrus.Fatalf("Can not get values: %v", err.Error()) 56 } 57 t, err := c.GetTemplate() 58 if err != nil { 59 logrus.Fatalf("Can not get template: %v", err.Error()) 60 } 61 a := goheader.New(goheader.WithValues(v), goheader.WithTemplate(t)) 62 s := token.NewFileSet() 63 var issues []*issue 64 for _, p := range paths { 65 f, err := parser.ParseFile(s, p, nil, parser.ParseComments) 66 if err != nil { 67 logrus.Fatalf("File %v can not be parsed due compilation errors %v", p, err.Error()) 68 } 69 i := a.Analyze(&goheader.Target{ 70 Path: p, 71 File: f, 72 }) 73 if i != nil { 74 issues = append(issues, &issue{ 75 Issue: i, 76 filePath: p, 77 }) 78 } 79 } 80 if len(issues) > 0 { 81 red := color.New(color.FgRed).SprintFunc() 82 for _, issue := range issues { 83 fmt.Printf("%v:%v\n%v\n", issue.filePath, issue.Location(), red(issue.Message())) 84 } 85 os.Exit(-1) 86 } 87} 88