From b27951b59f9b3282516628b41ab79b0ddfedc947 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Fri, 16 Jul 2021 14:32:39 +0200 Subject: [PATCH 1/2] added binary to create bash completion also do not bail out, when configs can't be read, still the --- Makefile | 7 ++++++- cmd/bash_completion/bash_completion.go | 23 ++++++++++++++++++++++ cmd/wwctl/main.go | 1 + internal/app/wwctl/root.go | 8 ++++++++ internal/pkg/node/datastructure.go | 3 ++- internal/pkg/warewulfconf/datastructure.go | 4 ++-- 6 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 cmd/bash_completion/bash_completion.go diff --git a/Makefile b/Makefile index 0ba63bb8..b041f180 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ GOLANGCI_LINT_VERSION := v1.31.0 # built tags needed for wwbuild binary WW_BUILD_GO_BUILD_TAGS := containers_image_openpgp containers_image_ostree -all: setup_tools vendor wwctl wwclient +all: setup_tools vendor wwctl wwclient bash_completion # set the go tools into the tools bin. setup_tools: $(GO_TOOLS_BIN) $(GOLANGCI_LINT) @@ -53,6 +53,8 @@ files: all install -d -m 0755 $(DESTDIR)/etc/warewulf/ install -d -m 0755 $(DESTDIR)/etc/warewulf/ipxe install -d -m 0755 $(DESTDIR)/var/lib/tftpboot/warewulf/ipxe/ + install -d -m 0755 $(DESTDIR)/etc/bash_completion.d/ + ./bash_completion $(DESTDIR)/etc/bash_completion.d/warewulf test -f $(DESTDIR)/etc/warewulf/warewulf.conf || install -m 644 etc/warewulf.conf $(DESTDIR)/etc/warewulf/ test -f $(DESTDIR)/etc/warewulf/hosts.tmpl || install -m 644 etc/hosts.tmpl $(DESTDIR)/etc/warewulf/ cp -r etc/dhcp $(DESTDIR)/etc/warewulf/ @@ -84,6 +86,9 @@ wwctl: wwclient: cd cmd/wwclient; CGO_ENABLED=0 GOOS=linux go build -mod vendor -a -ldflags '-extldflags -static' -o ../../wwclient +bash_completion: + cd cmd/bash_completion; go build -mod vendor -tags "$(WW_BUILD_GO_BUILD_TAGS)" -o ../../bash_completion + dist: vendor rm -rf _dist/warewulf-$(VERSION) mkdir -p _dist/warewulf-$(VERSION) diff --git a/cmd/bash_completion/bash_completion.go b/cmd/bash_completion/bash_completion.go new file mode 100644 index 00000000..2a300c87 --- /dev/null +++ b/cmd/bash_completion/bash_completion.go @@ -0,0 +1,23 @@ +// usage: ./bash_completion +package main + +import ( + "fmt" + "os" + "github.com/hpcng/warewulf/internal/app/wwctl" +) + +func main() { + fh, err := os.Create(os.Args[1]) + if err != nil { + fmt.Println(err) + return + } + + defer fh.Close() + + if err := wwctl.GenBashCompletion(fh); err != nil { + fmt.Println(err) + return + } +} diff --git a/cmd/wwctl/main.go b/cmd/wwctl/main.go index 0676131e..e1e0f479 100644 --- a/cmd/wwctl/main.go +++ b/cmd/wwctl/main.go @@ -9,3 +9,4 @@ func main() { root.Execute() } + diff --git a/internal/app/wwctl/root.go b/internal/app/wwctl/root.go index e355cd37..7e810b13 100644 --- a/internal/app/wwctl/root.go +++ b/internal/app/wwctl/root.go @@ -1,6 +1,7 @@ package wwctl import ( + "io" "github.com/hpcng/warewulf/internal/app/wwctl/configure" "github.com/hpcng/warewulf/internal/app/wwctl/container" "github.com/hpcng/warewulf/internal/app/wwctl/kernel" @@ -59,3 +60,10 @@ func rootPersistentPreRunE(cmd *cobra.Command, args []string) error { } return nil } + +// GenBashCompletionFile +func GenBashCompletion(w io.Writer) error { + return rootCmd.GenBashCompletion(w) +} + + diff --git a/internal/pkg/node/datastructure.go b/internal/pkg/node/datastructure.go index 406c656d..8d7527c9 100644 --- a/internal/pkg/node/datastructure.go +++ b/internal/pkg/node/datastructure.go @@ -101,7 +101,8 @@ func init() { c, err := os.OpenFile(ConfigFile, os.O_RDWR|os.O_CREATE, 0644) if err != nil { wwlog.Printf(wwlog.ERROR, "Could not create new configuration file: %s\n", err) - os.Exit(1) + // just return silently, as init is also called for bash_completion + return } fmt.Fprintf(c, "nodeprofiles:\n") diff --git a/internal/pkg/warewulfconf/datastructure.go b/internal/pkg/warewulfconf/datastructure.go index da2dc0ad..c47a08d8 100644 --- a/internal/pkg/warewulfconf/datastructure.go +++ b/internal/pkg/warewulfconf/datastructure.go @@ -1,7 +1,6 @@ package warewulfconf import ( - "os" "github.com/hpcng/warewulf/internal/pkg/util" "github.com/hpcng/warewulf/internal/pkg/wwlog" @@ -52,6 +51,7 @@ func init() { //TODO: Check to make sure nodes.conf is found if util.IsFile(ConfigFile) == false { wwlog.Printf(wwlog.ERROR, "Configuration file not found: %s\n", ConfigFile) - os.Exit(1) + // fail silently as this also called by bash_completion + return } } From 8d5162f88a6868e5750f5095768c16e9570fe4df Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Thu, 22 Jul 2021 11:53:58 +0200 Subject: [PATCH 2/2] automatic man page generation --- Makefile | 10 +++++++++- cmd/man_page/man_page.go | 16 ++++++++++++++++ internal/app/wwctl/root.go | 11 +++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 cmd/man_page/man_page.go diff --git a/Makefile b/Makefile index b041f180..a8cb43ce 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ GOLANGCI_LINT_VERSION := v1.31.0 # built tags needed for wwbuild binary WW_BUILD_GO_BUILD_TAGS := containers_image_openpgp containers_image_ostree -all: setup_tools vendor wwctl wwclient bash_completion +all: setup_tools vendor wwctl wwclient bash_completion man_page # set the go tools into the tools bin. setup_tools: $(GO_TOOLS_BIN) $(GOLANGCI_LINT) @@ -54,7 +54,10 @@ files: all install -d -m 0755 $(DESTDIR)/etc/warewulf/ipxe install -d -m 0755 $(DESTDIR)/var/lib/tftpboot/warewulf/ipxe/ install -d -m 0755 $(DESTDIR)/etc/bash_completion.d/ + install -d -m 0755 $(DESTDIR)/usr/share/man/man1 ./bash_completion $(DESTDIR)/etc/bash_completion.d/warewulf + ./man_page $(DESTDIR)/usr/share/man/man1 + gzip $(DESTDIR)/usr/share/man/man1/wwctl*1 test -f $(DESTDIR)/etc/warewulf/warewulf.conf || install -m 644 etc/warewulf.conf $(DESTDIR)/etc/warewulf/ test -f $(DESTDIR)/etc/warewulf/hosts.tmpl || install -m 644 etc/hosts.tmpl $(DESTDIR)/etc/warewulf/ cp -r etc/dhcp $(DESTDIR)/etc/warewulf/ @@ -89,6 +92,9 @@ wwclient: bash_completion: cd cmd/bash_completion; go build -mod vendor -tags "$(WW_BUILD_GO_BUILD_TAGS)" -o ../../bash_completion +man_page: + cd cmd/man_page; go build -mod vendor -tags "$(WW_BUILD_GO_BUILD_TAGS)" -o ../../man_page + dist: vendor rm -rf _dist/warewulf-$(VERSION) mkdir -p _dist/warewulf-$(VERSION) @@ -102,6 +108,8 @@ clean: rm -f wwctl rm -rf _dist rm -f warewulf-$(VERSION).tar.gz + rm -f bash_completion + rm -f man_page install: files diff --git a/cmd/man_page/man_page.go b/cmd/man_page/man_page.go new file mode 100644 index 00000000..45d42416 --- /dev/null +++ b/cmd/man_page/man_page.go @@ -0,0 +1,16 @@ +// usage: ./bash_completion +package main + +import ( + "fmt" + "os" + "github.com/hpcng/warewulf/internal/app/wwctl" +) + +func main() { + + if err := wwctl.GenManTree(os.Args[1]); err != nil { + fmt.Println(err) + return + } +} diff --git a/internal/app/wwctl/root.go b/internal/app/wwctl/root.go index 7e810b13..c6d9023c 100644 --- a/internal/app/wwctl/root.go +++ b/internal/app/wwctl/root.go @@ -1,7 +1,7 @@ package wwctl import ( - "io" + "io" "github.com/hpcng/warewulf/internal/app/wwctl/configure" "github.com/hpcng/warewulf/internal/app/wwctl/container" "github.com/hpcng/warewulf/internal/app/wwctl/kernel" @@ -13,6 +13,7 @@ import ( "github.com/hpcng/warewulf/internal/app/wwctl/server" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/spf13/cobra" + "github.com/spf13/cobra/doc" ) var ( @@ -66,4 +67,10 @@ func GenBashCompletion(w io.Writer) error { return rootCmd.GenBashCompletion(w) } - +func GenManTree(fileName string) error{ + header := &doc.GenManHeader{ + Title: "MINE", + Section: "1", + } + return doc.GenManTree(rootCmd,header,fileName) +}