github.com/y-taka-23/helm@v2.8.0+incompatible/cmd/helm/history.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  
    23  	"github.com/gosuri/uitable"
    24  	"github.com/spf13/cobra"
    25  
    26  	"k8s.io/helm/pkg/helm"
    27  	"k8s.io/helm/pkg/proto/hapi/chart"
    28  	"k8s.io/helm/pkg/proto/hapi/release"
    29  	"k8s.io/helm/pkg/timeconv"
    30  )
    31  
    32  var historyHelp = `
    33  History prints historical revisions for a given release.
    34  
    35  A default maximum of 256 revisions will be returned. Setting '--max'
    36  configures the maximum length of the revision list returned.
    37  
    38  The historical release set is printed as a formatted table, e.g:
    39  
    40      $ helm history angry-bird --max=4
    41      REVISION   UPDATED                      STATUS           CHART        DESCRIPTION
    42      1           Mon Oct 3 10:15:13 2016     SUPERSEDED      alpine-0.1.0  Initial install
    43      2           Mon Oct 3 10:15:13 2016     SUPERSEDED      alpine-0.1.0  Upgraded successfully
    44      3           Mon Oct 3 10:15:13 2016     SUPERSEDED      alpine-0.1.0  Rolled back to 2
    45      4           Mon Oct 3 10:15:13 2016     DEPLOYED        alpine-0.1.0  Upgraded successfully
    46  `
    47  
    48  type historyCmd struct {
    49  	max      int32
    50  	rls      string
    51  	out      io.Writer
    52  	helmc    helm.Interface
    53  	colWidth uint
    54  }
    55  
    56  func newHistoryCmd(c helm.Interface, w io.Writer) *cobra.Command {
    57  	his := &historyCmd{out: w, helmc: c}
    58  
    59  	cmd := &cobra.Command{
    60  		Use:     "history [flags] RELEASE_NAME",
    61  		Long:    historyHelp,
    62  		Short:   "fetch release history",
    63  		Aliases: []string{"hist"},
    64  		PreRunE: setupConnection,
    65  		RunE: func(cmd *cobra.Command, args []string) error {
    66  			switch {
    67  			case len(args) == 0:
    68  				return errReleaseRequired
    69  			case his.helmc == nil:
    70  				his.helmc = newClient()
    71  			}
    72  			his.rls = args[0]
    73  			return his.run()
    74  		},
    75  	}
    76  
    77  	f := cmd.Flags()
    78  	f.Int32Var(&his.max, "max", 256, "maximum number of revision to include in history")
    79  	f.UintVar(&his.colWidth, "col-width", 60, "specifies the max column width of output")
    80  
    81  	return cmd
    82  }
    83  
    84  func (cmd *historyCmd) run() error {
    85  	r, err := cmd.helmc.ReleaseHistory(cmd.rls, helm.WithMaxHistory(cmd.max))
    86  	if err != nil {
    87  		return prettyError(err)
    88  	}
    89  	if len(r.Releases) == 0 {
    90  		return nil
    91  	}
    92  
    93  	fmt.Fprintln(cmd.out, formatHistory(r.Releases, cmd.colWidth))
    94  	return nil
    95  }
    96  
    97  func formatHistory(rls []*release.Release, colWidth uint) string {
    98  	tbl := uitable.New()
    99  
   100  	tbl.MaxColWidth = colWidth
   101  	tbl.AddRow("REVISION", "UPDATED", "STATUS", "CHART", "DESCRIPTION")
   102  	for i := len(rls) - 1; i >= 0; i-- {
   103  		r := rls[i]
   104  		c := formatChartname(r.Chart)
   105  		t := timeconv.String(r.Info.LastDeployed)
   106  		s := r.Info.Status.Code.String()
   107  		v := r.Version
   108  		d := r.Info.Description
   109  		tbl.AddRow(v, t, s, c, d)
   110  	}
   111  	return tbl.String()
   112  }
   113  
   114  func formatChartname(c *chart.Chart) string {
   115  	if c == nil || c.Metadata == nil {
   116  		// This is an edge case that has happened in prod, though we don't
   117  		// know how: https://github.com/kubernetes/helm/issues/1347
   118  		return "MISSING"
   119  	}
   120  	return fmt.Sprintf("%s-%s", c.Metadata.Name, c.Metadata.Version)
   121  }