github.com/vektra/tachyon@v0.0.0-20150921164542-0da4f3861aef/main.go (about)

     1  package tachyon
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/jessevdk/go-flags"
     6  	"os"
     7  	"path/filepath"
     8  )
     9  
    10  type Options struct {
    11  	Vars        map[string]string `short:"s" long:"set" description:"Set a variable"`
    12  	ShowOutput  bool              `short:"o" long:"output" description:"Show command output"`
    13  	Host        string            `short:"t" long:"host" description:"Run the playbook on another host"`
    14  	Development bool              `long:"dev" description:"Use a dev version of tachyon"`
    15  	CleanHost   bool              `long:"clean-host" description:"Clean the host cache before using"`
    16  	Debug       bool              `short:"d" long:"debug" description:"Show all information about commands"`
    17  	Release     string            `long:"release" description:"The release to use when remotely invoking tachyon"`
    18  	JSON        bool              `long:"json" description:"Output the run details in chunked json"`
    19  	Install     bool              `long:"install" description:"Install tachyon a remote machine"`
    20  }
    21  
    22  var Release string = "dev"
    23  var Arg0 string
    24  
    25  func Main(args []string) int {
    26  	var opts Options
    27  
    28  	abs, err := filepath.Abs(args[0])
    29  	if err != nil {
    30  		panic(err)
    31  	}
    32  
    33  	Arg0 = abs
    34  
    35  	parser := flags.NewParser(&opts, flags.Default)
    36  
    37  	for _, o := range parser.Command.Group.Groups()[0].Options() {
    38  		if o.LongName == "release" {
    39  			o.Default = []string{Release}
    40  		}
    41  	}
    42  	args, err = parser.ParseArgs(args)
    43  
    44  	if err != nil {
    45  		if serr, ok := err.(*flags.Error); ok {
    46  			if serr.Type == flags.ErrHelp {
    47  				return 2
    48  			}
    49  		}
    50  
    51  		fmt.Printf("Error parsing options: %s", err)
    52  		return 1
    53  	}
    54  
    55  	if !opts.Install && len(args) != 2 {
    56  		fmt.Printf("Usage: tachyon [options] <playbook>\n")
    57  		return 1
    58  	}
    59  
    60  	if opts.Host != "" {
    61  		return runOnHost(&opts, args)
    62  	}
    63  
    64  	cfg := &Config{ShowCommandOutput: opts.ShowOutput}
    65  
    66  	ns := NewNestedScope(nil)
    67  
    68  	for k, v := range opts.Vars {
    69  		ns.Set(k, v)
    70  	}
    71  
    72  	env := NewEnv(ns, cfg)
    73  	defer env.Cleanup()
    74  
    75  	if opts.JSON {
    76  		env.ReportJSON()
    77  	}
    78  
    79  	playbook, err := NewPlaybook(env, args[1])
    80  	if err != nil {
    81  		fmt.Printf("Error loading plays: %s\n", err)
    82  		return 1
    83  	}
    84  
    85  	cur, err := os.Getwd()
    86  	if err != nil {
    87  		fmt.Printf("Unable to figure out the current directory: %s\n", err)
    88  		return 1
    89  	}
    90  
    91  	defer os.Chdir(cur)
    92  	os.Chdir(playbook.baseDir)
    93  
    94  	runner := NewRunner(env, playbook.Plays)
    95  	err = runner.Run(env)
    96  
    97  	if err != nil {
    98  		fmt.Fprintf(os.Stderr, "Error running playbook: %s\n", err)
    99  		return 1
   100  	}
   101  
   102  	return 0
   103  }
   104  
   105  func runOnHost(opts *Options, args []string) int {
   106  	if opts.Install {
   107  		fmt.Printf("=== Installing tachyon on %s\n", opts.Host)
   108  	} else {
   109  		fmt.Printf("=== Executing playbook on %s\n", opts.Host)
   110  	}
   111  
   112  	var playbook string
   113  
   114  	if !opts.Install {
   115  		playbook = args[1]
   116  	}
   117  
   118  	t := &Tachyon{
   119  		Target:      opts.Host,
   120  		Debug:       opts.Debug,
   121  		Clean:       opts.CleanHost,
   122  		Dev:         opts.Development,
   123  		Playbook:    playbook,
   124  		Release:     opts.Release,
   125  		InstallOnly: opts.Install,
   126  	}
   127  
   128  	_, err := RunAdhocCommand(t, "")
   129  	if err != nil {
   130  		fmt.Printf("Error: %s\n", err)
   131  		return 1
   132  	}
   133  
   134  	return 0
   135  }