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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  )
     8  
     9  // SetupInitialMachine will setup a machine to be able to use dot it'll do the
    10  // following:
    11  // 1. Create a .dotconfig file
    12  // 2. Create the files and backup folders
    13  // 3. Add the .dotconfig for tracking
    14  func SetupInitialMachine(pathDotConfig string) {
    15  	// create .dotconfig file
    16  	err := CreateDotConfigFile(pathDotConfig)
    17  	if err != nil {
    18  		PrintBodyError(err.Error())
    19  		return
    20  	}
    21  
    22  	// create dot folders
    23  	err = CreateDotFolders()
    24  	if err != nil {
    25  		PrintBodyError(err.Error())
    26  		return
    27  	}
    28  
    29  	// add .dotconfig for tracking
    30  	_ = TrackFile("dotconfig", pathDotConfig, false, false)
    31  }
    32  
    33  // CreateDotConfigFile will create a .dotconfig file in the specified path
    34  func CreateDotConfigFile(pathDotConfig string) error {
    35  	// remove HomeDir() from the currentWorkingDir to get relative path
    36  	// we need this relative path to put in the .dotconfig file
    37  	relPath, err := GetRelativePathFromCwd()
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	// create .dotconfig file
    43  	payload := fmt.Sprintf("{\"dot_path\": \"%s\", \"files\": {}}", relPath)
    44  	err = ioutil.WriteFile(pathDotConfig, []byte(payload), 0755)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	message := fmt.Sprintf("Creating new .dotconfig file: %s", pathDotConfig)
    50  	PrintBody(message)
    51  
    52  	return nil
    53  }
    54  
    55  // CreateDotFolders() will create the files and backup folders
    56  func CreateDotFolders() error {
    57  	currentWorkingDir, err := os.Getwd()
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	folders := [2]string{
    63  		fmt.Sprintf("%s/files", currentWorkingDir),
    64  		fmt.Sprintf("%s/backup", currentWorkingDir),
    65  	}
    66  
    67  	for _, folder := range folders {
    68  		message := fmt.Sprintf("Creating folder: %s", folder)
    69  		PrintBody(message)
    70  		err = os.Mkdir(folder, 0755)
    71  		if err != nil {
    72  			return err
    73  		}
    74  	}
    75  
    76  	return nil
    77  }