github.com/devcamcar/cli@v0.0.0-20181107134215-706a05759d18/common/bump.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/coreos/go-semver/semver"
     8  	bumper "github.com/giantswarm/semver-bump/bump"
     9  	"github.com/giantswarm/semver-bump/storage"
    10  	"github.com/urfave/cli"
    11  )
    12  
    13  // VType represents the version type
    14  type VType int
    15  
    16  //
    17  const (
    18  	Patch VType = iota
    19  	Minor
    20  	Major
    21  )
    22  
    23  var (
    24  	// InitialVersion - inital fn version.
    25  	InitialVersion = "0.0.1"
    26  )
    27  
    28  // BumpCommand command to build function version.
    29  func BumpCommand() cli.Command {
    30  	cmd := bumpcmd{}
    31  	flags := append([]cli.Flag{}, cmd.flags()...)
    32  	return cli.Command{
    33  		Name:        "bump",
    34  		Usage:       "\tBump function version",
    35  		Aliases:     []string{"bm"},
    36  		Category:    "DEVELOPMENT COMMANDS",
    37  		Description: "This command bumps the version of the func.yaml.",
    38  		Flags:       flags,
    39  		Action:      cmd.bump,
    40  	}
    41  }
    42  
    43  type bumpcmd struct {
    44  	verbose bool
    45  	major   bool
    46  	minor   bool
    47  }
    48  
    49  func (b *bumpcmd) flags() []cli.Flag {
    50  	return []cli.Flag{
    51  		cli.BoolFlag{
    52  			Name:        "major",
    53  			Usage:       "bumps major version",
    54  			Destination: &b.major,
    55  		},
    56  		cli.BoolFlag{
    57  			Name:        "minor",
    58  			Usage:       "bumps minor version",
    59  			Destination: &b.minor,
    60  		},
    61  		cli.BoolFlag{
    62  			Name:        "verbose, v",
    63  			Usage:       "verbose mode",
    64  			Destination: &b.verbose,
    65  		},
    66  		cli.StringFlag{
    67  			Name:  "working-dir,w",
    68  			Usage: "Specify the working directory to bump a function, must be the full path.",
    69  		},
    70  	}
    71  }
    72  
    73  // bump will take the found valid function and bump its version
    74  func (b *bumpcmd) bump(c *cli.Context) error {
    75  	var t VType
    76  	var dir string
    77  
    78  	if b.major {
    79  		t = Major
    80  	} else if b.minor {
    81  		t = Minor
    82  	} else {
    83  		t = Patch
    84  	}
    85  
    86  	dir = GetDir(c)
    87  
    88  	ff, err := ReadInFuncFile()
    89  	version := GetFuncYamlVersion(ff)
    90  	if version == LatestYamlVersion {
    91  		_, err = bumpItWdV20180708(dir, t)
    92  	} else {
    93  		_, err = bumpItWd(dir, t)
    94  	}
    95  
    96  	return err
    97  }
    98  
    99  func bumpItWd(wd string, vtype VType) (*FuncFile, error) {
   100  	fn, err := FindFuncfile(wd)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  	return BumpIt(fn, vtype)
   105  }
   106  
   107  // BumpIt returns updated funcfile
   108  func BumpIt(fpath string, vtype VType) (*FuncFile, error) {
   109  	// fmt.Println("Bumping version in func file at: ", fpath)
   110  	funcfile, err := ParseFuncfile(fpath)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	funcfile, err = bumpVersion(funcfile, vtype)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	if err := storeFuncfile(fpath, funcfile); err != nil {
   121  		return nil, err
   122  	}
   123  	fmt.Println("Bumped to version", funcfile.Version)
   124  	return funcfile, nil
   125  }
   126  
   127  func bumpVersion(funcfile *FuncFile, t VType) (*FuncFile, error) {
   128  	funcfile.Name = cleanImageName(funcfile.Name)
   129  	if funcfile.Version == "" {
   130  		funcfile.Version = InitialVersion
   131  		return funcfile, nil
   132  	}
   133  
   134  	s, err := storage.NewVersionStorage("local", funcfile.Version)
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	version := bumper.NewSemverBumper(s, "")
   140  	var newver *semver.Version
   141  	if t == Major {
   142  		newver, err = version.BumpMajorVersion("", "")
   143  	} else if t == Minor {
   144  		newver, err = version.BumpMinorVersion("", "")
   145  	} else {
   146  		newver, err = version.BumpPatchVersion("", "")
   147  	}
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  
   152  	funcfile.Version = newver.String()
   153  	return funcfile, nil
   154  }
   155  
   156  // cleanImageName is intended to remove any trailing tag from the image name
   157  // since the version field conveys this information. More cleanup could be done
   158  // here in future if necessary.
   159  func cleanImageName(name string) string {
   160  	slashParts := strings.Split(name, "/")
   161  	l := len(slashParts) - 1
   162  	if i := strings.Index(slashParts[l], ":"); i > -1 {
   163  		slashParts[l] = slashParts[l][:i]
   164  	}
   165  	return strings.Join(slashParts, "/")
   166  }
   167  
   168  // --------- FuncFileV20180708 -------------
   169  
   170  func bumpItWdV20180708(wd string, vtype VType) (*FuncFileV20180708, error) {
   171  	fn, err := FindFuncfile(wd)
   172  	if err != nil {
   173  		return nil, err
   174  	}
   175  	return BumpItV20180708(fn, vtype)
   176  }
   177  
   178  // BumpIt returns updated funcfile
   179  func BumpItV20180708(fpath string, vtype VType) (*FuncFileV20180708, error) {
   180  	// fmt.Println("Bumping version in func file at: ", fpath)
   181  	funcfile, err := ParseFuncFileV20180708(fpath)
   182  	if err != nil {
   183  		return nil, err
   184  	}
   185  
   186  	funcfile, err = bumpVersionV20180708(funcfile, vtype)
   187  	if err != nil {
   188  		return nil, err
   189  	}
   190  
   191  	if err := storeFuncFileV20180708(fpath, funcfile); err != nil {
   192  		return nil, err
   193  	}
   194  	fmt.Println("Bumped to version", funcfile.Version)
   195  	return funcfile, nil
   196  }
   197  
   198  func bumpVersionV20180708(funcfile *FuncFileV20180708, t VType) (*FuncFileV20180708, error) {
   199  	funcfile.Name = cleanImageName(funcfile.Name)
   200  	if funcfile.Version == "" {
   201  		funcfile.Version = InitialVersion
   202  		return funcfile, nil
   203  	}
   204  
   205  	s, err := storage.NewVersionStorage("local", funcfile.Version)
   206  	if err != nil {
   207  		return nil, err
   208  	}
   209  
   210  	version := bumper.NewSemverBumper(s, "")
   211  	var newver *semver.Version
   212  	if t == Major {
   213  		newver, err = version.BumpMajorVersion("", "")
   214  	} else if t == Minor {
   215  		newver, err = version.BumpMinorVersion("", "")
   216  	} else {
   217  		newver, err = version.BumpPatchVersion("", "")
   218  	}
   219  	if err != nil {
   220  		return nil, err
   221  	}
   222  
   223  	funcfile.Version = newver.String()
   224  	return funcfile, nil
   225  }