From 3526c2bd5f46d83ae435d87700006cac590986f1 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Wed, 1 Mar 2023 16:42:58 +0100 Subject: [PATCH] added wwctl genconf used to generate completions Signed-off-by: Christian Goll --- Makefile | 30 ++++++++----------- .../app/wwctl/genconf/completions/main.go | 23 ++++++++++++++ .../app/wwctl/genconf/completions/root.go | 22 ++++++++++++++ internal/app/wwctl/genconf/root.go | 26 ++++++++++++++++ internal/app/wwctl/root.go | 2 ++ 5 files changed, 86 insertions(+), 17 deletions(-) create mode 100644 internal/app/wwctl/genconf/completions/main.go create mode 100644 internal/app/wwctl/genconf/completions/root.go create mode 100644 internal/app/wwctl/genconf/root.go diff --git a/Makefile b/Makefile index 61e470bc..3d57dcb8 100644 --- a/Makefile +++ b/Makefile @@ -88,6 +88,11 @@ GO_TOOLS_VENDOR := $(addprefix vendor/, $(GO_TOOLS)) GOLANGCI_LINT := $(TOOLS_BIN)/golangci-lint GOLANGCI_LINT_VERSION := v1.50.0 +# helper functions +godeps=$(shell go list -deps -f '{{if not .Standard}}{{ $$dep := . }}{{range .GoFiles}}{{$$dep.Dir}}/{{.}} {{end}}{{end}}' $(1) | sed "s%${PWD}/%%g") +WWCTL_DEPS:=$(call godeps,cmd/wwctl/main.go) +WWCLIENT_DEPS:=$(call godeps,cmd/wwclient/main.go) + # use GOPROXY for older git clients and speed up downloads GOPROXY ?= https://proxy.golang.org export GOPROXY @@ -96,7 +101,7 @@ export GOPROXY WW_GO_BUILD_TAGS := containers_image_openpgp containers_image_ostree # Default target -all: config vendor wwctl wwclient bash_completion.d man_pages config_defaults print_defaults wwapid wwapic wwapird print_mnts +all: config vendor wwctl wwclient man_pages config_defaults print_defaults wwapid wwapic wwapird print_mnts # Validate source and build all packages build: lint test-it vet all @@ -197,7 +202,7 @@ files: all install -m 0644 include/firewalld/warewulf.xml $(DESTDIR)$(FIREWALLDDIR) install -m 0644 include/systemd/warewulfd.service $(DESTDIR)$(SYSTEMDDIR) install -m 0644 LICENSE.md $(DESTDIR)$(WWDOCDIR) - cp bash_completion.d/warewulf $(DESTDIR)$(BASHCOMPDIR) + ./wwctl genconf completions > $(DESTDIR)$(BASHCOMPDIR)/wwctl cp man_pages/*.1* $(DESTDIR)$(MANDIR)/man1/ cp man_pages/*.5* $(DESTDIR)$(MANDIR)/man5/ install -m 0644 staticfiles/README-ipxe.md $(DESTDIR)$(WWDATADIR)/ipxe @@ -210,25 +215,18 @@ init: cp -r tftpboot/* $(WWTFTPDIR)/ipxe/ restorecon -r $(WWTFTPDIR) -wwctl: - cd cmd/wwctl; GOOS=linux go build -mod vendor -tags "$(WW_GO_BUILD_TAGS)" -o ../../wwctl +wwctl: $(WWCTL_DEPS) + @echo Building "$@" + @cd cmd/wwctl; GOOS=linux go build -mod vendor -tags "$(WW_GO_BUILD_TAGS)" -o ../../wwctl -wwclient: - cd cmd/wwclient; CGO_ENABLED=0 GOOS=linux go build -mod vendor -a -ldflags "-extldflags -static \ +wwclient: $(WWCLIENT_DEPS) + @echo Building "$@" + @ cd cmd/wwclient; CGO_ENABLED=0 GOOS=linux go build -mod vendor -a -ldflags "-extldflags -static \ -X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=/etc/warewulf/warewulf.conf'" -o ../../wwclient install_wwclient: wwclient install -m 0755 wwclient $(DESTDIR)$(WWOVERLAYDIR)/wwinit/$(WWCLIENTDIR)/wwclient -bash_completion: - cd cmd/bash_completion && go build -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=./etc/warewulf.conf'\ - -X 'github.com/hpcng/warewulf/internal/pkg/node.ConfigFile=./etc/nodes.conf'"\ - -mod vendor -tags "$(WW_GO_BUILD_TAGS)" -o ../../bash_completion - -bash_completion.d: bash_completion - install -d -m 0755 bash_completion.d - ./bash_completion bash_completion.d/warewulf - man_page: cd cmd/man_page && go build -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=./etc/warewulf.conf'\ -X 'github.com/hpcng/warewulf/internal/pkg/node.ConfigFile=./etc/nodes.conf'"\ @@ -297,8 +295,6 @@ contclean: rm -f wwctl rm -rf .dist rm -f $(WAREWULF)-$(VERSION).tar.gz - rm -f bash_completion - rm -rf bash_completion.d rm -f man_page rm -rf man_pages rm -f warewulf.spec diff --git a/internal/app/wwctl/genconf/completions/main.go b/internal/app/wwctl/genconf/completions/main.go new file mode 100644 index 00000000..53827131 --- /dev/null +++ b/internal/app/wwctl/genconf/completions/main.go @@ -0,0 +1,23 @@ +package completions + +import ( + "os" + + "github.com/spf13/cobra" +) + +func CobraRunE(cmd *cobra.Command, args []string) error { + myArg := "bash" + if len(args) == 1 { + myArg = args[0] + } + switch myArg { + case "zsh": + cmd.GenZshCompletion(os.Stdout) + case "fish": + cmd.GenFishCompletion(os.Stdout, true) + default: + cmd.GenBashCompletion(os.Stdout) + } + return nil +} diff --git a/internal/app/wwctl/genconf/completions/root.go b/internal/app/wwctl/genconf/completions/root.go new file mode 100644 index 00000000..1e22f904 --- /dev/null +++ b/internal/app/wwctl/genconf/completions/root.go @@ -0,0 +1,22 @@ +package completions + +import "github.com/spf13/cobra" + +var ( + baseCmd = &cobra.Command{ + Use: "completions", + Short: "shell completion", + Long: "This command generates the bash completions if no argument is given.", + RunE: CobraRunE, + Args: cobra.MaximumNArgs(1), + Aliases: []string{"bash"}, + } + Zsh bool +) + +func init() { +} + +func GetCommand() *cobra.Command { + return baseCmd +} diff --git a/internal/app/wwctl/genconf/root.go b/internal/app/wwctl/genconf/root.go new file mode 100644 index 00000000..3584164d --- /dev/null +++ b/internal/app/wwctl/genconf/root.go @@ -0,0 +1,26 @@ +package genconf + +import ( + "github.com/hpcng/warewulf/internal/app/wwctl/genconf/completions" + "github.com/spf13/cobra" +) + +var ( + baseCmd = &cobra.Command{ + Use: "genconfig", + Short: "Generate various configurations", + Long: "This command will allow you to generate different configurations like bash-completions.", + Args: cobra.ExactArgs(0), + Aliases: []string{"cnf"}, + } + ListFull bool +) + +func init() { + baseCmd.AddCommand(completions.GetCommand()) +} + +// GetRootCommand returns the root cobra.Command for the application. +func GetCommand() *cobra.Command { + return baseCmd +} diff --git a/internal/app/wwctl/root.go b/internal/app/wwctl/root.go index cc8927d6..f60918b1 100644 --- a/internal/app/wwctl/root.go +++ b/internal/app/wwctl/root.go @@ -3,6 +3,7 @@ package wwctl import ( "github.com/hpcng/warewulf/internal/app/wwctl/configure" "github.com/hpcng/warewulf/internal/app/wwctl/container" + "github.com/hpcng/warewulf/internal/app/wwctl/genconf" "github.com/hpcng/warewulf/internal/app/wwctl/kernel" "github.com/hpcng/warewulf/internal/app/wwctl/node" "github.com/hpcng/warewulf/internal/app/wwctl/overlay" @@ -52,6 +53,7 @@ func init() { rootCmd.AddCommand(server.GetCommand()) rootCmd.AddCommand(version.GetCommand()) rootCmd.AddCommand(ssh.GetCommand()) + rootCmd.AddCommand(genconf.GetCommand()) } // GetRootCommand returns the root cobra.Command for the application.