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

     1  package app
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/jgrancell/metasync/configuration"
     8  	"github.com/jgrancell/metasync/utils"
     9  )
    10  
    11  type App struct {
    12  	Configuration *configuration.Configuration
    13  	Debug         bool
    14  	Dryrun        bool
    15  	ShowDiffs     bool
    16  	Subcommand    string
    17  	Verbose       bool
    18  	Version       string
    19  }
    20  
    21  func (a *App) Run() int {
    22  	// Running our subcommand
    23  	if a.Subcommand == "sync" {
    24  		// TODO: add app.Sync() or similar here
    25  		return a.Sync(".")
    26  	} else {
    27  		fmt.Println("Specified subcommand", a.Subcommand, "is not valid.")
    28  		return 1
    29  	}
    30  }
    31  
    32  func (a *App) Sync(target string) int {
    33  	a.LogDebug("Beginning repository sync.")
    34  
    35  	repo := &Repository{
    36  		Ref:                    a.Configuration.SourceRepoRef,
    37  		RefType:                a.Configuration.SourceRepoRefType,
    38  		RequiresAuthentication: false,
    39  		ShowDiffs:              a.ShowDiffs,
    40  		TemplatesDirectory:     a.Configuration.SourceTemplatePath,
    41  		Url:                    a.Configuration.SourceRepository,
    42  	}
    43  
    44  	// Preparation: we setup our starting and target directory variables
    45  	if err := utils.MoveToTarget(target); err != nil {
    46  		return utils.VisualizeError(err)
    47  	}
    48  
    49  	// Step 1: we create a temporary directory
    50  	tempdir, err := os.MkdirTemp("", "metasync-git-*")
    51  	defer os.RemoveAll(tempdir)
    52  	if err != nil {
    53  		return utils.VisualizeError(err)
    54  	}
    55  
    56  	// Step 2: we clone down the source repository and checkout the target ref
    57  	a.LogDebug("Fetching source templates from remote repository.")
    58  	if err = repo.Clone(tempdir, a.Debug); err != nil {
    59  		return utils.VisualizeError(err)
    60  	}
    61  
    62  	a.LogDebug("Checking out request ref " + repo.RefType + "/" + repo.Ref + " for source repository.")
    63  
    64  	if err = repo.Checkout(); err != nil {
    65  		return utils.VisualizeError(err)
    66  	}
    67  	a.LogDebug("Setup of source template repository complete.")
    68  
    69  	// Step 3: we determine if there are content differences between each template file and local file
    70  	a.LogDebug("Parsing source template files to find changes.")
    71  	if err := repo.FindSyncCandidates(); err != nil {
    72  		return utils.VisualizeError(err)
    73  	}
    74  
    75  	// Step 4: we update any sync candidate files if we don't have dryrun enabled
    76  	if len(repo.CandidateFiles) == 0 {
    77  		return 0
    78  	} else {
    79  		if a.Dryrun {
    80  			fmt.Println()
    81  			fmt.Println("*** Dryrun mode is enabled. Exiting without making changes.")
    82  			return 0
    83  		} else {
    84  			fmt.Println()
    85  			a.LogDebug("Updating all out-of-sync files.")
    86  			err := repo.ExecuteCandidateSync()
    87  			if err == nil {
    88  				fmt.Println("Success! All files have been synced.")
    89  			}
    90  			return utils.VisualizeError(err)
    91  		}
    92  	}
    93  }
    94  
    95  func (a *App) LogDebug(text string) {
    96  	if a.Debug {
    97  		fmt.Println(text)
    98  	}
    99  }