github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/update/releasenotes/releasenotes.go (about)

     1  package releasenotes
     2  
     3  import (
     4  	"fmt"
     5  
     6  	version "github.com/hashicorp/go-version"
     7  	"github.com/henvic/wedeploycli/defaults"
     8  	"github.com/henvic/wedeploycli/update/releasenotes"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var (
    13  	from      string
    14  	exclusive bool
    15  
    16  	all bool
    17  )
    18  
    19  // Cmd for "lcp update release-notes"
    20  var Cmd = &cobra.Command{
    21  	Use:   "release-notes",
    22  	Args:  cobra.NoArgs,
    23  	RunE:  updateRun,
    24  	Short: "Update Release Notes",
    25  }
    26  
    27  func updateRun(cmd *cobra.Command, args []string) error {
    28  	notes := releasenotes.ReleaseNotes
    29  
    30  	if all {
    31  		printReleaseNotes(notes)
    32  		return nil
    33  	}
    34  
    35  	switch from {
    36  	case "", "master":
    37  		notes = filterCurrentUpdate(notes)
    38  	default:
    39  		notes = filterUpdates(from, notes)
    40  	}
    41  
    42  	if len(notes) == 0 {
    43  		return checkNoUpdateNotices(from)
    44  	}
    45  
    46  	printReleaseNotes(notes)
    47  
    48  	return nil
    49  }
    50  
    51  func checkNoUpdateNotices(from string) error {
    52  	fromV, fromErr := version.NewVersion(from)
    53  	currentV, currentErr := version.NewVersion(defaults.Version)
    54  
    55  	if from != "" && fromErr == nil && currentErr == nil && fromV.GreaterThan(currentV) {
    56  		return fmt.Errorf("version on --from is %v, which is greater than the current version of this program: %v", fromV, currentV)
    57  	}
    58  
    59  	fmt.Println("No release notes found.")
    60  	return nil
    61  }
    62  
    63  func filterCurrentUpdate(updates []releasenotes.ReleaseNote) []releasenotes.ReleaseNote {
    64  	if defaults.Version == "master" {
    65  		return []releasenotes.ReleaseNote{
    66  			releasenotes.ReleaseNote{
    67  				Version:     "master",
    68  				Date:        "Future",
    69  				Description: "You're using an unpublished version of this software.",
    70  			},
    71  		}
    72  	}
    73  
    74  	for _, u := range updates {
    75  		if u.Version == defaults.Version {
    76  			return []releasenotes.ReleaseNote{u}
    77  		}
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  func filterUpdates(from string, notes []releasenotes.ReleaseNote) []releasenotes.ReleaseNote {
    84  	var filtered []releasenotes.ReleaseNote
    85  	fromV, fromErr := version.NewVersion(from)
    86  
    87  	for c, n := range notes {
    88  		noteV, noteErr := version.NewVersion(n.Version)
    89  
    90  		if (fromErr == nil && noteErr == nil && fromV.LessThan(noteV)) || n.Version == from {
    91  			filtered = notes[c:]
    92  			break
    93  		}
    94  	}
    95  
    96  	if exclusive && len(filtered) != 0 && filtered[0].Version == from {
    97  		filtered = filtered[1:]
    98  	}
    99  
   100  	return filtered
   101  }
   102  
   103  func printReleaseNotes(notes []releasenotes.ReleaseNote) {
   104  	for c, n := range notes {
   105  		fmt.Printf("Version %v (%v)\n", n.Version, n.Date)
   106  		fmt.Println(n.Description)
   107  
   108  		if c != len(notes)-1 {
   109  			fmt.Println()
   110  		}
   111  	}
   112  }
   113  
   114  func init() {
   115  	Cmd.Flags().BoolVar(&all, "all", false, "See all notes")
   116  	Cmd.Flags().StringVar(&from, "from", "", "See notes since a given version")
   117  	Cmd.Flags().BoolVar(&exclusive, "exclusive", false, "Filter version equal to --from value")
   118  }