chore(doc): update function documentation
This commit is contained in:
parent
a0662e9066
commit
56da676e15
|
@ -40,6 +40,11 @@ func NewItemsActions(items []configuration.OSLocation, storage string, repositor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Action performs a "save" or "load" action on all given items.
|
||||||
|
//
|
||||||
|
// Any error is printed to STDERR and item is skipped.
|
||||||
|
//
|
||||||
|
// If the performed action is "save", it'll also write the `.index` file with all new items.
|
||||||
func (e *Items) Action(action string) {
|
func (e *Items) Action(action string) {
|
||||||
color.Blue("# %s files and folders\n", action)
|
color.Blue("# %s files and folders\n", action)
|
||||||
newLines := []string{}
|
newLines := []string{}
|
||||||
|
@ -80,6 +85,13 @@ func (e *Items) Action(action string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// saveItem saves a given item inside the configured saved location.
|
||||||
|
//
|
||||||
|
// If an error is given during the process, the function returns an empty string
|
||||||
|
// (meaning the item hasn't been saved) and prints the error in STDERR.
|
||||||
|
//
|
||||||
|
// Else, returns the relative item location from the saved location to write the index
|
||||||
|
// (E.g: /home/user/.config => .config)
|
||||||
func (e *Items) saveItem(src, dst string, index int) string {
|
func (e *Items) saveItem(src, dst string, index int) string {
|
||||||
if err := os.MkdirAll(path.Dir(dst), 0755); err != nil {
|
if err := os.MkdirAll(path.Dir(dst), 0755); err != nil {
|
||||||
PrintError("[%d] failed to create directory architecture for destination path \"%s\": %v", index, path.Dir(dst), err)
|
PrintError("[%d] failed to create directory architecture for destination path \"%s\": %v", index, path.Dir(dst), err)
|
||||||
|
@ -130,6 +142,10 @@ func (e *Items) saveItem(src, dst string, index int) string {
|
||||||
return strings.ReplaceAll(dst, p+"/", "")
|
return strings.ReplaceAll(dst, p+"/", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// loadItem loads a given item onto the system.
|
||||||
|
//
|
||||||
|
// If an error is given during the process, the function returns an empty string
|
||||||
|
// (meaning the item hasn't been saved) and prints the error in STDERR.
|
||||||
func (e *Items) loadItem(src, dst string, index int) {
|
func (e *Items) loadItem(src, dst string, index int) {
|
||||||
if err := os.MkdirAll(path.Dir(dst), 0755); err != nil {
|
if err := os.MkdirAll(path.Dir(dst), 0755); err != nil {
|
||||||
PrintError("[%d] failed to create directory architecture for destination path \"%s\": %v", index, path.Dir(dst), err)
|
PrintError("[%d] failed to create directory architecture for destination path \"%s\": %v", index, path.Dir(dst), err)
|
||||||
|
|
|
@ -22,6 +22,7 @@ var (
|
||||||
ErrAptNotAvailable = errors.New("aptitude is not available on your system")
|
ErrAptNotAvailable = errors.New("aptitude is not available on your system")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// LoadPkgs triggers related functions with passed order
|
||||||
func LoadPkgs(c configuration.PkgManagers) error {
|
func LoadPkgs(c configuration.PkgManagers) error {
|
||||||
color.Blue("\n# Installing packages")
|
color.Blue("\n# Installing packages")
|
||||||
|
|
||||||
|
@ -43,13 +44,14 @@ func LoadPkgs(c configuration.PkgManagers) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SavePkgs triggers related functions with passed order
|
||||||
func SavePkgs(cfg configuration.Configuration) error {
|
func SavePkgs(cfg configuration.Configuration) error {
|
||||||
color.Blue("# Saving user installed packages")
|
color.Blue("# Saving user installed packages")
|
||||||
|
|
||||||
for _, pkg := range cfg.PackageManagers.InstallationOrder {
|
for _, pkg := range cfg.PackageManagers.InstallationOrder {
|
||||||
switch pkg {
|
switch pkg {
|
||||||
case "homebrew":
|
case "homebrew":
|
||||||
if err := SaveBrewPkgs(cfg); err != nil {
|
if err := saveBrewPkgs(cfg); err != nil {
|
||||||
PrintError(err.Error())
|
PrintError(err.Error())
|
||||||
return ErrFailedSaving
|
return ErrFailedSaving
|
||||||
}
|
}
|
||||||
|
@ -61,7 +63,9 @@ func SavePkgs(cfg configuration.Configuration) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveBrewPkgs(cfg configuration.Configuration) error {
|
// saveBrewPkgs gather user installed packages by running `brew leaves --installed-on-request`.
|
||||||
|
// It captures the output, parse it and save it into the configuration.
|
||||||
|
func saveBrewPkgs(cfg configuration.Configuration) error {
|
||||||
if _, err := exec.LookPath("brew"); err != nil {
|
if _, err := exec.LookPath("brew"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -89,6 +93,8 @@ func SaveBrewPkgs(cfg configuration.Configuration) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// installBrewPkgs installs homebrew packages by passing them to homebrew's CLI.
|
||||||
|
// STDERR and STDOUT are captured if verbose flag is passed.
|
||||||
func installBrewPkgs(pkgs []string) error {
|
func installBrewPkgs(pkgs []string) error {
|
||||||
if _, err := exec.LookPath("brew"); err != nil {
|
if _, err := exec.LookPath("brew"); err != nil {
|
||||||
return ErrBrewNotAvailable
|
return ErrBrewNotAvailable
|
||||||
|
@ -128,23 +134,24 @@ func installBrewPkgs(pkgs []string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// installAptPkgs installs all provided "apt" packages by passing them to the Advanced Package Tool's CLI
|
||||||
func installAptPkgs(pkgs []string) error {
|
func installAptPkgs(pkgs []string) error {
|
||||||
if _, err := exec.LookPath("apt-get"); err != nil {
|
if _, err := exec.LookPath("apt"); err != nil {
|
||||||
return ErrAptNotAvailable
|
return ErrAptNotAvailable
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(pkgs) == 0 {
|
if len(pkgs) == 0 {
|
||||||
fmt.Println("aptitude: nothing to do")
|
fmt.Println("apt: nothing to do")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command("sudo", "apt-get", "install")
|
cmd := exec.Command("sudo", "apt", "install")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
|
|
||||||
color.Blue("\n## Installing aptitude packages")
|
color.Blue("\n## Installing apt packages")
|
||||||
|
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
PrintError("aptitude command failed: %v", err)
|
PrintError("apt command failed: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue