github.com/ETCDEVTeam/janus@v0.2.4-0.20180611132348-f6c8fba730fa/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/ETCDEVTeam/janus/gcp"
     9  	"github.com/ETCDEVTeam/janus/gitvv"
    10  )
    11  
    12  func main() {
    13  
    14  	// Subcommands
    15  	deployCommand := flag.NewFlagSet("deploy", flag.ExitOnError)
    16  	versionCommand := flag.NewFlagSet("version", flag.ExitOnError)
    17  
    18  	// Deploy flags
    19  	var key, files, to string
    20  	var gpg bool
    21  	// Version flags
    22  	var dir, format string
    23  
    24  	// Set up flags.
    25  	//
    26  	// Deploy
    27  	deployCommand.StringVar(&to, "to", "", `directory path to deploy files to
    28  
    29  the first directory in the given path is GCP <bucket>
    30  files will be uploaded INTO this path
    31  
    32  eg.
    33  -to=builds.etcdevteam.com/go-ethereum/releases/v3.5.x \
    34  -files=./dist/*.zip [eg. ./dist/geth-linux.zip, ./dist/geth-osx.zip]
    35  
    36  --> builds.etcdevteam.com/go-ethereum/releases/v3.5.x/geth-linux.zip
    37  --> builds.etcdevteam.com/go-ethereum/releases/v3.5.x/geth-osx.zip
    38  `)
    39  	deployCommand.StringVar(&files, "files", "", "file(s) to upload, allows globbing")
    40  	deployCommand.StringVar(&key, "key", "", "service account json key file, may be encrypted OR decrypted")
    41  	deployCommand.BoolVar(&gpg, "gpg", false, "use GPG 2 instead of openssl for decryption")
    42  	// Version
    43  	versionCommand.StringVar(&dir, "dir", "", `path to base directory`)
    44  	versionCommand.StringVar(&format, "format", "", `format of git version:
    45  
    46  %M - major version
    47  %m - minor version
    48  %P - patch version
    49  %C - commit count since last tag
    50  %S[|NUMBER] - HEAD sha1, where NUMBER is optional desired length of hash (default: 7)
    51  %B - hybrid patch number (B = semver_minor_version*100 + commit_count)
    52  
    53  Default: v%M.%m.%P+%C-%S -> v3.5.0+66-bbb06b1
    54  `)
    55  
    56  	flag.Usage = func() {
    57  		fmt.Println("Usage for Janus:")
    58  		fmt.Println("  $ janus deploy -to builds.etcdevteam.com/go-ethereum/version -file geth.zip -key .gcloud.json")
    59  		fmt.Println("  $ janus version -format 'v%M.%m.%P+%C-%S'")
    60  		flag.PrintDefaults()
    61  	}
    62  
    63  	// Ensure subcommand is used.
    64  	if len(os.Args) < 2 {
    65  		fmt.Println("'deploy' or 'version' subcommand is required")
    66  		os.Exit(1)
    67  	}
    68  
    69  	// Parse subcommands.
    70  	switch os.Args[1] {
    71  	case "deploy":
    72  		deployCommand.Parse(os.Args[2:])
    73  	case "version":
    74  		versionCommand.Parse(os.Args[2:])
    75  	default:
    76  		flag.Usage()
    77  		os.Exit(1)
    78  	}
    79  
    80  	// Handle which command is used.
    81  	//
    82  	// Deploy
    83  	if deployCommand.Parsed() {
    84  		// Ensure required flags are set.
    85  		if to == "" {
    86  			fmt.Println("--to requires an argument")
    87  			flag.Usage()
    88  			os.Exit(1)
    89  		}
    90  		if files == "" {
    91  			fmt.Println("--files requires an argument")
    92  			flag.Usage()
    93  			os.Exit(1)
    94  		}
    95  		if key == "" {
    96  			fmt.Println("--key requires an argument")
    97  			flag.Usage()
    98  			os.Exit(1)
    99  		}
   100  
   101  		// Handle deploy.
   102  		// -- Will check for existing file(s) to upload, will return error if not exists.
   103  		if e := gcp.SendToGCP(to, files, key, gpg); e != nil {
   104  			fmt.Println("Failed to deploy:")
   105  			fmt.Println(e)
   106  			os.Exit(1)
   107  		}
   108  	} else
   109  	// Version
   110  	if versionCommand.Parsed() {
   111  		v := gitvv.GetVersion(format, dir)
   112  		fmt.Print(v)
   113  		os.Exit(0)
   114  	} else
   115  	// No command
   116  	{
   117  		// Must use a subcommand.
   118  		flag.Usage()
   119  		os.Exit(1)
   120  	}
   121  }