Source file src/cmd/go/internal/modinfo/info.go

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package modinfo
     6  
     7  import (
     8  	"cmd/go/internal/modfetch/codehost"
     9  	"encoding/json"
    10  	"time"
    11  )
    12  
    13  // Note that these structs are publicly visible (part of go list's API)
    14  // and the fields are documented in the help text in ../list/list.go
    15  
    16  type ModulePublic struct {
    17  	Path       string        `json:",omitempty"` // module path
    18  	Version    string        `json:",omitempty"` // module version
    19  	Query      string        `json:",omitempty"` // version query corresponding to this version
    20  	Versions   []string      `json:",omitempty"` // available module versions
    21  	Replace    *ModulePublic `json:",omitempty"` // replaced by this module
    22  	Time       *time.Time    `json:",omitempty"` // time version was created
    23  	Update     *ModulePublic `json:",omitempty"` // available update (with -u)
    24  	Main       bool          `json:",omitempty"` // is this the main module?
    25  	Indirect   bool          `json:",omitempty"` // module is only indirectly needed by main module
    26  	Dir        string        `json:",omitempty"` // directory holding local copy of files, if any
    27  	GoMod      string        `json:",omitempty"` // path to go.mod file describing module, if any
    28  	GoVersion  string        `json:",omitempty"` // go version used in module
    29  	Retracted  []string      `json:",omitempty"` // retraction information, if any (with -retracted or -u)
    30  	Deprecated string        `json:",omitempty"` // deprecation message, if any (with -u)
    31  	Error      *ModuleError  `json:",omitempty"` // error loading module
    32  
    33  	Origin *codehost.Origin `json:",omitempty"` // provenance of module
    34  	Reuse  bool             `json:",omitempty"` // reuse of old module info is safe
    35  }
    36  
    37  type ModuleError struct {
    38  	Err string // error text
    39  }
    40  
    41  type moduleErrorNoMethods ModuleError
    42  
    43  // UnmarshalJSON accepts both {"Err":"text"} and "text",
    44  // so that the output of go mod download -json can still
    45  // be unmarshalled into a ModulePublic during -reuse processing.
    46  func (e *ModuleError) UnmarshalJSON(data []byte) error {
    47  	if len(data) > 0 && data[0] == '"' {
    48  		return json.Unmarshal(data, &e.Err)
    49  	}
    50  	return json.Unmarshal(data, (*moduleErrorNoMethods)(e))
    51  }
    52  
    53  func (m *ModulePublic) String() string {
    54  	s := m.Path
    55  	versionString := func(mm *ModulePublic) string {
    56  		v := mm.Version
    57  		if len(mm.Retracted) == 0 {
    58  			return v
    59  		}
    60  		return v + " (retracted)"
    61  	}
    62  
    63  	if m.Version != "" {
    64  		s += " " + versionString(m)
    65  		if m.Update != nil {
    66  			s += " [" + versionString(m.Update) + "]"
    67  		}
    68  	}
    69  	if m.Deprecated != "" {
    70  		s += " (deprecated)"
    71  	}
    72  	if m.Replace != nil {
    73  		s += " => " + m.Replace.Path
    74  		if m.Replace.Version != "" {
    75  			s += " " + versionString(m.Replace)
    76  			if m.Replace.Update != nil {
    77  				s += " [" + versionString(m.Replace.Update) + "]"
    78  			}
    79  		}
    80  		if m.Replace.Deprecated != "" {
    81  			s += " (deprecated)"
    82  		}
    83  	}
    84  	return s
    85  }
    86  

View as plain text