gomod

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: BSD-3-Clause Imports: 3 Imported by: 1

README

GoMod

Package gomod provides a parser for go.mod files according to the official specification. Since this package heavily relies on the go command, a go installation is required for this module to work.

The documentation is available here: https://pkg.tk-software.de/gomod

package main

import (
	"fmt"

	"pkg.tk-software.de/gomod"
)

func main() {
	mod, err := gomod.NewFromDir("./testdata/default/")
	if err != nil {
		return
	}

	fmt.Println("Module path:", mod.Module.Path)
	fmt.Println("Go version:", mod.Go)
	fmt.Println()

	fmt.Println("Required:")
	for _, r := range mod.Require {
		if !r.Indirect {
			fmt.Printf("%s@%s\n", r.Path, r.Version)
		}
	}
	fmt.Println()

	fmt.Println("Required (indirect):")
	for _, r := range mod.Require {
		if r.Indirect {
			fmt.Printf("%s@%s\n", r.Path, r.Version)
		}
	}
	fmt.Println()

	fmt.Println("Replaced:")
	for _, r := range mod.Replace {
		if r.Old.Version != "" {
			fmt.Printf("%s@%s -> %s@%s\n", r.Old.Path, r.Old.Version, r.New.Path, r.New.Version)
		} else {
			fmt.Printf("%s -> %s\n", r.Old.Path, r.New.Path)
		}
	}
	fmt.Println()

	fmt.Println("Excluded:")
	for _, e := range mod.Exclude {
		fmt.Printf("%s@%s\n", e.Path, e.Version)
	}
	fmt.Println()

	fmt.Println("Retracted:")
	for _, r := range mod.Retract {
		if r.Low == r.High {
			fmt.Printf("%s: %s\n", r.Low, r.Rationale)
		} else {
			fmt.Printf("[%s, %s]: %s\n", r.Low, r.High, r.Rationale)
		}
	}
	fmt.Println()
}

Contributing

see CONTRIBUTING.md

Donating

Thanks for your interest in this project. You can show your appreciation and support further development by donating.

License

GoMod © 2023 Tobias Koch. Released under a BSD-style license.

Documentation

Overview

Package gomod provides a parser for go.mod files according to the official specification. Since this package heavily relies on the `go` command, a go installation is required for this module to work.

mod, err := gomod.NewFromDir("./testdata/default/")
if err != nil {
	return
}

fmt.Println("Module path:", mod.Module.Path)
fmt.Println("Go version:", mod.Go)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Exclude

type Exclude struct {

	// The path of the module to be excluded.
	Path string `json:"Path"`

	// The version of the module to be excluded.
	Version string `json:"Version"`
}

An exclude directive prevents a module version from being loaded by the go command. See https://go.dev/ref/mod#go-mod-file-exclude

type GoMod

type GoMod struct {

	// A module directive defines the main module’s path.
	Module Module `json:"Module"`

	// A go directive indicates that a module was written assuming the semantics of a given version of Go.
	Go string `json:"Go"`

	// A toolchain directive declares a suggested Go toolchain to use with a module.
	Toolchain string `json:"Toolchain"`

	// A require directive declares a minimum required version of a given module dependency.
	Require []Require `json:"Require"`

	// An exclude directive prevents a module version from being loaded by the go command.
	Exclude []Exclude `json:"Exclude"`

	// A replace directive replaces the contents of a specific version of a module, or all versions of a module, with contents found elsewhere.
	Replace []Replace `json:"Replace"`

	// A retract directive indicates that a version or range of versions of the module should not be depended upon.
	Retract []Rectract `json:"Retract"`
}

A module is defined by a file named go.mod in its root directory. See https://go.dev/ref/mod#go-mod-file

func NewFromDir

func NewFromDir(d string) (*GoMod, error)

NewFromDir parses and returns the go.mod file located in the directory d.

type Module

type Module struct {

	// The main module’s path.
	Path string `json:"Path"`
}

A module directive defines the main module’s path. See https://go.dev/ref/mod#go-mod-file-module

type New

type New struct {

	// The path of the module.
	Path string `json:"Path"`

	// If the path is not a local path, it must be a valid module path. In this case, a version is required.
	Version string `json:"Version"`
}

The module replacement.

type Old

type Old struct {

	// The path of the module.
	Path string `json:"Path"`

	// If a version is present only that specific version of the module is replaced.
	Version string `json:"Version"`
}

The module to be replaced.

type Rectract

type Rectract struct {

	// A directive may be written with either a single version or with a closed interval of versions with an upper and lower bound.
	Low string `json:"Low"`

	// A directive may be written with either a single version or with a closed interval of versions with an upper and lower bound.
	High string `json:"High"`

	// A directive should have a comment explaining the rationale for the retraction.
	Rationale string `json:"Rationale"`
}

A retract directive indicates that a version or range of versions of the module should not be depended upon. See https://go.dev/ref/mod#go-mod-file-retract

type Replace

type Replace struct {

	// The module to be replaced.
	Old Old `json:"Old"`

	// The module replacement.
	New New `json:"New"`
}

A replace directive replaces the contents of a specific version of a module, or all versions of a module, with contents found elsewhere. See https://go.dev/ref/mod#go-mod-file-replace

type Require

type Require struct {

	// The path of the module dependency.
	Path string `json:"Path"`

	// The minimum required version of the module dependency.
	Version string `json:"Version"`

	// Indicates that no package from the required module is directly imported.
	Indirect bool `json:"Indirect"`
}

A require directive declares a minimum required version of a given module dependency. See https://go.dev/ref/mod#go-mod-file-require

Jump to

Keyboard shortcuts

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