github.com/jpbruinsslot/dot@v0.0.0-20231229172555-797f05ba0642/commands.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"text/tabwriter"
     8  )
     9  
    10  // CommandSync will do several things depending configuration:
    11  //  1. Sync files when there is a .dotconfig present in the correct location
    12  //  2. Create a .dotconfig in the correct location when it isn't
    13  //  3. Create a new setup of dot, including a .dotconfig, files and backup
    14  //     folders
    15  func CommandSync() {
    16  	// get current working directory
    17  	currentWorkingDir, err := os.Getwd()
    18  	if err != nil {
    19  		log.Fatal(err)
    20  	}
    21  
    22  	// path to .dotconfig in current working dir
    23  	pathDotConfigCwd := fmt.Sprintf(
    24  		"%s/files/dotconfig/%s", currentWorkingDir, ConfigFileName)
    25  
    26  	// here we try to uncover 3 possibilities:
    27  	// 1. .dotconfig (symlink) is already on machine in correct location
    28  	// 2. .dotconfig (regular file) is already on machine
    29  	// 3. .dotconfig not in home folder but in current working directory
    30  	// 4. .dotconfig not in home folder and not in current working directory
    31  	if _, err := os.Lstat(PathDotConfig); err == nil {
    32  
    33  		// .dotconfig (symlink) found in home dir => SyncFiles
    34  		PrintBody("The .dotconfig file is present")
    35  
    36  		// relink everything
    37  		SyncFiles()
    38  
    39  	} else if _, err := os.Stat(HomeDir() + "/" + ConfigFileName); err == nil {
    40  		// .dotconfig (regular file, not symlinked) found in home dir =>
    41  		// symlink .dotconfig
    42  		PrintBody("Found .dotconfig file in home folder")
    43  
    44  		// make sure .dotconfig is present in DotPath
    45  		if _, err := os.Stat(pathDotConfigCwd); err != nil {
    46  			PrintBodyError("couldn't find .dotconfig in your archive, make sure it is present")
    47  			return
    48  		}
    49  
    50  		// remove found .dotconfig
    51  		err = os.Remove(HomeDir() + "/" + ConfigFileName)
    52  		if err != nil {
    53  			log.Fatal(err)
    54  		}
    55  
    56  		// make symlink for .dotconfig
    57  		dotconfigOld := fmt.Sprintf("%s/files/dotconfig/%s",
    58  			currentWorkingDir, ConfigFileName)
    59  
    60  		dotconfigNew := fmt.Sprintf("%s/%s", HomeDir(), ConfigFileName)
    61  
    62  		err = os.Symlink(dotconfigOld, dotconfigNew)
    63  		if err != nil {
    64  			log.Fatal(err)
    65  		}
    66  
    67  		// relink everything
    68  		SyncFiles()
    69  	} else if _, err := os.Stat(pathDotConfigCwd); err == nil {
    70  
    71  		// .dotconfig not found in home dir,
    72  		// .dotconfig found in current working dir => symlink .dotconfig
    73  		PrintBody("Found .dotconfig file in repository folder")
    74  
    75  		// make a symlink for .dotconfig file
    76  		dotconfigOld := fmt.Sprintf("%s/files/dotconfig/%s",
    77  			currentWorkingDir, ConfigFileName)
    78  
    79  		dotconfigNew := fmt.Sprintf("%s/%s", HomeDir(), ConfigFileName)
    80  
    81  		err = os.Symlink(dotconfigOld, dotconfigNew)
    82  		if err != nil {
    83  			log.Fatal(err)
    84  		}
    85  
    86  		// relink everything
    87  		SyncFiles()
    88  	} else {
    89  
    90  		// .dotconfig not found in home dir,
    91  		// .dotconfig not found in current working dir => new setup
    92  		PrintBody("Couldn't find the .dotconfig file, do you want to create a new one? [Y/N]")
    93  
    94  		// get input
    95  		var input string
    96  		_, err := fmt.Scan(&input)
    97  		if err != nil {
    98  			log.Fatal(err)
    99  		}
   100  
   101  		if input == "y" || input == "Y" {
   102  			// setup initial machine
   103  			// create new .dotconfig file
   104  			SetupInitialMachine(PathDotConfig)
   105  
   106  			PrintBody("You're now ready to use dot! Type 'dot -help' for help")
   107  		} else {
   108  			return
   109  		}
   110  	}
   111  }
   112  
   113  // CommandAdd will add a file or folder for tracking.
   114  func CommandAdd(name, path string, push, force bool) {
   115  	PrintHeader("Adding new entry for tracking ...")
   116  	_ = TrackFile(name, path, push, false)
   117  }
   118  
   119  // CommandRemove will remove a file from tracking.
   120  func CommandRemove(name string, push bool) {
   121  	PrintHeader("Removing entry from tracking ...")
   122  	UntrackFile(name, push)
   123  }
   124  
   125  // CommandList will output the list of files that are being tracked by dot.
   126  func CommandList() {
   127  	PrintHeader("Following files are being tracked by dot ...")
   128  
   129  	// open config file
   130  	config, err := NewConfig(HomeDir() + "/" + ConfigFileName)
   131  	if err != nil {
   132  		PrintBodyError("not able to find .dotconfig")
   133  		return
   134  	}
   135  
   136  	// check if there is anything to display
   137  	if len(config.Files) == 0 {
   138  		PrintBodyError(
   139  			"there are no files being tracked. Begin doing so, with `dot add -name [name] -path [path]`",
   140  		)
   141  		return
   142  	}
   143  
   144  	// print out the tracked files
   145  	w := new(tabwriter.Writer)
   146  	w.Init(os.Stdout, 0, 8, 0, '\t', 0)
   147  	fmt.Fprintln(w, "name\tpath")
   148  	for name, path := range config.Files {
   149  		line := fmt.Sprintf("%s\t%s%s", name, HomeDir(), path)
   150  		fmt.Fprintln(w, line)
   151  	}
   152  	w.Flush()
   153  }