github.com/jgrancell/metasync@v0.0.0-20220105143315-c43793d9d9c1/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/jgrancell/metasync/app"
     9  	"github.com/jgrancell/metasync/configuration"
    10  )
    11  
    12  var (
    13  	configPath string
    14  	debug      bool
    15  	diff       bool
    16  	dryRun     bool
    17  	subcommand string
    18  	verbose    bool
    19  
    20  	version string = "0.1.0"
    21  )
    22  
    23  func main() {
    24  	os.Exit(Run())
    25  }
    26  
    27  func Run() int {
    28  
    29  	// We could set the flags normally, but in the event we need different register and deregister flags in the future
    30  	// I've added this as a Flagset for easy duplication between the two commands.
    31  	flagset := flag.NewFlagSet("standard", flag.ExitOnError)
    32  	flagset.StringVar(&configPath, "conf", ".metasync.yml", "The metasync YAML configuration file to read from.")
    33  	flagset.BoolVar(&dryRun, "dryrun", false, "Dry-run outputs the changes that would be made without changing any files.")
    34  	flagset.BoolVar(&verbose, "verbose", false, "Provide verbose output.")
    35  	flagset.BoolVar(&debug, "debug", false, "Provide debugging output.")
    36  	flagset.BoolVar(&diff, "diff", false, "Show diffs for files that require sync.")
    37  
    38  	// Detecting our subcommand and parsing CLI flags
    39  	switch os.Args[1] {
    40  	case "sync":
    41  		subcommand = "sync"
    42  		flagset.Parse(os.Args[2:])
    43  	default:
    44  		fmt.Println("The first argument to metasync must be a valid subcommand. These are:")
    45  		fmt.Println("  sync")
    46  		return 1
    47  	}
    48  
    49  	// Setting up our configurations and initializing the application
    50  	conf := &configuration.Configuration{
    51  		ConfigurationPath: configPath,
    52  	}
    53  	if err := conf.Load(); err != nil {
    54  		fmt.Println(err.Error())
    55  		return 1
    56  	}
    57  
    58  	// Initializing the application
    59  	app := &app.App{
    60  		Configuration: conf,
    61  		Debug:         debug,
    62  		Dryrun:        dryRun,
    63  		ShowDiffs:     diff,
    64  		Subcommand:    subcommand,
    65  		Verbose:       verbose,
    66  		Version:       version,
    67  	}
    68  	return app.Run()
    69  }