github.com/nak3/source-to-image@v1.1.10-0.20180319140719-2ed55639898d/tools/godepchecker/godepchecker.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  )
     9  
    10  // Dependency represents a Golang dependency
    11  type Dependency struct {
    12  	ImportPath string
    13  	Comment    string `json:",omitempty"`
    14  	Rev        string
    15  }
    16  
    17  // Godeps represents a Godeps/Godeps.json file
    18  type Godeps struct {
    19  	ImportPath   string
    20  	GoVersion    string
    21  	GodepVersion string
    22  	Packages     []string
    23  	Deps         map[string]Dependency
    24  }
    25  
    26  // UnmarshalJSON unmarshals the contents of a Godeps/Godeps.json file
    27  func (g *Godeps) UnmarshalJSON(data []byte) error {
    28  	var v struct {
    29  		ImportPath   string
    30  		GoVersion    string
    31  		GodepVersion string
    32  		Packages     []string `json:",omitempty"`
    33  		Deps         []Dependency
    34  	}
    35  
    36  	err := json.Unmarshal(data, &v)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	g.ImportPath = v.ImportPath
    42  	g.GoVersion = v.GoVersion
    43  	g.GodepVersion = v.GodepVersion
    44  	g.Packages = v.Packages
    45  	g.Deps = map[string]Dependency{}
    46  	for _, dep := range v.Deps {
    47  		g.Deps[dep.ImportPath] = dep
    48  	}
    49  	return nil
    50  }
    51  
    52  func readJSON(filename string) (*Godeps, error) {
    53  	f, err := os.Open(filename)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	defer f.Close()
    58  
    59  	godeps := &Godeps{}
    60  	json.NewDecoder(f).Decode(godeps)
    61  	return godeps, nil
    62  }
    63  
    64  func main() {
    65  	s2i, err := readJSON("Godeps/Godeps.json")
    66  	if err != nil {
    67  		fmt.Printf("error: can't read Godeps/Godeps.json: %v\n", err)
    68  		os.Exit(1)
    69  	}
    70  
    71  	origin, err := readJSON("../origin/Godeps/Godeps.json")
    72  	if err != nil {
    73  		fmt.Printf("info: can't read ../origin/Godeps/Godeps.json: %v, not continuing\n", err)
    74  		return
    75  	}
    76  
    77  	code := 0
    78  	for importPath, s2idep := range s2i.Deps {
    79  		origindep, found := origin.Deps[importPath]
    80  		if !found {
    81  			if !strings.HasPrefix(importPath, "github.com/openshift/origin") {
    82  				fmt.Printf("warning: origin missing godep %s\n", importPath)
    83  				code = 1
    84  			}
    85  			continue
    86  		}
    87  
    88  		if origindep.Rev != s2idep.Rev {
    89  			fmt.Printf("warning: differing godep %s: origin %q vs s2i %q\n", importPath, origindep.Rev, s2idep.Rev)
    90  			code = 1
    91  		}
    92  	}
    93  
    94  	os.Exit(code)
    95  }