modinfo

package module
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2024 License: GPL-3.0 Imports: 13 Imported by: 1

README

modinfo

This module contains:

  • an analyzer that returns module information.
  • methods to find and read go.mod file

Examples

package main

import (
	"fmt"

	"github.com/golangci/modinfo"
	"golang.org/x/tools/go/analysis"
	"golang.org/x/tools/go/analysis/passes/inspect"
)

var Analyzer = &analysis.Analyzer{
    Name: "example",
    Doc:  "Example",
    Run: func(pass *analysis.Pass) (interface{}, error) {
        file, err := modinfo.ReadModuleFileFromPass(pass)
        if err != nil {
          return nil, err
        }

        fmt.Println("go.mod", file)

        // TODO

        return nil, nil
    },
    Requires: []*analysis.Analyzer{
        inspect.Analyzer,
        modinfo.Analyzer,
    },
}
package main

import (
	"fmt"

	"github.com/golangci/modinfo"
	"golang.org/x/tools/go/analysis"
	"golang.org/x/tools/go/analysis/passes/inspect"
)

var Analyzer = &analysis.Analyzer{
    Name: "example",
    Doc:  "Example",
    Run: func(pass *analysis.Pass) (interface{}, error) {
        info, err := modinfo.FindModuleFromPass(pass)
        if err != nil {
          return nil, err
        }

        fmt.Println("Module", info.Dir)

        // TODO

        return nil, nil
    },
    Requires: []*analysis.Analyzer{
        inspect.Analyzer,
        modinfo.Analyzer,
    },
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:       "modinfo",
	Doc:        "Module information",
	URL:        "https://github.com/golangci/modinfo",
	Run:        runOnce,
	ResultType: reflect.TypeOf([]ModInfo(nil)),
}

Functions

func ReadModuleFile

func ReadModuleFile(info ModInfo) (*modfile.File, error)

ReadModuleFile read the `go.mod` file.

func ReadModuleFileFromPass

func ReadModuleFileFromPass(pass *analysis.Pass) (*modfile.File, error)

ReadModuleFileFromPass read the `go.mod` file from the pass result.

Types

type ModInfo

type ModInfo struct {
	Path      string `json:"Path"`
	Dir       string `json:"Dir"`
	GoMod     string `json:"GoMod"`
	GoVersion string `json:"GoVersion"`
	Main      bool   `json:"Main"`
}

func FindModuleFromPass

func FindModuleFromPass(pass *analysis.Pass) (ModInfo, error)

FindModuleFromPass finds the module related to the files of the pass.

func GetModuleInfo

func GetModuleInfo(pass *analysis.Pass) ([]ModInfo, error)

GetModuleInfo gets modules information. Always returns 1 element except for workspace (returns all the modules of the workspace). Based on `go list -m -json` behavior.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL