diff --git a/TODO.md b/TODO.md index c7f92be..7a97d93 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,4 @@ -- Maybe add `fetch` command to download the current registry - - If so, stop downloading it with most requests - - If it has been more than x amount of time, print a notice to the user if they try to install or upgrade something without fetching, but let them proceed with their action... just a nag message - Add versioning: `flag@0.2.0`? May require more language support in slope to deal with programs wanting to use various versions on the same system -- Have `gen` build out a readme skeleton - +- Add support for an install.json file, which slp can process to install non-library code: + - Dependencies: [] + - slope version required diff --git a/main.go b/main.go index df35671..5ab43c7 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ const ( banner string = "\033[32;1mslp - slope package manager\033[0m" helptext string = ` slp docs [[-g]] [module] # open a module's readme in $PAGER +slp fetch # fetches the updated registry slp gen # creates new module dir/skeleton slp help # print usage information slp install [[-g]] [module...] # installs module(s) @@ -59,6 +60,12 @@ func main() { fmt.Println(err) os.Exit(1) } + case "fetch": + err := operators.Fetch() + if err != nil { + fmt.Println(err) + os.Exit(1) + } case "list": err := operators.List() if err != nil { diff --git a/operators/fetch.go b/operators/fetch.go new file mode 100644 index 0000000..5067c37 --- /dev/null +++ b/operators/fetch.go @@ -0,0 +1,7 @@ +package operators + +func Fetch() error { + err := downloadTheDirectory() + + return err +} diff --git a/operators/generate.go b/operators/generate.go index 08e0523..89b5969 100644 --- a/operators/generate.go +++ b/operators/generate.go @@ -95,6 +95,17 @@ func Generate() error { sf.WriteString(fmt.Sprintf("(define %s-hello-world (lambda ()\n", opts.Title)) sf.WriteString(fmt.Sprintf(" (display \"'Hello, world!' from %s\\n\")))\n\n; vim: ts=2 sw=2 expandtab ft=slope\n", opts.Title)) + rm, err := os.Create(filepath.Join(dir, "README.md")) + if err != nil { + return err + } + defer rm.Close() + rm.WriteString("# ") + rm.WriteString(opts.Title) + rm.WriteString("\n\n") + rm.WriteString(opts.Description) + rm.WriteString("\n\n") + fmt.Printf("\nPackage \033[1m%q\033[0m has been created!\n", opts.Title) return nil diff --git a/operators/install.go b/operators/install.go index 8664357..203c0bf 100644 --- a/operators/install.go +++ b/operators/install.go @@ -21,9 +21,9 @@ func Install(pkg string, global bool) error { return fmt.Errorf(" \033[2m└\033[0m \033[91mError:\033[0m package %q is already installed", pkg) } - err = downloadTheDirectory() + err = checkRegistry() if err != nil { - return fmt.Errorf(" \033[2m└\033[0m \033[91mError:\033[0m could not retrieve remote package registry") + return fmt.Errorf(" \033[2m└\033[0m \033[91mError:\033[0m %s", err.Error()) } packages, err := getPackages() diff --git a/operators/search.go b/operators/search.go index 3f4874e..6ab84d5 100644 --- a/operators/search.go +++ b/operators/search.go @@ -6,7 +6,7 @@ import ( ) func Search(val string) error { - err := downloadTheDirectory() + err := checkRegistry() if err != nil { return err } diff --git a/operators/show.go b/operators/show.go index 200dbf1..8a2d8c4 100644 --- a/operators/show.go +++ b/operators/show.go @@ -15,7 +15,7 @@ func Show(pkg string) error { if _, err := os.Stat(path); err != nil { // If the package is not locally available // get it from the registry - err := downloadTheDirectory() + err := checkRegistry() if err != nil { return err } diff --git a/operators/update.go b/operators/update.go index 51a8298..4519afb 100644 --- a/operators/update.go +++ b/operators/update.go @@ -17,7 +17,7 @@ import ( // package, to ensure it is up to date func Update(pkg string, global bool) error { // Get remote data - err := downloadTheDirectory() + err := checkRegistry() if err != nil { return fmt.Errorf(" \033[2m└\033[0m \033[91mError:\033[0m could not retrieve remote package registry") } diff --git a/operators/util.go b/operators/util.go index 3c78e48..e5e3849 100644 --- a/operators/util.go +++ b/operators/util.go @@ -10,7 +10,7 @@ import ( "os/user" "path/filepath" "strings" - // "time" + "time" ) const ( @@ -66,15 +66,23 @@ func getModBaseDir() string { return p } -func downloadTheDirectory() error { +func checkRegistry() error { modDir := getModBaseDir() path := filepath.Join(modDir, "package.json") - // Only get a new file every, at most, 15 minutes - // f, err := os.Stat(path) - // if err == nil && f.ModTime().Before(time.Now().Add(-15 * time.Minute)) { - // return nil - // } + f, err := os.Stat(path) + if err != nil { + return fmt.Errorf("The registry is not present on the system. Run: `slp fetch`") + } else if f.ModTime().Before(time.Now().Add(-7 * 24 * time.Hour)) { + fmt.Fprint(os.Stderr, "You have not run `slp fetch` in more than 7 days: consider updating the package list\n") + return nil + } + return nil +} + +func downloadTheDirectory() error { + modDir := getModBaseDir() + path := filepath.Join(modDir, "package.json") resp, err := http.Get(packageListURL) if err != nil {