github.com/naphatkrit/deis@v1.12.3/client/parser/releases.go (about)

     1  package parser
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/deis/deis/client/cmd"
     8  	docopt "github.com/docopt/docopt-go"
     9  )
    10  
    11  // Releases routes releases commands to their specific function.
    12  func Releases(argv []string) error {
    13  	usage := `
    14  Valid commands for releases:
    15  
    16  releases:list        list an application's release history
    17  releases:info        print information about a specific release
    18  releases:rollback    return to a previous release
    19  
    20  Use 'deis help [command]' to learn more.
    21  `
    22  
    23  	switch argv[0] {
    24  	case "releases:list":
    25  		return releasesList(argv)
    26  	case "releases:info":
    27  		return releasesInfo(argv)
    28  	case "releases:rollback":
    29  		return releasesRollback(argv)
    30  	default:
    31  		if printHelp(argv, usage) {
    32  			return nil
    33  		}
    34  
    35  		if argv[0] == "releases" {
    36  			argv[0] = "releases:list"
    37  			return releasesList(argv)
    38  		}
    39  
    40  		PrintUsage()
    41  		return nil
    42  	}
    43  }
    44  
    45  func releasesList(argv []string) error {
    46  	usage := `
    47  Lists release history for an application.
    48  
    49  Usage: deis releases:list [options]
    50  
    51  Options:
    52    -a --app=<app>
    53      the uniquely identifiable name for the application.
    54    -l --limit=<num>
    55      the maximum number of results to display, defaults to config setting
    56  `
    57  
    58  	args, err := docopt.Parse(usage, argv, true, "", false, true)
    59  
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	results, err := responseLimit(safeGetValue(args, "--limit"))
    65  
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	return cmd.ReleasesList(safeGetValue(args, "--app"), results)
    71  }
    72  
    73  func releasesInfo(argv []string) error {
    74  	usage := `
    75  Prints info about a particular release.
    76  
    77  Usage: deis releases:info <version> [options]
    78  
    79  Arguments:
    80    <version>
    81      the release of the application, such as 'v1'.
    82  
    83  Options:
    84    -a --app=<app>
    85      the uniquely identifiable name for the application.
    86  `
    87  
    88  	args, err := docopt.Parse(usage, argv, true, "", false, true)
    89  
    90  	version, err := versionFromString(args["<version>"].(string))
    91  
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	return cmd.ReleasesInfo(safeGetValue(args, "--app"), version)
    97  }
    98  
    99  func releasesRollback(argv []string) error {
   100  	usage := `
   101  Rolls back to a previous application release.
   102  
   103  Usage: deis releases:rollback [<version>] [options]
   104  
   105  Arguments:
   106    <version>
   107      the release of the application, such as 'v1'.
   108  
   109  Options:
   110    -a --app=<app>
   111      the uniquely identifiable name of the application.
   112  `
   113  
   114  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   115  
   116  	var version int
   117  
   118  	if args["<version>"] == nil {
   119  		version = -1
   120  	} else {
   121  		version, err = versionFromString(args["<version>"].(string))
   122  
   123  		if err != nil {
   124  			return err
   125  		}
   126  	}
   127  
   128  	return cmd.ReleasesRollback(safeGetValue(args, "--app"), version)
   129  }
   130  
   131  func versionFromString(version string) (int, error) {
   132  	if version[:1] == "v" {
   133  		if len(version) < 2 {
   134  			return -1, fmt.Errorf("%s is not in the form 'v#'", version)
   135  		}
   136  
   137  		return strconv.Atoi(version[1:])
   138  	}
   139  
   140  	return strconv.Atoi(version)
   141  }