github.com/amy/helm@v2.7.2+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 } 54 55 func newHistoryCmd(c helm.Interface, w io.Writer) *cobra.Command { 56 his := &historyCmd{out: w, helmc: c} 57 58 cmd := &cobra.Command{ 59 Use: "history [flags] RELEASE_NAME", 60 Long: historyHelp, 61 Short: "fetch release history", 62 Aliases: []string{"hist"}, 63 PreRunE: setupConnection, 64 RunE: func(cmd *cobra.Command, args []string) error { 65 switch { 66 case len(args) == 0: 67 return errReleaseRequired 68 case his.helmc == nil: 69 his.helmc = newClient() 70 } 71 his.rls = args[0] 72 return his.run() 73 }, 74 } 75 76 cmd.Flags().Int32Var(&his.max, "max", 256, "maximum number of revision to include in history") 77 78 return cmd 79 } 80 81 func (cmd *historyCmd) run() error { 82 r, err := cmd.helmc.ReleaseHistory(cmd.rls, helm.WithMaxHistory(cmd.max)) 83 if err != nil { 84 return prettyError(err) 85 } 86 if len(r.Releases) == 0 { 87 return nil 88 } 89 90 fmt.Fprintln(cmd.out, formatHistory(r.Releases)) 91 return nil 92 } 93 94 func formatHistory(rls []*release.Release) string { 95 tbl := uitable.New() 96 tbl.MaxColWidth = 60 97 tbl.AddRow("REVISION", "UPDATED", "STATUS", "CHART", "DESCRIPTION") 98 for i := len(rls) - 1; i >= 0; i-- { 99 r := rls[i] 100 c := formatChartname(r.Chart) 101 t := timeconv.String(r.Info.LastDeployed) 102 s := r.Info.Status.Code.String() 103 v := r.Version 104 d := r.Info.Description 105 tbl.AddRow(v, t, s, c, d) 106 } 107 return tbl.String() 108 } 109 110 func formatChartname(c *chart.Chart) string { 111 if c == nil || c.Metadata == nil { 112 // This is an edge case that has happened in prod, though we don't 113 // know how: https://github.com/kubernetes/helm/issues/1347 114 return "MISSING" 115 } 116 return fmt.Sprintf("%s-%s", c.Metadata.Name, c.Metadata.Version) 117 }