nexus

package module
v0.0.0-...-12d6510 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2019 License: MIT Imports: 13 Imported by: 2

README

A Go library to pull data from a Sonatype Nexus instance.

TravisCI GoDoc

How?

go get should see you through:

go get sbrubbles.org/go/nexus

And therefore import:

package main

import (
  "fmt"
  "sbrubbles.org/go/nexus"
  "sbrubbles.org/go/nexus/credentials"
  "sbrubbles.org/go/nexus/search"
  "reflect"
)

func main() {
  n := nexus.New("http://nexus.somewhere.com", credentials.BasicAuth("username", "password"))

  artifacts, err := n.Artifacts(
    search.InRepository{
      "shamalamadingdong",
      search.ByKeyword("org.sbrubbles*")})

  if err != nil {
    fmt.Printf("%v: %v", reflect.TypeOf(err), err)
  }

  for _, a := range artifacts {
    fmt.Println(a)
  }
}

Why?

Nexus has a large REST API, but some information isn't readily available, requiring several API calls and some mashing up to produce.

And it was a good excuse to try Go out :)

License

MIT License. See LICENSE for the gory details.

Documentation

Overview

Package nexus is a client for Go code to pull data from a Sonatype Nexus instance.

Nexus provides a REST API, although some information may require several calls to collate all the data. So this client provides some methods to abstract away the necessary plumbing.

Example
package main

import (
	"sbrubbles.org/go/nexus"
	"sbrubbles.org/go/nexus/credentials"
	"sbrubbles.org/go/nexus/search"

	"fmt"
	"reflect"
)

func main() {
	n := nexus.New("https://maven.java.net", credentials.None)

	// obtaining all repositories in Nexus
	repositories, err := n.Repositories()
	if err != nil {
		fmt.Printf("%v: %v", reflect.TypeOf(err), err)
		return
	}

	// printing out all artifacts which are in a hosted repository, and have
	// both 'javax.enterprise' in their groupID and a 'sources' classifier.
	for _, repo := range repositories {
		if repo.Type != "hosted" {
			continue
		}

		artifacts, err := n.Artifacts(
			search.InRepository{
				RepositoryID: repo.ID,
				Criteria: search.ByCoordinates{
					GroupID:    "javax.enterprise*",
					Classifier: "sources"}})

		if err != nil {
			fmt.Printf("%v: %v", reflect.TypeOf(err), err)
			return
		}

		for _, a := range artifacts {
			fmt.Println(a)
		}
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Artifact

type Artifact struct {
	GroupID      string // e.g. org.springframework
	ArtifactID   string // e.g. spring-core
	Version      string // e.g. 4.1.3.RELEASE
	Classifier   string // e.g. sources, javadoc, <the empty string>...
	Extension    string // e.g. jar
	RepositoryID string // e.g. releases
}

Artifact is a Maven coordinate to a single artifact, plus the repository where it came from.

func (Artifact) String

func (a Artifact) String() string

String implements the fmt.Stringer interface, as per Maven docs (http://maven.apache.org/pom.html#Maven_Coordinates).

type ArtifactInfo

type ArtifactInfo struct {
	*Artifact

	Uploader    string
	Uploaded    time.Time
	LastChanged time.Time
	Sha1        string
	Size        util.ByteSize
	MimeType    string
	URL         string
}

ArtifactInfo holds extra information about an artifact. There are no constructors; use nexus.InfoOf to fetch and build instances.

func (ArtifactInfo) String

func (info ArtifactInfo) String() string

String implements the fmt.Stringer interface.

func (*ArtifactInfo) UnmarshalXML

func (info *ArtifactInfo) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements the xml.Unmarshaler interface. The ArtifactInfo should already have a valid *Artifact pointer; the rest of the information will be extracted from the payload.

type Client

type Client interface {
	// Returns all artifacts in this Nexus which satisfy the given criteria.
	// Nil is the same as search.All. If no criteria are given
	// (e.g. search.All), it does a full search in all repositories.
	Artifacts(criteria search.Criteria) ([]*Artifact, error)

	// Returns all repositories in this Nexus.
	Repositories() ([]*Repository, error)

	// Returns extra information about the given artifact.
	InfoOf(artifact *Artifact) (*ArtifactInfo, error)
}

Client accesses a Nexus instance. The default Client should work for the newest Nexus versions. Older Nexus versions may need or benefit from a specific client.

func New

New creates a new Nexus client, using the default Client implementation.

type Error

type Error struct {
	URL        string // e.g. http://somewhere.com
	StatusCode int    // e.g. 400
	Status     string // e.g. 400 Bad response
	Message    string // e.g. Error (400 Bad response) from http://somewhere.com
}

Error is returned when there's an error on an attempt to access Nexus.

func (Error) Error

func (err Error) Error() string

Error implements the error interface.

type Nexus2x

type Nexus2x struct {
	URL         string                  // e.g. http://somewhere.com:8080/nexus
	Credentials credentials.Credentials // e.g. credentials.BasicAuth("u", "p")
	HTTPClient  *http.Client            // the network client
}

Nexus2x represents a Nexus v2.x instance. It's the default Client implementation.

func (Nexus2x) Artifacts

func (nexus Nexus2x) Artifacts(criteria search.Criteria) ([]*Artifact, error)

Artifacts implements the Client interface, returning all artifacts in this Nexus which satisfy the given criteria. Nil is the same as search.All. If no criteria are given (e.g. search.All), it does a full search in all repositories.

Generally you don't want that, especially if you have proxy repositories; Maven Central (which many people will proxy) has, at the time of this comment, over 800,000 artifacts (!), which in this implementation will be *all* loaded into memory (!!). But, if you insist...

Example
package main

import (
	"sbrubbles.org/go/nexus"
	"sbrubbles.org/go/nexus/credentials"
	"sbrubbles.org/go/nexus/search"
)

func main() {
	n := nexus.New("http://maven.java.net", credentials.None)

	// using a simple search
	n.Artifacts(search.ByClassname("javax.servlet.Servlet"))

	// using a composite search
	n.Artifacts(
		search.InRepository{
			RepositoryID: "releases",
			Criteria:     search.ByKeyword("javax.enterprise")})

	// searching for every artifact in Nexus (WARNING: this can take a LOOONG
	// time - and memory!)
	n.Artifacts(search.All)
}
Output:

func (Nexus2x) InfoOf

func (nexus Nexus2x) InfoOf(artifact *Artifact) (*ArtifactInfo, error)

InfoOf implements the Client interface, fetching extra information about the given artifact.

func (Nexus2x) Repositories

func (nexus Nexus2x) Repositories() ([]*Repository, error)

Repositories implements the Client interface, returning all repositories in this Nexus.

type Repository

type Repository struct {
	ID        string // e.g. releases
	Name      string // e.g. Releases
	Type      string // e.g. hosted, proxy, virtual...
	Format    string // e.g. maven2, maven1...
	Policy    string // e.g. RELEASE, SNAPSHOT
	RemoteURI string // e.g. http://repo1.maven.org/maven2/
}

Repository is a non-group Nexus repository. Nexus actually provides a bit more data, but this should be enough for most uses. Groups aren't considered repositories by Nexus' API; there's a separate call for them.

func (Repository) String

func (repo Repository) String() string

String implements the fmt.Stringer interface.

Directories

Path Synopsis
Package credentials provides credentials to an http.Request.
Package credentials provides credentials to an http.Request.
Package search provides a mini-DSL for nexus.Client.Artifacts().
Package search provides a mini-DSL for nexus.Client.Artifacts().
Package util stores useful helper code.
Package util stores useful helper code.

Jump to

Keyboard shortcuts

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