Skip to content

Commit

Permalink
Add nodocs and noembeddocs build tags (#431)
Browse files Browse the repository at this point in the history
Add nodocs and noembeddocs build tags
  • Loading branch information
twpayne authored Sep 20, 2019
2 parents 5212eb3 + ad21e43 commit 3733485
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cmd/docs.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// +build !nodocs

package cmd

import (
"fmt"
"regexp"
"strings"

packr "github.com/gobuffalo/packr/v2"
"github.com/spf13/cobra"
vfs "github.com/twpayne/go-vfs"
)
Expand All @@ -24,16 +25,19 @@ func init() {
}

func (c *Config) runDocsCmd(fs vfs.FS, args []string) error {
box := packr.New("docs", "../docs")
filename := "REFERENCE.md"
if len(args) > 0 {
pattern := args[0]
re, err := regexp.Compile(strings.ToLower(pattern))
if err != nil {
return err
}
docsFilenames, err := getDocsFilenames()
if err != nil {
return err
}
var filenames []string
for _, fn := range box.List() {
for _, fn := range docsFilenames {
if re.FindStringIndex(strings.ToLower(fn)) != nil {
filenames = append(filenames, fn)
}
Expand All @@ -47,7 +51,7 @@ func (c *Config) runDocsCmd(fs vfs.FS, args []string) error {
return fmt.Errorf("%s: ambiguous pattern, matches %s", pattern, strings.Join(filenames, ", "))
}
}
data, err := box.Find(filename)
data, err := getDoc(filename)
if err != nil {
return err
}
Expand Down
19 changes: 19 additions & 0 deletions cmd/docs_embeddocs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// +build !nodocs
// +build !noembeddocs

package cmd

import packr "github.com/gobuffalo/packr/v2"

// DocsDir is unused when chezmoi is built with embedded docs.
var DocsDir = ""

var docsBox = packr.New("docs", "../docs")

func getDocsFilenames() ([]string, error) {
return docsBox.List(), nil
}

func getDoc(filename string) ([]byte, error) {
return docsBox.Find(filename)
}
27 changes: 27 additions & 0 deletions cmd/docs_noembeddocs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build !nodocs
// +build noembeddocs

package cmd

import (
"io/ioutil"
"os"
"path/filepath"
)

// DocsDir is the directory containing docs when chezmoi is built without
// embedded docs. It should be an absolute path.
var DocsDir = "docs"

func getDocsFilenames() ([]string, error) {
f, err := os.Open(DocsDir)
if err != nil {
return nil, err
}
defer f.Close()
return f.Readdirnames(-1)
}

func getDoc(filename string) ([]byte, error) {
return ioutil.ReadFile(filepath.Join(DocsDir, filename))
}
13 changes: 13 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ If you plan to package chezmoi for your distibution, then note:
* Please enable CGO, if possible. chezmoi can be built and run without CGO, but
the `.chezmoi.group` template variable may not be set on some systems.

* chezmoi includes a `docs` command which prints its documentation. By default,
the docs are embedded in the binary. You can disable this behaviour, and have
chezmoi read its docs from the filesystem by building with the `noembeddocs`
build tag and setting the directory where chezmoi can find them with the `-X
github.com/twpayne/chezmoi/cmd.DocDir={{ .PathToDocs }}` linker flag. For
example:

```
go build -tags noembeddocs -ldflags "-X github.com/twpayne/chezmoi/cmd.DocsDir=/usr/share/doc/chezmoi" .
```

To disable the `docs` command completely, use the `nodocs` build tag.

* chezmoi includes an `upgrade` command which attempts to self-upgrade. You can
remove this command completely by building chezmoi with the `noupgrade` build
tag.
Expand Down

0 comments on commit 3733485

Please sign in to comment.