Tidy up structure of briefcase

I had a spare fifteen minutes and decided that I should tidy up my
monorepo. The work of tidying up is not finished; this is a small step in the
right direction.

TL;DR
- Created a tools directory
- Created a scratch directory (see README.md for more information)
- Added README.md to third_party
- Renamed delete_dotfile_symlinks -> symlinkManager
- Packaged symlinkManager as an executable symlink-mgr using buildGo
This commit is contained in:
William Carroll 2020-02-12 16:58:29 +00:00
parent 5ec5a6da8c
commit fabf1c9334
89 changed files with 53 additions and 41 deletions

View file

@ -0,0 +1,14 @@
# Dotfile Symlink Manager
Find and delete all symlinks to the dotfiles defined in `$BRIEFCASE`.
Oftentimes I corrupt the state of my configuration files. The intention with
this script is to help me clean things up when this happens. An example workflow
might look like:
```shell
> symlink-mgr --audit
> symlink-mgr --seriously
> briefcase # changes directory to $BRIEFCASE
> make install
```

View file

@ -0,0 +1,15 @@
{
depot ? import <depot> {},
briefcase ? import <briefcase> {},
...
}:
depot.buildGo.program {
name = "symlink-mgr";
srcs = [
./main.go
];
deps = with briefcase.gopkgs; [
utils
];
}

View file

@ -0,0 +1,83 @@
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"utils"
)
var hostnames = map[string]string{
os.Getenv("DESKTOP"): "desktop",
os.Getenv("LAPTOP"): "work_laptop",
os.Getenv("CLOUDTOP"): "cloudtop",
}
func main() {
audit := flag.Bool("audit", false, "Output all symlinks that would be deleted. This is the default behavior. This option is mutually exclusive with the --seriously option.")
seriously := flag.Bool("seriously", false, "Actually delete the symlinks. This option is mutually exclusive with the --audit option.")
repoName := flag.String("repo-name", "briefcase", "The name of the repository.")
deviceOnly := flag.Bool("device-only", false, "Only output the device-specific dotfiles.")
flag.Parse()
if !*audit && !*seriously {
log.Fatal(errors.New("Either -audit or -seriously needs to be set."))
}
if *audit == *seriously {
log.Fatal(errors.New("Arguments -audit and -seriously are mutually exclusive"))
}
home, err := os.UserHomeDir()
utils.FailOn(err)
count := 0
err = filepath.Walk(home, func(path string, info os.FileInfo, err error) error {
if utils.IsSymlink(info.Mode()) {
dest, err := os.Readlink(path)
utils.FailOn(err)
var predicate func(string) bool
if *deviceOnly {
predicate = func(dest string) bool {
var hostname string
hostname, err = os.Hostname()
utils.FailOn(err)
seeking, ok := hostnames[hostname]
if !ok {
log.Fatal(fmt.Sprintf("Hostname \"%s\" not supported in the hostnames map.", hostname))
}
return strings.Contains(dest, *repoName) && strings.Contains(dest, seeking)
}
} else {
predicate = func(dest string) bool {
return strings.Contains(dest, *repoName)
}
}
if predicate(dest) {
if *audit {
fmt.Printf("%s -> %s\n", path, dest)
} else if *seriously {
fmt.Printf("rm %s\n", path)
err = os.Remove(path)
utils.FailOn(err)
}
count += 1
}
}
return nil
})
utils.FailOn(err)
if *audit {
fmt.Printf("Would have deleted %d symlinks.\n", count)
} else if *seriously {
fmt.Printf("Successfully deleted %d symlinks.\n", count)
}
os.Exit(0)
}