github.com/mfpierre/corectl@v0.5.6/main.go (about)

     1  // Copyright 2015 - António Meireles  <antonio.meireles@reformi.st>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"log"
    21  	"os"
    22  	"runtime"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/google/go-github/github"
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  var (
    31  	// RootCmd ...
    32  	RootCmd = &cobra.Command{
    33  		Use:   "corectl",
    34  		Short: "CoreOS over OSX made simple.",
    35  		Long: fmt.Sprintf("%s\n%s",
    36  			"CoreOS over OSX made simple.",
    37  			"❯❯❯ http://github.com/TheNewNormal/corectl"),
    38  		RunE: func(cmd *cobra.Command, args []string) (err error) {
    39  			versionCommand(cmd, args)
    40  			return cmd.Usage()
    41  		},
    42  	}
    43  	versionCmd = &cobra.Command{
    44  		Use: "version", Short: "Shows corectl version information",
    45  		Run: versionCommand,
    46  	}
    47  	engine sessionContext
    48  	// -ldflags "-X main.Version=
    49  	//            `git describe --abbrev=6 --dirty=-unreleased --always --tags`"
    50  	Version, BuildDate string
    51  )
    52  
    53  func main() {
    54  	if err := RootCmd.Execute(); err != nil {
    55  		os.Exit(-1)
    56  	}
    57  }
    58  func init() {
    59  	// logger defaults
    60  	log.SetFlags(0)
    61  	log.SetOutput(os.Stderr)
    62  	log.SetPrefix("[corectl] ")
    63  
    64  	RootCmd.PersistentFlags().Bool("debug", false,
    65  		"adds extra verbosity, and options, for debugging purposes "+
    66  			"and/or power users")
    67  
    68  	RootCmd.SetUsageTemplate(HelpTemplate)
    69  	RootCmd.AddCommand(versionCmd)
    70  
    71  	// remaining defaults / startupChecks
    72  	engine.init()
    73  }
    74  
    75  func versionCommand(cmd *cobra.Command, args []string) {
    76  	var (
    77  		err      error
    78  		latest   *github.RepositoryRelease
    79  		stamp, _ = time.Parse("2006-01-02T15:04:05MST", BuildDate)
    80  	)
    81  	fmt.Printf("%s\n%s\n%s\n\n", "CoreOS over OSX made simple.",
    82  		" - http://github.com/TheNewNormal/corectl",
    83  		"   Copyright (c) 2015-2016, António Meireles")
    84  	fmt.Printf("Running version: %s\n"+
    85  		"                 built with %s, %v\n",
    86  		strings.TrimPrefix(Version, "v"), runtime.Version(), stamp)
    87  
    88  	if latest, _, err =
    89  		github.NewClient(nil).Repositories.GetLatestRelease("TheNewNormal",
    90  			"corectl"); err != nil {
    91  		return
    92  	}
    93  	fmt.Println("Latest upstream:", strings.TrimPrefix(
    94  		strings.Trim(github.Stringify(latest.Name), "\""), "v"))
    95  }