github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/go/modinfo/info.go (about)

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