Skip to content

Commit

Permalink
test out docs server and command-tree (#89)
Browse files Browse the repository at this point in the history
* test out docs server and command-tree

use logger package instead of logrus directly
likely fix some autocomplete issues with index commands
  • Loading branch information
unRob authored Mar 19, 2023
1 parent 648e037 commit 9263e2f
Show file tree
Hide file tree
Showing 31 changed files with 1,377 additions and 122 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/fixtures/* binary
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/unrob/milpa
go 1.20

require (
git.rob.mx/nidito/chinampa v0.0.0-20230317025104-62aefa0e3bd1
git.rob.mx/nidito/chinampa v0.0.0-20230319015046-5b405d67475d
github.com/alecthomas/chroma/v2 v2.5.0
github.com/alessio/shellescape v1.4.1
github.com/bmatcuk/doublestar/v4 v4.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ cloud.google.com/go/webrisk v1.5.0/go.mod h1:iPG6fr52Tv7sGk0H6qUFzmL3HHZev1htXuW
cloud.google.com/go/workflows v1.6.0/go.mod h1:6t9F5h/unJz41YqfBmqSASJSXccBLtD1Vwf+KmJENM0=
cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoISEXH2bcHC3M=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
git.rob.mx/nidito/chinampa v0.0.0-20230317025104-62aefa0e3bd1 h1:C6hC3bsUXrx7Sp4DcaWNBpWTsHsKhXAqfdVIwF3z5nA=
git.rob.mx/nidito/chinampa v0.0.0-20230317025104-62aefa0e3bd1/go.mod h1:ImvF16HDuvzSgb1VYOlrw6v1Hy/QNNNr2drVetpEvsk=
git.rob.mx/nidito/chinampa v0.0.0-20230319015046-5b405d67475d h1:x+eaaGfShjrTAoG7HDVEU3G/wZNfRjdYBRfwfKxYGWE=
git.rob.mx/nidito/chinampa v0.0.0-20230319015046-5b405d67475d/go.mod h1:ImvF16HDuvzSgb1VYOlrw6v1Hy/QNNNr2drVetpEvsk=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down
40 changes: 34 additions & 6 deletions internal/actions/command_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,32 @@ import (
"git.rob.mx/nidito/chinampa/pkg/render"
"git.rob.mx/nidito/chinampa/pkg/runtime"
"git.rob.mx/nidito/chinampa/pkg/tree"

"github.com/sirupsen/logrus"
milpaCmd "github.com/unrob/milpa/internal/command"
"github.com/unrob/milpa/internal/logger"

"gopkg.in/yaml.v3"
)

var ctLog = logger.Sub("itself command-tree")

type serializar func(interface{}) ([]byte, error)

func addMetaToTree(t *tree.CommandTree) {
if t.Command != nil && t.Command.Meta == nil {
meta := &milpaCmd.Meta{
Path: "",
Repo: "",
Name: t.Command.Path,
Kind: milpaCmd.KindVirtual,
}
t.Command.Meta = &meta
}

for _, subT := range t.Children {
addMetaToTree(subT)
}
}

var CommandTree = &command.Command{
Path: []string{"__command_tree"},
Hidden: true,
Expand Down Expand Up @@ -85,15 +105,23 @@ var CommandTree = &command.Command{
depth := cmd.Options["depth"].ToValue().(int)
format := cmd.Options["format"].ToString()

logrus.Debugf("looking for commands at %s depth: %d", base.Name(), depth)
ctLog.Debugf("looking for commands at %s depth: %d", base.Name(), depth)
tree.Build(base, depth)

var serializationFn func(interface{}) ([]byte, error)
var serializationFn serializar
addMeta := func(res serializar) serializar {
return func(i interface{}) ([]byte, error) {
if t, ok := i.(*tree.CommandTree); ok {
addMetaToTree(t)
}
return res(i)
}
}
switch format {
case "yaml":
serializationFn = yaml.Marshal
serializationFn = addMeta(yaml.Marshal)
case "json":
serializationFn = func(t interface{}) ([]byte, error) { return json.MarshalIndent(t, "", " ") }
serializationFn = addMeta(func(t interface{}) ([]byte, error) { return json.MarshalIndent(t, "", " ") })
case "text":
outputTpl := cmd.Options["template"].ToString()

Expand Down
10 changes: 8 additions & 2 deletions internal/actions/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"time"
Expand All @@ -16,7 +17,6 @@ import (
"git.rob.mx/nidito/chinampa/pkg/errors"
"git.rob.mx/nidito/chinampa/pkg/render"
"git.rob.mx/nidito/chinampa/pkg/statuscode"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/unrob/milpa/internal/docs"

Expand All @@ -31,7 +31,7 @@ var dlog = logger.Sub("action:docs")
var AfterHelp = os.Exit

func startServer(listen, address string) error {
logrus.Warnf("Using static resources at %s", os.Getenv("MILPA_ROOT")+"/internal/static")
dlog.Warnf("Using static resources at %s", os.Getenv("MILPA_ROOT")+"/internal/static")
os.Setenv(env.HelpUnstyled, "true")
http.Handle("/static/", docs.StaticHandler())
http.HandleFunc("/", docs.RenderHandler(address))
Expand All @@ -42,6 +42,12 @@ func startServer(listen, address string) error {
}

dlog.Info("Starting help http server")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
os.Exit(0)
}()
return server.ListenAndServe()
}

Expand Down
6 changes: 4 additions & 2 deletions internal/actions/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"git.rob.mx/nidito/chinampa/pkg/command"
"git.rob.mx/nidito/chinampa/pkg/tree"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
"github.com/unrob/milpa/internal/bootstrap"
_c "github.com/unrob/milpa/internal/constants"
"github.com/unrob/milpa/internal/logger"
)

var docLog = logger.Sub("itself doctor")

func DoctorModeEnabled() bool {
if len(os.Args) < 3 {
return false
Expand Down Expand Up @@ -69,7 +71,7 @@ var Doctor = &command.Command{
if cmd.Hidden {
continue
}
logrus.Debugf("Validating %s", cmd.FullName())
docLog.Debugf("Validating %s", cmd.FullName())
report := cmd.Validate()
message := ""

Expand Down
19 changes: 9 additions & 10 deletions internal/actions/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import (

"git.rob.mx/nidito/chinampa/pkg/command"
"github.com/hashicorp/go-getter"
"github.com/sirupsen/logrus"
"github.com/unrob/milpa/internal/logger"
)

var fetchLog = logger.Sub("itself repo install")

func NormalizeRepoURI(src string) (uri *url.URL, scheme string, err error) {
fqurl, err := getter.Detect(src, os.Getenv("PWD"), getter.Detectors)
if err != nil {
err = fmt.Errorf("could not detect source for uri %s: %w", src, err)
return
}
logrus.Debugf("Detected uri: %s", fqurl)
fetchLog.Debugf("Detected uri: %s", fqurl)

uri, err = url.Parse(fqurl)
if err != nil {
Expand All @@ -32,7 +34,7 @@ func NormalizeRepoURI(src string) (uri *url.URL, scheme string, err error) {
}

// remove git:: from git::ssh://[email protected]/...
logrus.Debugf("Unwrapping uri: %s", uri.Opaque[1:])
fetchLog.Debugf("Unwrapping uri: %s", uri.Opaque[1:])
uri, err = url.Parse(uri.Opaque[1:])
if err != nil {
err = fmt.Errorf("could not parse unwrapped URI %s: %w", uri.String(), err)
Expand Down Expand Up @@ -77,24 +79,21 @@ var Fetch = &command.Command{

uri, scheme, err := NormalizeRepoURI(src)
if err != nil {
logrus.Fatal(err)
fetchLog.Fatal(err)
}

if scheme == "file" {
logrus.Fatal("Refusing to copy local folder")
// fmt.Print(dst + "/" + folder)
// os.Exit(3)
// return nil
fetchLog.Fatal("Refusing to copy local folder")
}

folder := RepoFolderName(uri)
logrus.Debugf("Downloading %s to %s using %s", src, dst+"/"+folder, scheme)
fetchLog.Debugf("Downloading %s to %s using %s", src, dst+"/"+folder, scheme)

err = getter.Get(dst+"/"+folder, src)
if err != nil {
return err
}
logrus.Debug("Download complete")
fetchLog.Debug("Download complete")
fmt.Print(dst + "/" + folder)
return nil
},
Expand Down
31 changes: 16 additions & 15 deletions internal/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
"strings"
"time"

"github.com/sirupsen/logrus"
_c "github.com/unrob/milpa/internal/constants"
"github.com/unrob/milpa/internal/errors"
"github.com/unrob/milpa/internal/logger"
"github.com/unrob/milpa/internal/util"
)

var log = logger.Sub("bootstrap")
var MilpaPath = ParseMilpaPath()

// ParseMilpaPath turns MILPA_PATH into a string slice.
Expand All @@ -42,7 +43,7 @@ func Run() error {
if envRoot != "" {
MilpaRoot = envRoot
} else {
logrus.Debugf("%s is not set, using default %s", _c.EnvVarMilpaRoot, envRoot)
log.Debugf("%s is not set, using default %s", _c.EnvVarMilpaRoot, envRoot)
}

if !IsDir(MilpaRoot, false) {
Expand All @@ -51,21 +52,21 @@ func Run() error {

if len(MilpaPath) != 0 && MilpaPath[0] != "" {
if util.IsTrueIsh(os.Getenv(_c.EnvVarMilpaPathParsed)) {
logrus.Debugf("%s already parsed upstream. %d items found", _c.EnvVarMilpaPath, len(MilpaPath))
log.Debugf("%s already parsed upstream. %d items found", _c.EnvVarMilpaPath, len(MilpaPath))
return nil
}

logrus.Debugf("%s is has %d items, parsing", _c.EnvVarMilpaPath, len(MilpaPath))
log.Debugf("%s is has %d items, parsing", _c.EnvVarMilpaPath, len(MilpaPath))
for idx, p := range MilpaPath {
if p == "" || !IsDir(p, true) {
logrus.Debugf("Dropping non-directory <%s> from MILPA_PATH", p)
log.Debugf("Dropping non-directory <%s> from MILPA_PATH", p)
MilpaPath = append(MilpaPath[:idx], MilpaPath[idx+1:]...)
continue
}

if !strings.HasSuffix(p, _c.RepoRoot) {
p = filepath.Join(p, _c.RepoRoot)
logrus.Debugf("Updated path to %s", p)
log.Debugf("Updated path to %s", p)
}
pathMap.Add(0, p)
}
Expand All @@ -80,7 +81,7 @@ func Run() error {
if pwd, err := os.Getwd(); err == nil {
pwdRepo := filepath.Join(pwd, _c.RepoRoot)
if IsDir(pwdRepo, false) {
logrus.Debugf("Adding pwd repo %s", pwdRepo)
log.Debugf("Adding pwd repo %s", pwdRepo)
pathMap.Add(2, pwdRepo)
}
}
Expand All @@ -96,7 +97,7 @@ func Run() error {
}

func lookupGitRepo() []string {
logrus.Debugf("looking for a git repo")
log.Debugf("looking for a git repo")
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

Expand All @@ -112,15 +113,15 @@ func lookupGitRepo() []string {
repoRoot := strings.TrimSuffix(stdout.String(), "\n")
gitRepo := filepath.Join(repoRoot, _c.RepoRoot)
if IsDir(gitRepo, false) {
logrus.Debugf("Found repo from git: %s", gitRepo)
log.Debugf("Found repo from git: %s", gitRepo)
return []string{gitRepo}
}
}
return []string{}
}

func lookupUserRepos() []string {
logrus.Debugf("looking for user repos")
log.Debugf("looking for user repos")
found := []string{}
home := os.Getenv("XDG_DATA_HOME")

Expand All @@ -129,7 +130,7 @@ func lookupUserRepos() []string {
}

if home == "" {
logrus.Debugf("Ignoring user repo lookup, neither XDG_DATA_HOME nor HOME were found in the environment")
log.Debugf("Ignoring user repo lookup, neither XDG_DATA_HOME nor HOME were found in the environment")
return found
}

Expand All @@ -138,26 +139,26 @@ func lookupUserRepos() []string {
for _, file := range files {
userRepo := filepath.Join(userRepos, file.Name())
if IsDir(userRepo, true) {
logrus.Debugf("Found user repo: %s", userRepo)
log.Debugf("Found user repo: %s", userRepo)
found = append(found, userRepo)
}
}
} else {
logrus.Warnf("User repo directory not found: %s", userRepos)
log.Warnf("User repo directory not found: %s", userRepos)
}

return found
}

func lookupGlobalRepos() []string {
logrus.Debugf("looking for global repos")
log.Debugf("looking for global repos")
found := []string{}
globalRepos := filepath.Join(MilpaRoot, "repos")
if files, err := os.ReadDir(globalRepos); err == nil {
for _, file := range files {
globalRepo := filepath.Join(globalRepos, file.Name())
if IsDir(globalRepo, true) {
logrus.Debugf("Found global repo: %s", globalRepo)
log.Debugf("Found global repo: %s", globalRepo)
found = append(found, globalRepo)
}
}
Expand Down
3 changes: 1 addition & 2 deletions internal/bootstrap/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sort"
"sync"

"github.com/sirupsen/logrus"
"github.com/unrob/milpa/internal/util"
)

Expand All @@ -20,7 +19,7 @@ func IsDir(path string, warn bool) bool {
}

if warn {
logrus.Warnf("Discarding non-directory <%s> from MILPA_PATH", path)
log.Warnf("Discarding non-directory <%s> from MILPA_PATH", path)
}

return false
Expand Down
4 changes: 2 additions & 2 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"strings"

"git.rob.mx/nidito/chinampa/pkg/command"
"github.com/sirupsen/logrus"
_c "github.com/unrob/milpa/internal/constants"
"github.com/unrob/milpa/internal/errors"
"github.com/unrob/milpa/internal/logger"
"gopkg.in/yaml.v3"
)

Expand All @@ -22,7 +22,7 @@ func New(path string, repo string) (cmd *command.Command, err error) {
if err := canRun(cmd); err != nil {
return err
}
logrus.Debugf("running command %s", cmd.FullName())
logger.Sub(cmd.FullName()).Debugf("running command")

env := ToEval(cmd, []string{})

Expand Down
6 changes: 3 additions & 3 deletions internal/docs/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func contentsForRequest(comps []string) ([]byte, string, error) {
return contents, desc, fmt.Errorf("not found: %s", comps)
}

fuckingDocs := len(args) == 2 && (args[0] == "help" && args[1] == "docs")
if cmd.Name() == "docs" || fuckingDocs {
isDocsCommand := len(args) == 2 && (args[0] == "help" && args[1] == "docs")
if cmd.Name() == "docs" || isDocsCommand {
log.Debugf("Rendering docs for args %s", args)
helpMD.WriteString("\n")
if len(args) == 0 || fuckingDocs {
if len(args) == 0 || isDocsCommand {
log.Info("Rendering docs main help page")
cmd.SetOutput(&helpMD)
if err := cmd.Help(); err != nil {
Expand Down
Loading

0 comments on commit 9263e2f

Please sign in to comment.